Merge mp3s with PHP?
July 18, 2005 3:06 PM Subscribe
How can I merge multiple MP3s into a single MP3 using a web scripting language such as PHP, ASP.NET, etc.?
You'd think this would be the kind of thing you could just look up on Google, but I've had a heck of a time finding anything close to what I'm looking for.
Specifically, I've got a bunch of small MP3 snippets, and I want to merge them end-to-end into a single MP3 file.
There are Windows-based tools that will do this, but I need to automate it for a website.
All I'm finding on Google are scripts having to do with playlists or ID3 tags. Haven't been able to come up with anything about actually manipulating mp3 files.
Any suggestions?
You'd think this would be the kind of thing you could just look up on Google, but I've had a heck of a time finding anything close to what I'm looking for.
Specifically, I've got a bunch of small MP3 snippets, and I want to merge them end-to-end into a single MP3 file.
There are Windows-based tools that will do this, but I need to automate it for a website.
All I'm finding on Google are scripts having to do with playlists or ID3 tags. Haven't been able to come up with anything about actually manipulating mp3 files.
Any suggestions?
mbrubeck, using "cat" will only play the first MP3 in the concatenated file.
If you run Cygwin or Linux, you can compile SoX to include MP3 support, then modify this script to join MP3 files on the command-line. Anything you can do on the command line you can script with PHP.
posted by Rothko at 3:30 PM on July 18, 2005
If you run Cygwin or Linux, you can compile SoX to include MP3 support, then modify this script to join MP3 files on the command-line. Anything you can do on the command line you can script with PHP.
posted by Rothko at 3:30 PM on July 18, 2005
The SoX script apparently requires decoding and then re-encoding the MP3 files, resulting in a loss of quality. It would be better to do the same thing using a program like mpgtx, which works losslessly.
posted by mbrubeck at 3:40 PM on July 18, 2005
posted by mbrubeck at 3:40 PM on July 18, 2005
What mbrubeck said.
posted by revgeorge at 3:47 PM on July 18, 2005
header('Content-type: audio/mpeg');
readfile('foo.mp3');
readfile('bar.mp3');
It's not going to make valid MP3s, but if you want archival quality materials you probably shouldn't be using MP3.posted by revgeorge at 3:47 PM on July 18, 2005
MPEG uses a robust streaming architecture for its files that means if there's garbage in the middle, it can move through it, recognise where valid MPEG data begins again, and re-synchronize.
ID3 tags are themselves garbage data added to the beginning or end of a valid chunk of MPEG frames, and the designers of ID3 assumed players would skip over them. Therefore an MP3 with tags in the middle is no less valid then one with tags anywhere else (See Section 3, here).
So yes, use concat if your needs are simple. I've jus ttried it with iTunes and QuickTime, and both play fine.
(NB This is based on hearsay, I haven't read up on it formally)
posted by cillit bang at 4:11 PM on July 18, 2005
ID3 tags are themselves garbage data added to the beginning or end of a valid chunk of MPEG frames, and the designers of ID3 assumed players would skip over them. Therefore an MP3 with tags in the middle is no less valid then one with tags anywhere else (See Section 3, here).
So yes, use concat if your needs are simple. I've jus ttried it with iTunes and QuickTime, and both play fine.
(NB This is based on hearsay, I haven't read up on it formally)
posted by cillit bang at 4:11 PM on July 18, 2005
Alright, I'm basing my information on this doc. An mp3 file format consists of a series of frames, each with its own header and payload. Therefore, in this setting you should be able to just concatenate two mp3 files together and get a valid file out.
Except for the ID3v1 tag which is 128 bits on the end. So, it would probably be best to remove the ID3 tags from each file before joining them (this is just ID3v1 tags, I don't know how v2+ work).
Except, mp3 frames are apparently allowed to reference other frames in the file. I don't know how they do this. If you're lucky, they use a relative offset so that the frame is relocatable. If you're not, then they use an absolute offset and you're screwed.
But emperical evidence suggests catting two files together should work so I guess it's a relative offset. If you can figure out how to remove the ID3 tags then I think you'll be good in almost all situations.
posted by sbutler at 4:27 PM on July 18, 2005
Except for the ID3v1 tag which is 128 bits on the end. So, it would probably be best to remove the ID3 tags from each file before joining them (this is just ID3v1 tags, I don't know how v2+ work).
Except, mp3 frames are apparently allowed to reference other frames in the file. I don't know how they do this. If you're lucky, they use a relative offset so that the frame is relocatable. If you're not, then they use an absolute offset and you're screwed.
But emperical evidence suggests catting two files together should work so I guess it's a relative offset. If you can figure out how to remove the ID3 tags then I think you'll be good in almost all situations.
posted by sbutler at 4:27 PM on July 18, 2005
Actually, PHP has an id3_remove_tag function, which is exactly what you want. Good luck!
posted by sbutler at 4:29 PM on July 18, 2005
posted by sbutler at 4:29 PM on July 18, 2005
note: ASP.NET is not a scripting language. It's a set of technologies, in which a scripting language can be a part.
posted by blue_beetle at 5:46 PM on July 18, 2005
posted by blue_beetle at 5:46 PM on July 18, 2005
Neat! I never knew that php had that, sbutler. Those php d00dz have just thought of everything, haven't they?
posted by ph00dz at 7:36 PM on July 18, 2005
posted by ph00dz at 7:36 PM on July 18, 2005
This thread is closed to new comments.
Warning: This can leave garbage like ID3 tags in the middle of the resulting file, making it technically invalid. Most programs can still play the file, but some might not.
For a more "correct" solution, you will need to call a library or executable that understands MPEG streams. What operating system are you using, and do you have a preferred programming language?
posted by mbrubeck at 3:28 PM on July 18, 2005