8

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!

2
  • 1
    You can always add an a few extra lines to make a copy of the files before converting them. Commented Jul 6, 2014 at 22:33
  • I mentioned this, but I was wondering if I could do this from within PyDub itself. Still, thanks! :D
    – Eamonn
    Commented Jul 6, 2014 at 22:54

2 Answers 2

15

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")
2
  • 5
    Can we do the same operation and hold the audio stream object in memory instead of writing to a file? Commented Jun 21, 2018 at 6:38
  • How to save multiple chunks?
    – abdoulsn
    Commented Nov 10, 2019 at 18:31
-3

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')  
2
  • 4
    What is make_chunks?
    – CristiFati
    Commented Dec 15, 2021 at 9:21
  • Does not make sence!
    – chikitin
    Commented Nov 8, 2023 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.