Python 批量转wav录音格式为MP3

一:应用场景

   有的设备需要录制双轨录音 ,wav录音格式的文件体积很大。上传的时候就需要转化为MP3格式

二:环境

FFMPEG  Python  


"""
    wav格式转为MP3格式
    生成的MP3文件在原来的文件夹下,并且删除原来的wav格式录音
"""
import os
fd = open('d:/testerror.txt','w')  # 录音转化失败 日志
source_path = "D:/test文件测试"  # 原始录音地
url = "ffmpeg -i"
out_path = "D:/20210809/"  # 转换后的格式MP3存放的地址,注意后面要跟一个反斜杠
def getAllDir(source_path,sp=""):
    filesList = os.listdir(source_path)
    # 处理每一个文件
    sp += " "
    for fileName in filesList:
        fileAbsPath = os.path.join(source_path, fileName)
        if(os.path.isdir(fileAbsPath)):
            print(sp + "目录:", fileName, "======", fileAbsPath)
            getAllDir(fileAbsPath,sp)
        else:

            try:
                pathdirsourceFile = source_path + "/" + fileName
                print("测试地址",pathdirsourceFile)
                print("source_path", source_path.split(":/")[1])
                out_file_path=out_path+source_path.split(":/")[1]
                print("输出地址", out_file_path)
                mkdir(out_file_path)
                os.system(url + " " + "\"" + pathdirsourceFile + "\""" " + out_file_path+"/"+fileName.replace(".wav", ".mp3"))
                # os.remove(pathdirsourceFile)  # 删除原来的文件
            except:
                print("转化出错")
                fd.write(pathdirsourceFile)
                pass
        pass


def mkdir(path):
    # 去除首位空格
    path = path.strip()
    # 去除尾部 \ 符号
    path = path.rstrip("\\")

    # 判断路径是否存在
    # 存在     True
    # 不存在   False
    isExists = os.path.exists(path)

    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        # 创建目录操作函数
        os.makedirs(path)

        print(path ,' 创建成功')

        return True
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path + ' 目录已存在')

        return False




if __name__ == '__main__':
    getAllDir(source_path)
    pass

猜你喜欢

转载自blog.csdn.net/beautifulYuan/article/details/119543424