安裝pydub: pip install pydub
使用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)
#取得兩個(gè)MP3文件的聲音分貝
db1 = song1.dBFS
db2 = song2.dBFS
song1 = song1[300:] #從300ms開始截取英文MP3
#調(diào)整兩個(gè)MP3的聲音大小欲虚,防止出現(xiàn)一個(gè)聲音大一個(gè)聲音小的情況
dbplus = db1 - db2
if dbplus < 0: # song1的聲音更小
song1+=abs(dbplus)
elif dbplus > 0: #song2的聲音更小
song2+=abs(dbplus)
#拼接兩個(gè)音頻文件
song = song1 + song2
#導(dǎo)出音頻文件
song.export(targetPath, format="mp3") #導(dǎo)出為MP3格式