使用Python实现MP3格式转化
作者:cheese-liang
这篇文章主要为大家详细介绍了如何使用Python实现MP3格式转化为wav,flac和ogg等,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
文件准备

这是我要转化的mp3文件,我想把它转化为wav文件,并存储到wav之中
代码准备
import os
import logging
from pydub import AudioSegment
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
# 设置日志记录器
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class AudioConverter:
def __init__(self, input_format, output_format):
"""
初始化音频转换器类
: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):
"""
执行音频格式转换
:param input_file_path: 输入文件路径
:param output_file_path: 输出文件路径
"""
try:
# 检查输入文件是否存在
if not os.path.exists(input_file_path):
logging.error(f"输入文件不存在: {input_file_path}")
return
# 根据输入格式加载音频文件
logging.info(f"开始转换: {input_file_path} -> {output_file_path}")
audio = AudioSegment.from_file(input_file_path, format=self.input_format)
# 导出为目标格式
audio.export(output_file_path, format=self.output_format)
logging.info(f"转换完成: {output_file_path}")
except Exception as e:
logging.error(f"转换失败: {e}")
@staticmethod
def batch_convert(file_pairs, input_format, output_format, max_workers=4):
"""
批量音频转换,支持多线程
:param file_pairs: [(input_file_path, output_file_path), ...] 文件路径对
:param input_format: 输入文件格式
:param output_format: 输出文件格式
:param max_workers: 最大并发线程数
"""
converter = AudioConverter(input_format, output_format)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# 使用进度条跟踪批量转换
for _ in tqdm(executor.map(lambda file_pair: converter.convert(*file_pair), file_pairs), total=len(file_pairs)):
pass
# 使用示例
if __name__ == "__main__":
# 设置要转换的格式,支持mp3, wav, ogg, flac等格式
input_format = "mp3"
output_format = "wav"
# 单文件转换示例
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"
这两处第一个是转化MP3文件的路径,第二处是准换结果wav文件的路径
升级用法一
# 设置要转换的格式,支持mp3, wav, ogg, flac等格式
input_format = "mp3"
output_format = "wav"
我们可以修改此处的input和output,支持的格式有,ogg,wav,flac等等等等,也就是说,如果我想从MP3到flac那么我的代码将会是
# 设置要转换的格式,支持mp3, wav, ogg, flac等格式
input_format = "mp3"
output_format = "flac"
只用修改对应的位置即可,对应位置在下方

升级用法二批量准换
# 批量转换示例
file_pairs = [
("file1.mp3", "file1.wav"),
("file2.mp3", "file2.wav"),
("file3.mp3", "file3.wav")
]
# 批量转换mp3到wav
AudioConverter.batch_convert(file_pairs, input_format, output_format)批量转换只需将 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"),
]
# 批量转换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"),
]
需要修改的位置对应到代码处为

运行结果
单个转换

批量准换

到此这篇关于使用Python实现MP3格式转化的文章就介绍到这了,更多相关Python MP3格式转化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
