使用Python實(shí)現(xiàn)MP3格式轉(zhuǎn)化
文件準(zhǔn)備
這是我要轉(zhuǎn)化的mp3文件,我想把它轉(zhuǎn)化為wav文件,并存儲(chǔ)到wav之中
代碼準(zhǔn)備
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)出為目標(biā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), ...] 文件路徑對(duì) :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: # 使用進(jìn)度條跟蹤批量轉(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"
這兩處第一個(gè)是轉(zhuǎn)化MP3文件的路徑,第二處是準(zhǔn)換結(jié)果wav文件的路徑
升級(jí)用法一
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式 input_format = "mp3" output_format = "wav"
我們可以修改此處的input和output,支持的格式有,ogg,wav,flac等等等等,也就是說(shuō),如果我想從MP3到flac那么我的代碼將會(huì)是
# 設(shè)置要轉(zhuǎn)換的格式,支持mp3, wav, ogg, flac等格式 input_format = "mp3" output_format = "flac"
只用修改對(duì)應(yīng)的位置即可,對(duì)應(yīng)位置在下方
升級(jí)用法二批量準(zhǔn)換
# 批量轉(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"), ]
需要修改的位置對(duì)應(yīng)到代碼處為
運(yùn)行結(jié)果
單個(gè)轉(zhuǎn)換
批量準(zhǔn)換
到此這篇關(guān)于使用Python實(shí)現(xiàn)MP3格式轉(zhuǎn)化的文章就介紹到這了,更多相關(guān)Python MP3格式轉(zhuǎn)化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用pydub庫(kù)對(duì)mp3與wav格式進(jìn)行互轉(zhuǎn)的方法
- Python使用ffmpy將amr格式的音頻轉(zhuǎn)化為mp3格式的例子
- python腳本實(shí)現(xiàn)音頻m4a格式轉(zhuǎn)成MP3格式的實(shí)例代碼
- Python實(shí)現(xiàn)批量將MP3音頻轉(zhuǎn)為WAV格式詳解
- Python實(shí)現(xiàn)將mp3音頻格式轉(zhuǎn)換為wav格式
- Python實(shí)現(xiàn)PDF轉(zhuǎn)MP3的示例代碼
- Python使用pydub實(shí)現(xiàn)M4A轉(zhuǎn)MP3轉(zhuǎn)換器
- python如何將aac轉(zhuǎn)為mp3,保持原有目錄結(jié)構(gòu)
相關(guān)文章
Python read函數(shù)按字節(jié)(字符)讀取文件的實(shí)現(xiàn)
這篇文章主要介紹了Python read函數(shù)按字節(jié)(字符)讀取文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python+wxPython實(shí)現(xiàn)將圖片轉(zhuǎn)換為草圖
將照片轉(zhuǎn)換為藝術(shù)風(fēng)格的草圖是一種有趣的方式,可以為您的圖像添加獨(dú)特的效果,本文主要介紹了如何Python和wxPython來(lái)實(shí)現(xiàn)這一目標(biāo),需要的可以參考下2023-08-08Python中實(shí)現(xiàn)文本預(yù)處理的方法小結(jié)
文本數(shù)據(jù)是數(shù)據(jù)科學(xué)和自然語(yǔ)言處理領(lǐng)域的關(guān)鍵組成部分,本文將深入探討Python中文本預(yù)處理的關(guān)鍵步驟,并提供豐富的示例代碼,希望對(duì)大家有所幫助2023-12-12python中列表和元組的用法以及區(qū)別超詳細(xì)講解
這篇文章主要介紹了Python中的列表和元組,包括它們的定義、特點(diǎn)、常見操作以及與列表的區(qū)別,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03對(duì)python產(chǎn)生隨機(jī)的二維數(shù)組實(shí)例詳解
今天小編就為大家分享一篇對(duì)python產(chǎn)生隨機(jī)的二維數(shù)組實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12python 利用Pyinstaller打包Web項(xiàng)目
這篇文章主要介紹了python 利用Pyinstaller打包Web項(xiàng)目,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-10-10Python面向?qū)ο蟪绦蛟O(shè)計(jì)示例小結(jié)
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì),結(jié)合實(shí)例形式總結(jié)分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中比較常見的類定義、實(shí)例化、繼承、私有變量等相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-01-01通過(guò)Python將MP4視頻轉(zhuǎn)換為GIF動(dòng)畫
Python可用于讀取常見的MP4視頻格式并將其轉(zhuǎn)換為GIF動(dòng)畫。本文將詳細(xì)為大家介紹實(shí)現(xiàn)的過(guò)程,文中的代碼具有一定的參考價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12Pandas merge合并操作的實(shí)現(xiàn)
Pandas的merge()函數(shù)用于合并兩個(gè)DataFrame數(shù)據(jù)表,本文就來(lái)介紹一下Pandas merge合并操作,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12