python处理音频
1、安装pydub
直接pip install pydub就可以了
2、安装ffmpeg
下载地址:[https://ffmpeg.zeranoe.com/builds/](https://ffmpeg.zeranoe.com/builds/)
解压到任意目录下,再将bin文件夹添加到系统环境变量的path中
3、处理音频
file_name =r"123.mp3"
sound = pydub.AudioSegment.from_mp3(file_name)
start_time ="0:30"
stop_time ="0:47"
print("time:", start_time, "~", stop_time)
start_time = (int(start_time.split(':')[0])*60+int(start_time.split(':')[1]))*1000
stop_time = (int(stop_time.split(':')[0])*60+int(stop_time.split(':')[1]))*1000
print("ms:", start_time, "~", stop_time)
word = sound[start_time:stop_time]
save_name ="word111"+file_name[6:] +"." + file_name.split(".")[1]
print(save_name)
word.export(save_name, format=file_name.split(".")[1], tags={'artist':'AppLeU0', 'album': save_name[:-4]})
运行以后可能会报错,找不到文件。可以在代码里添加一条:
pydub.AudioSegment.ffmpeg = r"D:\ffmpeg-20191113-a7245ad-win64-static\bin\ffmpeg"
给pydub设置ffmpeg的绝对路径。找了好久才找到原因。有不同意见在评论区评论。