Share

目录下的wma批量转成MP3

import os
from pydub import AudioSegment

# 设置原始WMA文件所在的目录
source_directory = '.'
# 设置转换后的MP3文件要保存的新目录
destination_directory = 'in-mp3'

# 如果目标目录不存在,则创建它
if not os.path.exists(destination_directory):
    os.makedirs(destination_directory)

# 遍历源目录中的所有文件
for filename in os.listdir(source_directory):
    if filename.endswith('.wma'):
        # 构建原始文件的完整路径
        source_file_path = os.path.join(source_directory, filename)
        
        # 使用pydub加载WMA文件
        audio = AudioSegment.from_file(source_file_path)
        
        # 构建MP3文件的新文件名和路径
        mp3_filename = os.path.splitext(filename)[0] + '.mp3'
        destination_file_path = os.path.join(destination_directory, mp3_filename)
        
        # 导出MP3文件到新目录
        audio.export(destination_file_path, format='mp3')
        
        print(f'Converted {filename} to {mp3_filename}')

print('All files have been converted and saved to the new directory.')


#pip install pydub