使用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)換器類(lèi)
: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"開(kāi)始轉(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實(shí)戰(zhàn)之看圖猜字游戲的實(shí)現(xiàn)
看圖猜成語(yǔ),是考驗(yàn)一個(gè)人的反應(yīng)能力,也考驗(yàn)一個(gè)人的右腦思維。據(jù)說(shuō)越聰明的人,這道題的完成率越高。本文就來(lái)用Python實(shí)現(xiàn)這一經(jīng)典小游戲,需要的可以參考一下2023-02-02
如何使用Python的Requests包實(shí)現(xiàn)模擬登陸
這篇文章主要為大家詳細(xì)介紹了使用Python的Requests包模擬登陸,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
python 定義給定初值或長(zhǎng)度的list方法
今天小編就為大家分享一篇python 定義給定初值或長(zhǎng)度的list方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
python執(zhí)行精確的小數(shù)計(jì)算方法
今天小編就為大家分享一篇python執(zhí)行精確的小數(shù)計(jì)算方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python實(shí)現(xiàn)ssh批量登錄并執(zhí)行命令
本篇文章主要是介紹了Python實(shí)現(xiàn)ssh批量登錄并執(zhí)行命令,有一些任務(wù)可以進(jìn)行批量完成,Python就可以完成,有需要的同學(xué)可以了解一下。2016-10-10
樹(shù)莓派實(shí)現(xiàn)移動(dòng)拍照
這篇文章主要為大家詳細(xì)介紹了樹(shù)莓派實(shí)現(xiàn)移動(dòng)拍照,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python實(shí)現(xiàn)多線程暴力破解登陸路由器功能代碼分享
這篇文章主要介紹了python實(shí)現(xiàn)多線程暴力破解登陸路由器功能代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-01-01
python pandas庫(kù)中DataFrame對(duì)行和列的操作實(shí)例講解
今天小編就為大家分享一篇python pandas庫(kù)中DataFrame對(duì)行和列的操作實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
python等差數(shù)列求和公式前 100 項(xiàng)的和實(shí)例
今天小編就為大家分享一篇python等差數(shù)列求和公式前 100 項(xiàng)的和實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

