用FFmpeg批量处理wav音频文件到指定格式方法

import os   
# import fnmatch    
import re
import subprocess
 
 
gl_file_list = []
gl_failed_list = []
 
 
def getFilesPath(path):
    # 获得指定目录中的内容
    file_list = os.listdir(path)
    for file_name in file_list:
        new_path = os.path.join(path, file_name) 
        if os.path.isdir(new_path): 
            getFilesPath(new_path)
        elif os.path.isfile(new_path):  
            # 文件处理
            # if fnmatch.fnmatch(new_path, '*.(mp4|avi)'): # 匹配mp4格式
            #     # 视频处理
            #     fileProcessing(new_path)
            result = re.match(r".+\.(m4a|avi|mpeg|mov|flv|mpg|f4v|rmvb|mkv|ogg|asf|3gp|m4a)$", new_path)
            if result:
                gl_file_list.append(new_path)
                # fileProcessing(new_path)
        else:
            print("It's not a directory or a file.")
 
 
def fileProcessing(file_list):
    print("start----------------")
    codePre = "ffmpeg -i "
    # codeMid = " -vcodec h264 "
    # codeMid = " -vcodec libx264 -acodec aac -preset fast -b:v 2000k "
    codeMid = " -vcodec libx264 -acodec aac "
	
    for file_path in file_list:
        subname = file_path.split('.')
        print(subname)
        output_path = subname[0] + "_new.wav"   # 处理后的文件路径
        command = codePre + file_path + '-ac 1 -ar 16000' + output_path
        file_name = os.path.basename(file_path).split('.')
        # result = os.system(command)
        # if(result != 0):
        #     gl_failed_list.append(file_path)
        #     print(file_name[0], "is failed-----", "result = ", result)
        # else:
        #     print("end------", file_name[0], "result = ", result)
 
        try:
            retcode = subprocess.call(command, shell=True)
            if retcode == 0:
                print(file_name[0], "successed------")
            else:
                print(file_name[0], "is failed--------")
        except Exception as e:
            print("Error:", e)
 
    print("---------------End all-----------------")
    print("failed:", gl_failed_list)
 
 
if __name__ =='__main__':
    file_path = r'C:\Users\Administrator\Desktop\audio'
    getFilesPath(file_path)
    fileProcessing(gl_file_list)

FFmpeg处理wav文件的主要命令:

https://blog.csdn.net/qq_43318971/article/details/113096815

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值