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

Question:

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!

Asked By: Eamonn

||

Answers:

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")
Answered By: Jiaaro

Hope this helps:

    myaudio = AudioSegment.from_mp3("XXXXX/y.mp3")
    chunk_length_ms = 1000000  # pydub calculates in millisec
    chunks = make_chunks(myaudio, chunk_length_ms)  # Make chunks of one sec

    chunks.export('path where file needs to be exported' + chunks, format='mp3')  
Answered By: Chaitanya Mallepudi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.