Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python library to modify MP3 audio without transcoding

Tags:

python

mp3

codec

I am looking for some general advice about the mp3 format before I start a small project to make sure I am not on a wild-goose chase.

My understanding of the internals of the mp3 format is minimal. Ideally, I am looking for a library that would abstract those details away. I would prefer to use Python (but could be convinced otherwise).

I would like to modify a set of mp3 files in a fairly simple way. I am not so much interested in the ID3 tags but in the audio itself. I want to be able to delete sections (e.g. drop 10 seconds from the 3rd minute), and insert sections (e.g. add credits to the end.)

My understanding is that the mp3 format is lossy, and so decoding it to (for example) PCM format, making the modifications, and then encoding it again to MP3 will lower the audio quality. (I would love to hear that I am wrong.)

I conjecture that if I stay in mp3 format, there will be some sort of minimum frame or packet-size to deal with, so the granularity of the operations may be coarser. I can live with that, as long as I get an accuracy of within a couple of seconds.

I have looked at PyMedia, but it requires me to migrate to PCM to process the data. Similarly, LAME wants to help me encode, but not access the data in place. I have seen several other libraries that only deal with the ID3 tags.

Can anyone recommend a Python MP3 library? Alternatively, can you disabuse me of my assumption that going to PCM and back is bad and avoidable?

like image 670
Oddthinking Avatar asked Nov 22 '08 02:11

Oddthinking


People also ask

How do you trim an audio file in Python?

For this first, we have to install 'pydub' library to our system. We can do this using the pip command as shown below in your terminal or shell. After executing the above command pydub will be installed in your machine. In the next code, we can select the duration of the file we want to cut.

What is the name of the library used to play mp3 files in Python?

Play Mp3 Files With Python Using the playsound Package One simple way to play an mp3 file using Python is with the help of playsound library. It can be used to play both . mp3 and . wav files.


2 Answers

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.

like image 70
iconoplast Avatar answered Oct 29 '22 01:10

iconoplast


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.

like image 44
3 revs, 3 users 95% Avatar answered Oct 29 '22 00:10

3 revs, 3 users 95%