python處理音頻檔案(mp3),
今天aiping點讀筆的MP3源檔案出現聲音大小不一致,而且需要將英文單詞MP3與單詞翻譯MP3檔案串連起來,剛開始使用ffmpeg效果很差。萬能的gitgub再次證明了它的萬能,我找到了pydub
1 網址:https://github.com/jiaaro/pydub
2 pydub需要依賴 libav或者ffmpeg
3 在mac環境下安裝依賴:(二選一)
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.dBFS db2 = song2.dBFS song1 = song1[300:] #從300ms開始截取英文MP3 #調整兩個MP3的聲音大小,防止出現一個聲音大一個聲音小的情況 dbplus = db1 - db2 if dbplus < 0: # song1的聲音更小 song1+=abs(dbplus) elif dbplus > 0: #song2的聲音更小 song2+=abs(dbplus) #拼接兩個音頻檔案 song = song1 + song2 #匯出音頻檔案 song.export(targetPath, format="mp3") #匯出為MP3格式