1: Application scenario
Some equipment requires dual-track recording, and the files in the wav recording format are very large. When uploading, you need to convert it to MP3 format.
Two: environment
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