欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中音頻處理庫pydub的使用教程

 更新時間:2017年06月07日 10:07:18   作者:gent__chen  
這篇文章主要給大家介紹了關于Python中音頻處理庫pydub的使用教程,pydub是Python中用戶處理音頻文件的一個庫,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。

前言

pydub是Python中用戶處理音頻文件的一個庫。本文主要介紹了關于Python音頻處理庫pydub使用的相關內(nèi)容,分享出來供大家參考學習,下面來看看詳細的介紹:

安裝:

  1、安裝pip工具:sudo apt-get install python-pip

  2、安裝pydub:sudo pip install pydub

  3、pydub依賴于ffmpeg,所以還需要安裝ffmpeg,由于Ubunbtu14.04官方源移除了ffmpeg,因此通過ppa源安裝:

 sudo apt-add-repository ppa:mc3man/trusty-media
 sudo apt-get update
 sudo apt-get install ffmpeg

使用:

AudioSegment方法能夠?qū)⒁粋€音頻文件打開成AudioSegment示例,并使用各種方法處理音頻,使用前先調(diào)用from pydub import AudioSegment

打開音頻:

sound1 = AudioSegment.from_file("/path/to/sound.wav", format="wav") //默認mp3格式

sound2 = AudioSegment.from_file("/path/to/another_sound.mp3", format="mp3")等價于sound1
 = AudioSegment.from_mp3("/path/to/sound.mp3")

音量處理:

louder = sound1 + 6 //sound1 聲音提高6dB

quieter = sound1 - 6 //sound1 聲音降低6dB

combined = sound1 + sound2  //sound1 和sound2疊加

duration_in_milliseconds = len(sound1)  //獲取sound的時長

beginning = sound1[:5000] //獲取sound1的前5秒音頻數(shù)據(jù)

end = sound1[-5000:]  //獲取sound1的后5秒音頻數(shù)據(jù)

注意:

1、對于多個音頻的計算,需要多個音頻之間的通道數(shù)、幀數(shù)、采樣率以及比特數(shù)都一樣,否則低質(zhì)量的音頻會向高質(zhì)量的轉(zhuǎn)換,單聲道會向立體聲轉(zhuǎn)換,低幀數(shù)向高幀數(shù)轉(zhuǎn)換。

2、AudioSegment原生就支持wav和raw,如果其他文件需要安裝ffmpeg。raw還需要,sample_width,frame_rate,channels三個參數(shù)。

生成文件:

export()方法可以使一個AudioSegment對象轉(zhuǎn)化成一個文件。

sound = AudioSegment.from_file("/path/to/sound.wav", format="wav") 

file_handle = sound.export("/path/to/output.mp3", format="mp3")  //簡單輸出

file_handle = sound.export("/path/to/output.mp3", 
       format="mp3",
       bitrate="192k",
       tags={"album": "The Bends", "artist": "Radiohead"})   //復雜輸出

AudioSegment.empty():

AudioSegment.empty()用于生成一個長度為0的AudioSegment對象,一般用于多個音頻的合并。

sounds = [
 AudioSegment.from_wav("sound1.wav"), 
 AudioSegment.from_wav("sound2.wav"), 
 AudioSegment.from_wav("sound3.wav"), 
]
playlist = AudioSegment.empty()
for sound in sounds:
 playlist += sound

AudioSegment.silent():

ten_second_silence = AudioSegment.silent(duration=10000) //產(chǎn)生一個持續(xù)時間為10s的無聲AudioSegment對象

獲取參數(shù):

此外,還能通過AudioSegment獲取音頻的參數(shù),同時還能修改原始參數(shù)。

具體詳見:https://github.com/jiaaro/pydub/blob/master/API.markdown

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

最新評論