22

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.

salezica
  • 74,081
  • 25
  • 105
  • 166

2 Answers2

10

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.

Simon Buchan
  • 12,707
  • 2
  • 48
  • 55
  • Yep - MP3 files are strange in that they're just the raw stream without any encapsulation. One correction: they're usually MPEG1-Layer3. – John Ripley Jun 03 '11 at 02:13
  • @John: Oh? I've always heard MPEG2, is that because it's the 'current' mp3? Or am I just crazy? – Simon Buchan Jun 03 '11 at 02:20
  • MPEG-numbers aren't really versions - they're just standards that increment. In this case, MPEG-2 extends the MPEG-1 audio standard with some lower bitrates, and weirdly doesn't entirely overlap with it - it doesn't allow for high bitrates. So what most people are using at 128kbit is actually MPEG-1 Layer-3. See the 'mp3 headers' link in the other answer. – John Ripley Jun 03 '11 at 03:25
9

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238