Python library to modify MP3 audio without transcoding Python library to modify MP3 audio without transcoding python python

Python library to modify MP3 audio without transcoding


If you want to do things low-level, use pymad. It turns MP3s into a buffer of sample data.

If you want something a little higher-level, use the Echo Nest Remix API (disclosure: I wrote part of it for my dayjob). It includes a few examples. If you look at the cowbell example (i.e., MoreCowbell.dj), you'll see a fork of pymad that gives you a NumPy array instead of a buffer. That datatype makes it easier to slice out sections and do math on them.


I got three quality answers, and I thank you all (and upvoted you all) for them. I haven't chosen any as the accepted answer, because each addressed one aspect, so I wanted to write a summary.

Do you need to work in MP3?

  • Transcoding to PCM and back to MP3 is unlikely to result in a drop in quality.

  • Don't optimise audio-quality prematurely; test it with a simple prototype and listen to it.

Working in MP3

  • Wikipedia has a summary of the MP3 File Format.

  • MP3 frames are short (1152 samples, or just a few milliseconds) allowing for moderate precision at that level.

  • However, Wikipedia warns that "Frames are not independent items ("byte reservoir") and therefore cannot be extracted on arbitrary frame boundaries."

  • Existing libraries are unlikely to be of assistance, if I really want to avoid decoding.

Working in PCM

There are several libraries at this level:

  • LAME (latest release: October 2017)
  • PyMedia (latest release: February 2006)
  • PyMad (Linux only? Decoder only? Latest release: January 2007)

Working at a higher level

  • Echo Nest Remix API (Mac or Linux only, at the moment) is an API to a web-service that supports quite sophisticated operations (e.g. finding the locations of music beats and tempo, etc.)

  • mp3DirectCut (Windows only) is a GUI that apparently performs the operations I want, but as an app. It is not open-source. (I tried to run it, got an Access Denied installer error, and didn't follow up. A GUI isn't suitably for me, as I want to repeatedly run these operations on a changing library of files.)

My plan is now to start out in PyMedia, using PCM. Thank you all for your assistance.


Mp3 is lossy, but it is lossy in a very specific way. The algorithms used as designed to discard certain parts of the audio which your ears are unable to hear (or are very difficult to hear). Re-doing the compression process at the same level of compression over and over is likely to yield nearly identical results for a given piece of audio. However, some additional losses may slowly accumulate. If you're going to be modifying files a lot, this might be a bad idea. It would also be a bad idea if you were concerned about quality, but then using MP3 if you are concerned about quality is a bad idea over all.

You could construct a test using an encoder and a decoder to re-encode a few different mp3 files a few times and watch how they change, this could help you determine the rate of deterioration and figure out if it is acceptable to you. Sounds like you have libraries you could use to run this simple test already.

MP3 files are composed of "frames" of audio and so it should be possible, with some effort, to remove entire frames with minimal processing (remove the frame, update some minor details in the file header). I believe frames are pretty short (a few milliseconds each) which would give the precision you're looking for. So doing some reading on the MP3 File Format should give you enough information to code your own python library to do this. This is a fair bit different than traditional "audio processing" (since you don't care about precision) and so you're unlikely to find an existing library that does this. Most, as you've found, will decompress the audio first so you can have complete fine-grained control.