简体   繁体   中英

How to convert many mp3 files to wav files in python

I know with the help of ffmpeg, we can convert an mp3 to wav file. But is there any code or function for automating the process. Which means I have many mp3 files, Instead of manually converting each and every file, is there any other option to convert all the mp3 files to wav files in a particular folder?

Install the module pydub

pip install pydub

Install ffmpeg

sudo apt-get install ffmpeg

Use the below code to convert all the mp3 files

from pydub import AudioSegment
import os

# files                                                                         
src_folder = "/home/user/Music/mp3"
dst_folder = "/home/user/Music/wav"

#get all music file
files = os.listdir(src_folder)
for name in files:
    #name of the file                                                           
    wav_name = name.replace(".mp3", "")
    try:
        # convert wav to mp3 
        sound = AudioSegment.from_mp3("{}/{}".format(src_folder, name))
        sound.export("{}/{}".format(dst_folder, wav_name), format="wav")
    except Exception as e:
        pass

You can find more detailed information here - Link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM