【问题标题】:Convert multiple MP3 files to WAV in python在python中将多个MP3文件转换为WAV
【发布时间】:2019-09-27 11:12:25
【问题描述】:

我想使用 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 文件。

请推荐

【问题讨论】:

    标签: python-3.x pydub


    【解决方案1】:

    我会这样做:

    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的更多信息。

    【讨论】:

    • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受:)
    猜你喜欢
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2019-03-11
    相关资源
    最近更新 更多