下载是示代码 enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路径cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路径targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合并文件的路径#加载MP3文件song1 = AudioSegment.from...
求python将两个MP3音频文件拼接成一个MP3文件的代码
建议用别人现成的库,不过这个库依赖于ffmpeg或avconv。在win32上不一定好安装。其实这些工作用ffmpeg的命令行也很容易实现。
from pydub import AudioSegment
sound = AudioSegment.from_mp3("/path/to/file.mp3")
# len() and slicing are in milliseconds
halfway_point = len(sound) / 2
second_half = sound[halfway_point:]
# Concatenation is just adding
second_half_3_times = second_half + second_half + second_half
# writing mp3 files is a one liner
second_half_3_times.export("/path/to/new/file.mp3", format="mp3")2014-09-08
可以使用pydub
1 网址:
https://github.com/jiaaro/pydub2 pydub需要依赖 libav或者ffmpeg
3 在mac环境下安装依赖:(二选一)
[plain] view plaincopy
brew install libav --with-libvorbis --with-sdl --with-theora
将所有依赖都安装上~~
brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis--with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-fdk-aac --with-freetype --with-ffplay --with-ffplay --with-freetype --with-frei0r --with-libass --with-libbluray --with-libcaca --with-libquvi --with-libvidstab --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-x265
4 安装pydub: pip install pydub
5 使用pydub:
下载是示代码
enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路径cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路径targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合并文件的路径#加载MP3文件song1 = AudioSegment.from_mp3(enPath)song2 = AudioSegment.from_mp3(cnPath)#取得两个MP3文件的声音分贝db1 = song1.dBFSdb2 = song2.dBFSsong1 = song1[300:] #从300ms开始截取英文MP3#调整两个MP3的声音大小,防止出现一个声音大一个声音小的情况dbplus = db1 - db2if dbplus < 0: # song1的声音更小song1+=abs(dbplus)elif dbplus > 0: #song2的声音更小song2+=abs(dbplus)#拼接两个音频文件song = song1 + song2#导出音频文件song.export(targetPath, format="mp3") #导出为MP3格式2016-01-02