使用Python實現(xiàn)MP3格式轉(zhuǎn)化
文件準備
這是我要轉(zhuǎn)化的mp3文件,我想把它轉(zhuǎn)化為wav文件,并存儲到wav之中
代碼準備
import os import logging from pydub import AudioSegment from concurrent.futures import ThreadPoolExecutor from tqdm import tqdm # 設(shè)置日志記錄器 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class AudioConverter: def __init__(self, input_format, output_format): """ 初始化音頻轉(zhuǎn)換器類 :param input_format: 輸入文件格式 (例如: 'mp3', 'wav', 'ogg', 'flac') :param output_format: 輸出文件格式 (例如: 'mp3', 'wav', 'ogg', 'flac') """ self.input_format = input_format self.output_format = output_format logging.info(f"AudioConverter initialized for {input_format} to {output_format} conversion.") def convert(self, input_file_path, output_file_path): """ 執(zhí)行音頻格式轉(zhuǎn)換 :param input_file_path: 輸入文件路徑 :param output_file_path: 輸出文件路徑 """ try: # 檢查輸入文件是否存在 if not os.path.exists(input_file_path): logging.error(f"輸入文件不存在: {input_file_path}") return # 根據(jù)輸入格式加載音頻文件 logging.info(f"開始轉(zhuǎn)換: {input_file_path} -> {output_file_path}") audio = AudioSegment.from_file(input_file_path, format=self.input_format) # 導(dǎo)出為目標格式 audio.export(output_file_path, format=self.output_format) logging.info(f"轉(zhuǎn)換完成: {output_file_path}") except Exception as e: logging.error(f"轉(zhuǎn)換失敗: {e}") @staticmethod def batch_convert(file_pairs, input_format, output_format, max_workers=4): """ 批量音頻轉(zhuǎn)換,支持多線程 :param file_pairs: [(input_file_path, output_file_path), ...] 文件路徑對 :param input_format: 輸入文件格式 :param output_format: 輸出文件格式 :param max_workers: 最大并發(fā)線程數(shù) """ converter = AudioConverter(input_format, output_format) with ThreadPoolExecutor(max_workers=max_workers) as executor: # 使用進度條跟蹤批量轉(zhuǎn)換 for _ in tqdm(executor.map(lambda file_pair: converter.convert(*file_pair), file_pairs), total=len(file_pairs)): pass # 使用示例 if __name__ == "__main__": # 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式 input_format = "mp3" output_format = "wav" # 單文件轉(zhuǎn)換示例 input_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3" output_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav" converter = AudioConverter(input_format, output_format) converter.convert(input_file_path, output_file_path)
代碼共修改的位置一共有兩處
input_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3" output_file_path = "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"
這兩處第一個是轉(zhuǎn)化MP3文件的路徑,第二處是準換結(jié)果wav文件的路徑
升級用法一
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式 input_format = "mp3" output_format = "wav"
我們可以修改此處的input和output,支持的格式有,ogg,wav,flac等等等等,也就是說,如果我想從MP3到flac那么我的代碼將會是
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式 input_format = "mp3" output_format = "flac"
只用修改對應(yīng)的位置即可,對應(yīng)位置在下方
升級用法二批量準換
# 批量轉(zhuǎn)換示例 file_pairs = [ ("file1.mp3", "file1.wav"), ("file2.mp3", "file2.wav"), ("file3.mp3", "file3.wav") ] # 批量轉(zhuǎn)換mp3到wav AudioConverter.batch_convert(file_pairs, input_format, output_format)
批量轉(zhuǎn)換只需將 file1.MP3替換為路徑,file1.wav替換為輸出路徑
如
file_pairs = [ ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3", "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"), ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Swag _ Miyauchi _ Audio.mp3", "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Swag _ Miyauchi _ Audio.wav"), ] # 批量轉(zhuǎn)換mp3到wav AudioConverter.batch_convert(file_pairs, input_format, output_format)
為方便觀看我將()中,的部分換行,顯示到下方
file_pairs = [ ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Joel Adams - Please Don't Go (Official Music Video).mp3" , "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Joel Adams - Please Don't Go (Official Music Video).wav"), ("D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/mp3/Swag _ Miyauchi _ Audio.mp3" , "D:/400-File/000-Project/000-Pycharm/005-CSDN_File/005-convert_music/wav/Swag _ Miyauchi _ Audio.wav"), ]
需要修改的位置對應(yīng)到代碼處為
運行結(jié)果
單個轉(zhuǎn)換
批量準換
到此這篇關(guān)于使用Python實現(xiàn)MP3格式轉(zhuǎn)化的文章就介紹到這了,更多相關(guān)Python MP3格式轉(zhuǎn)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用pydub庫對mp3與wav格式進行互轉(zhuǎn)的方法
- Python使用ffmpy將amr格式的音頻轉(zhuǎn)化為mp3格式的例子
- python腳本實現(xiàn)音頻m4a格式轉(zhuǎn)成MP3格式的實例代碼
- Python實現(xiàn)批量將MP3音頻轉(zhuǎn)為WAV格式詳解
- Python實現(xiàn)將mp3音頻格式轉(zhuǎn)換為wav格式
- Python實現(xiàn)PDF轉(zhuǎn)MP3的示例代碼
- Python使用pydub實現(xiàn)M4A轉(zhuǎn)MP3轉(zhuǎn)換器
- python如何將aac轉(zhuǎn)為mp3,保持原有目錄結(jié)構(gòu)
相關(guān)文章
Python read函數(shù)按字節(jié)(字符)讀取文件的實現(xiàn)
這篇文章主要介紹了Python read函數(shù)按字節(jié)(字符)讀取文件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python+wxPython實現(xiàn)將圖片轉(zhuǎn)換為草圖
將照片轉(zhuǎn)換為藝術(shù)風(fēng)格的草圖是一種有趣的方式,可以為您的圖像添加獨特的效果,本文主要介紹了如何Python和wxPython來實現(xiàn)這一目標,需要的可以參考下2023-08-08Python中實現(xiàn)文本預(yù)處理的方法小結(jié)
文本數(shù)據(jù)是數(shù)據(jù)科學(xué)和自然語言處理領(lǐng)域的關(guān)鍵組成部分,本文將深入探討Python中文本預(yù)處理的關(guān)鍵步驟,并提供豐富的示例代碼,希望對大家有所幫助2023-12-12對python產(chǎn)生隨機的二維數(shù)組實例詳解
今天小編就為大家分享一篇對python產(chǎn)生隨機的二維數(shù)組實例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python面向?qū)ο蟪绦蛟O(shè)計示例小結(jié)
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計,結(jié)合實例形式總結(jié)分析了Python面向?qū)ο蟪绦蛟O(shè)計中比較常見的類定義、實例化、繼承、私有變量等相關(guān)使用技巧與操作注意事項,需要的朋友可以參考下2019-01-01