Reputation: 55
I'd like to seperate the channels of a mp3 file in Python and save it in two other files. Does anybody know a library for this. Thanks in advance.
Upvotes: 3
Views: 1981
Reputation: 1624
The left and right channels are separated from the loaded audio array with librosa
, the separated channels are saved as new audio files using soundfile
import librosa
import soundfile as sf
input_file_path = "your_input_file.mp3"
output_left_path = "output_left.wav"
output_right_path = "output_right.wav"
audio, sr = librosa.load(input_file_path, sr=None, mono=False)
# separate channels
left_channel = audio[0, :]
right_channel = audio[1, :]
# save
sf.write(output_file_left, left_channel, sr)
sf.write(output_file_right, right_channel, sr)
Upvotes: 1
Reputation: 1778
I assume you want to split the channels losslessly, without decoding MP3 and re-encoding it - otherwise you would not have mentioned MP3 at all and would have easily found many tools like Audacity to do that.
There are 4 channel modes of MP3 frames - this means 4 types of MP3 files: simple stereo, joint-stereo, dual-channel, mono. joint-stereo files can't be split without loss. mono files doesn't need splitting. The rest: stereo and dual-channel, consists of less than 0.1% of all MP3 files, technically can be split into 2 files, each for a channel, without loss. However there is not any tool on the Internet to do that - not any command line tool nor any GUI tool, because few need the function.
There are not any python library for you neither. Most libraries abstracted MP3 files into a common audio which you can manipulate, after decoding. pymad is the only one specific to MP3 file, and it can tell if a file is using any of the 4 channel modes, but does not offer to extract a channel without decoding it. If you write a new tool, you will have to work on raw MP3 files or produce a library for it.
And it is not easy to write a tool or library for it. It's one stream with 2 channels and not two streams interleaved on a frame level. You cannot simply work on MP3 frames, drop some frames, keep others, and manage to extract a channel out that way. It's a task for a professional, and perhaps best happen in a decoder project (like lame or libmad) and not in a file manipulation project (like mp3info or the python eyeD3). In other words, this feature is likely written in C, not python.
Implementaiton Note:
The task to build such a tool thus suits well for a computer science C-programming language course project: 1. it takes a lot of time to do; 2. it requires every skill learned from C programming course; 3. it can get wrong easily; 4. it is likely built on the work of other projects, a lesson of adaptating existing work; 5. is a damn-hard endeavor that no-one did before and thus very rewarding 6. perhaps can be done in 300 difficult lines of code instead of bloated simple Visual Basic code, thus is a good lession of modesty and quality; 7. and finally: nobody is waiting in an hurry for a working implementation.
All condition fits perfectly for a C-programming course project.
Implementation Note 2:
some bit-rates are only possible in mono mode (80kbps), and some bit-rates are only possible in stereo mode (e.g. 320kpbs). Luckily this does not present a problem in this task, because all dual-mp3 bit-rate can be mapped into a fitting mono-mp3 bit-rate -- but not vice versa!
Upvotes: 3