python處理音頻檔案(mp3),

來源:互聯網
上載者:User

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格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.