bin文件查看器app使用方法:解壓bin文件命令

Pydub是一個基於ffmpeg的Python音頻處理模塊,封裝了許多ffmpeg底層接口,因此用它來做音樂歌曲文件格式轉換會非常方便。

今天給大家介紹它的音樂文件格式轉換功能,幾乎支持所有音樂音頻格式,非常牛逼。

1.安裝

安裝Pydub前需要先安裝 ffmpeg:

(可選一) Mac (打開終端(Terminal), 用 homebrew 安裝):

brew install ffmpeg –with-libvorbis –with-sdl2 –with-theora

(可選二) Linux:

apt-get install ffmpeg libavcodec-extra

(可選三) Windows:

1. 進入
http://ffmpeg.org/download.html#build-windows,點擊 windows 對應的圖標,進入下載界面點擊 download 下載按鈕。

2. 解壓下載好的zip文件到指定目錄。

3. 將解壓後的文件目錄中 bin 目錄(包含 ffmpeg.exe )添加進 path 環境變量中。

(必須) 按照上述ffmpeg安裝步驟成功後就可以打開命令提示符(cmd)或終端(Terminal),安裝pydub:

pip install pydub

2.mp3轉wav或其他格式

將單個mp3音頻文件轉化為wav音頻格式:

from pydub import AudioSegment
def trans_mp3_to_wav(filepath):
“””
將mp3文件轉化為wav格式
Args:
filepath (str): 文件路徑
“””
song = AudioSegment.from_mp3(filepath)
filename = filepath.split(“.”)[0]
song.export(f”{filename}.wav”, format=”wav”)

可以繼續封裝該函數,將單個mp3文件轉化為任意其他音樂音頻格式:

from pydub import AudioSegment
def trans_mp3_to_any_audio(filepath, audio_type):
“””
將mp3文件轉化為任意音頻文件格式
Args:
filepath (str): 文件路徑
audio_type(str): 文件格式
“””
song = AudioSegment.from_mp3(filepath)
filename = filepath.split(“.”)[0]
song.export(f”{filename}.{audio_type}”, format=f”{audio_type}”)

如ogg格式:

trans_mp3_to_any_audio(“Alone.mp3”, “ogg”)

只要是ffmpeg支持的音樂音頻格式,它都可以轉換,支持的格式長達幾十個,下面我簡單列一些:

wavavimp4flv
oggflacapemp2
aiffvocau

3.更加通用的轉換函數

剛剛是mp3轉任意音頻格式,我希望把它寫成任意音頻格式轉任意音頻格式:

from pydub import AudioSegment
def trans_any_audio_types(filepath, input_audio_type, output_audio_type):
“””
將任意音頻文件格式轉化為任意音頻文件格式
Args:
filepath (str): 文件路徑
input_audio_type(str): 輸入音頻文件格式
output_audio_type(str): 輸出音頻文件格式
“””
song = AudioSegment.from_file(filepath, input_audio_type)
filename = filepath.split(“.”)[0]
song.export(f”{filename}.{output_audio_type}”, format=f”{output_audio_type}”)

比如將ogg音樂音頻格式轉化為flv音樂音頻格式:

trans_any_audio_types(“Alone.ogg”, “ogg”, “flv”)

或者MP4格式,總之,一般你需要的格式它都能滿足。

trans_any_audio_types(“Alone.ogg”, “ogg”, “mp4”)

4.批量轉化音頻格式

現在,嘗試將一個文件夾下的所有非mp3音頻格式的文件轉化為mp3音頻格式:

def trans_all_file(files_path, target=”mp3″):
“””
批量轉化音頻音樂格式
Args:
files_path (str): 文件夾路徑
target (str, optional): 目標音樂格式. Defaults to “mp3”.
“””
for filepath in os.listdir(files_path):
# 路徑處理
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
datapath = os.path.join(modpath, files_path + filepath)
# 分割為文件名字和後綴並載入文件
input_audio = os.path.splitext(datapath)
song = AudioSegment.from_file(datapath, input_audio[-1].split(“.”)[-1])
# 導出
song.export(f”{input_audio[0]}.{target}”, format=target)

只要輸入文件夾名稱,即可全部轉化該文件夾下的音樂文件格式為mp3格式:

trans_all_file(“F:\\push\\20200607\\music\\”)

看了我們今天的教程,學會這招後,你再也不用擔心格式之間的轉化問題,通過使用 pydub模塊你能輕易地解決這些問題。

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/220442.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 11:27
下一篇 2024-12-09 11:27

相關推薦

發表回復

登錄後才能評論