from pydub import AudioSegment
import os

# 指定文件夹路径
folder_path = r"C:\Users\Administrator\Downloads\test"

# 存储文件名的列表
wav_files_list = []

# 遍历文件夹中的文件
for filename in os.listdir(folder_path):
    # 获取文件的完整路径
    file_path = os.path.join(folder_path, filename)

    # 判断是否为WAV文件
    if filename.lower().endswith('.wav') and os.path.isfile(file_path):
        # 将文件名添加到列表中
        wav_files_list.append(filename)

for file_name in wav_files_list:
    file_name = file_name.split(".")[0]
    wav_file = AudioSegment.from_wav(folder_path + '\\' + '{}.wav'.format(file_name))

    # 设置导出参数
    bitrate = '192k'  # 比特率为192kbps
    channels = 2  # 双声道

    # 导出为MP3文件
    save_path = r"C:\Users\Administrator\Downloads\test2"
    mp3_file = save_path + '\\' '{}.mp3'.format(file_name)
    wav_file.export(mp3_file, format='mp3', bitrate=bitrate, parameters=['-ac', str(channels)])