在python中将多个MP3文件转换为WAV

时间:2019-05-09 15:05:54

标签: python-3.x pydub

我想使用python代码将一个文件夹中的多个MP3音频文件转换为WAV格式(具有单声道类型)。

我使用pydub尝试了以下代码:

import os
from pydub import AudioSegment

audio_files = os.listdir('path')
# Folder is having audio files of both MP3 and WAV formats
len_audio=len(audio_files)
for i in range (len_audio):
    if os.path.splitext(audio_files[i])[1] == ".mp3":
       mp3_sound = AudioSegment.from_mp3(audio_files[i])
       mp3_sound.export("<path>\\converted.wav", format="wav")

我正在了解如何导出具有不同文件名的转换后的wav文件。

请提出建议

1 个答案:

答案 0 :(得分:0)

我会做类似的事情:

import os
from pydub import AudioSegment

path = "the path to the audio files"

#Change working directory
os.chdir(path)

audio_files = os.listdir()

# You dont need the number of files in the folder, just iterate over them directly using:
for file in audio_files:
    #spliting the file into the name and the extension
    name, ext = os.path.splitext(file)
    if ext == ".mp3":
       mp3_sound = AudioSegment.from_mp3(file)
       #rename them using the old name + ".wav"
       mp3_sound.export("{0}.wav".format(name), format="wav")

您可以找到有关格式迷你语言here的更多信息。