Export (but not save to) an audio file in PyDub?

Eamonn picture Eamonn · Jul 7, 2014 · Viewed 7.2k times · Source

The PyDub library, for me, is pretty much ideal for converting audio formats. I recently used it to write a command line audio converter to convert about 200 audio files, and it saved me having to buy or look for an audio converter that would allow me to queue up songs and other audio files for conversion. But I quickly noticed that it replaced my audio files. Now, for me, this was ideal. This was great. But what if I didn't want PyDub to replace the audio files, but rather duplicate it but in a different format? I could just copy the files into the directory and convert them, but is there no way to do this from within PyDub? I looked into it and I couldn't find a way to do this, nor could I find a question on this, so maybe this isn't a very common thing to do.

Thanks!

Answer

Jiaaro picture Jiaaro · Jul 7, 2014

When you export an audio segment, you can always specify a new name for the file (or use the same name but in a different folder)

from pydub import AudioSegment

song = AudioSegment.from_file("/path/to/file.mp3", format="mp3")
song.export("/path/to/new/filename.mp4", format="mp4")