Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cat to join mp3 files. What is this black sorcery?

Tags:

A friend of mine just used plain-old cat to concatenate two mp3 files,...

cat file1.mp3 file2.mp3 > out.mp3 

...and the resulting file is perfectly reproducible, playing one song and then the next.

What is this black magic? What happened to headers, metadata? How can this work? The duration is even displayed correctly.

like image 764
slezica Avatar asked Jun 03 '11 01:06

slezica


2 Answers

An MP3 file is nothing more than the raw MPEG2-Layer 3 (audio) stream data, there is no file level header structure with, for example, duration, original source, encoding info. An MP3 stream is made of blocks starting with a synchronization marker FF Fx, so arbitrary data, such as ID3 tags, can be placed anywhere and will not affect the audio. Players either guess duration from the bitrate and file size if ID3 tags don't list this information or do a full scan of the file to accurately calculate it.

like image 194
Simon Buchan Avatar answered Oct 17 '22 07:10

Simon Buchan


Don't forget that players are typically prepared to handle variable bitrate encodings, so each frame is liable to have a different bitrate anyway.

As for metadata, that's an odd duck; even though the id3 tags from both tracks would be included in the new file, most players are only going to be looking for tags at the end of the file for display to the user, and simply skip over embedded tags in the middle of a file as known 'not-music' content. Some might play garbage or crash, but I doubt they'd be popular if they are that brittle.

And note that the mp3 headers don't encode any information about overall file size -- that's all calculated at runtime. (Perhaps through magic.)

Back when I was trying to learn German by listening to streaming radio stations, I frequently used dd to split apart giant streams by guessing how far into the track I wanted to start and stop cuts... inelegant, but no re-encoding, and my player handled it fine.

like image 43
sarnold Avatar answered Oct 17 '22 08:10

sarnold