码迷,mamicode.com
首页 > 编程语言 > 详细

批量处理文件的Python程序

时间:2020-02-01 16:09:09      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:pen   lob   system   utf-8   程序   lin   mpeg   并保存   模板   

经常批量处理文件,这里有个python的模板,保存一下

这个例子是把目录里面所有子目录的mp3文件放慢0.85倍并保存到./processed/目录下面。

 

#coding=utf-8
import sys,os ,shutil
import struct
import glob
import time
import subprocess


def slow(file_name,to_file):
    command=‘ffmpeg -i {} -af atempo=0.85 {} ‘.format(file_name,to_file)
    print(command)
    os.system(command)

def process_file(file_name,dest_node):
    if not file_name.endswith(‘.mp3‘):
        return;
    slow(file_name,dest_node)

def process_dir(folder):
    for sub_node in glob.glob ( folder+‘/*‘ ):
        dest_node=sub_node.replace(node,"../processed/")
        if os.path.isdir(sub_node):
            if not os.path.isdir(dest_node):
                os.mkdir(dest_node)
            process_dir(sub_node)
        else:
            process_file(sub_node,dest_node)

node=sys.argv[1]
if os.path.isdir(node):
    process_dir(node)
    for pcm_file in glob.glob ( node+‘/*‘ ):
        process_file(pcm_file)
if os.path.isfile(node):
    with open(node, ‘r‘) as file_to_read:
        for line in file_to_read.readlines():
            process_file(line)

 

批量处理文件的Python程序

标签:pen   lob   system   utf-8   程序   lin   mpeg   并保存   模板   

原文地址:https://www.cnblogs.com/shinedream/p/12248661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!