Python批量M4A文件转MP3录音文件

一:场景介绍

      M4A录音文件在有的设备上无法播放,就需要转为MP3播放。用市面上的转换工具少量录音还可以,多了就出现问题现在有15G要转换,有的还要收费。

二:工具准备

   1.这里用的开源的FFMPEG转换,下载安装,添加环境变量

   2.安装Python  3.5 ,添加环境变量

   3.开发工具是Python 推荐的Pycharm

三:缺点

     目前代码没有使用多线程,速度比较慢

四:开始代码

   这里就直接上写好的代码,前提是安装好FFmpeg 

#MA4 转mp3文件
# _*_ encoding:utf-8 _*_

import os
import multiprocessing as mp
# 打开文件
fd =open('d:/testerror.txt','w') #错误录音存放文件名

m4a_path = "D:/test文件测试/测试录音/M4A/"  # D:/test文件测试/测试录音/M4A/曹桂玉/"

dir_path = "D:/MP3/" #mp3生成后的路劲

url="ffmpeg -i"

#得到当前目录下所有的文件
def getALLDir(path,sp = ""):
    filesList = os.listdir(path)
    #处理每一个文件
    sp += " "
    for fileName in filesList:
        #判断一个文件是否为目录(用绝对路径)  join拼判断接法
        fileAbsPath = os.path.join(path,fileName)
        if os.path.isdir(fileAbsPath):#临界条件: 如果不是目录 执行else
            print(sp + "目录:",fileName,"======",fileAbsPath)
            global pathdirsource
            pathdirsource= dir_path+fileName+"/"
            print("拼接地址",pathdirsource)
            mkdir(pathdirsource)
            getALLDir(fileAbsPath,sp)#递归调用 自己调用自己
        else:

            #pool.apply_async(doWriteFile,(pathdirsource,fileName,fileAbsPath))
            #fileprocess=Process(target=doWriteFile(pathdirsource,fileName,fileAbsPath))
            #fileprocess.start()
            #print("进程id===",fileprocess.pid)
            doWriteFile(pathdirsource,fileName,fileAbsPath)


def doWriteFile(pathdirsource,fileName,fileAbsPath):
    try:
        print("地址全局", pathdirsource)
        strpile = fileName[0:-4]
        str = pathdirsource + "\"" + strpile + "\""
        print(url + " " + "\"" + fileAbsPath.replace('\\', '/') + "\""" " + str + ".mp3")
        str = os.system(url + " " + "\"" + fileAbsPath + "\""" " + str + ".mp3")
        if str == 1:
            fd.write(fileAbsPath)
            # fd.close()
            print(str, "错误")
        else:
            print(str, "====")
    except:
        print("程序错误===")


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(m4a_path)  # 需要遍历的path


猜你喜欢

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