tags:

views:

64

answers:

3

Hello,

Do you know a class to merge two mp3 in php ?

I've found nothing serious on google.

Thank you !

+2  A: 

This is not possible. There is no implementation of the MP3 codec in PHP. You will need to use an external command-line tool to do this. (Which, depending on your server configuration, you can execute from within PHP as @ceejayoz says.)

See these questions for solutions:

Pekka
You can, however, have PHP execute the external command-line tools.
ceejayoz
+2  A: 

If by merging, you mean placing one audio over the other, then please disregard this answer.

If you dont want to re-encode the MP3s, you can probably just append them. I know this worked for MPEG movies, so I guess it could work for MP3s too. Another option would be to add the audo files to a Zip Archive with no compression and then rename the extension to .mp3.

I did a quick test and this

file_put_contents('combined.mp3',
    file_get_contents('file1.mp3') .
    file_get_contents('file2.mp3'));

worked fine. The ID3 tags will be wrong, but the resulting file contains both audio files. For some other possible gotchas, see the link in Pekka's answer.

Also, some quick googling resulted in

and some discussion

Gordon
This sounds kind of.. uhm.. dirty.
elusive
@Gordon: Sorry, i should have been more clear: Concatenating two files is not that dirty. Many formats work like that. I am talking about the ZIP-solution. This seems rather complicated and unnecessary to me.
elusive
@elusive using a Zip file has the added benefit of being able to unzip and have the mp3s back again. And also, having two solutions to pick from when the other answer is "impossible" is quite good, dont you think :)
Gordon
@Gordon: Like i said, the concatenating solution is good, but isn't ZIP using a header? An MP3 decoder does not necessarily know how to handle that.
elusive
@elusive Maybe. I am no Decoder expert. I just remembered I often did that some years ago and it worked. I just did a quick test from my desktop and it worked now too.
Gordon
@Gordon: Thats right! I did similar things like renaming ZIPs to ISOs a long time ago (which worked, too). This is really weird, but i never tried to burn one of those to CD/DVD. My loop-device was really smart, obviously ;)
elusive
I will try that. I absolutely do not care about tags and everything.
A: 

If the .mp3 files are just MPEG-1 or MPEG-2 Layer III audio, then the files can just be concatenated. There is no real concept of a header for the whole file. Each frame has a header followed by data, and the file is just comprised of a sequence of frames, which is called the bitstream. The bitrate, stereo mode, etc, do not necessarily have to be the same within a bitstream, so you can concatenate dissimilar files. The Wikipedia article explains this, but I think the spec actually is easier to understand.

ID3 tags, or an other data, within the file (which technically renders it a non-compliant bitstream) may muck things up on the decoder end.

The spec for MPEG-1 and MPEG-2 audio is actually pretty simple. Writing a parser to chunk a file into frames, and then interpreting the headers is not that much work. The last time I did this, it only took an hour or two.

The ID3 spec isn't that complicated either, so I suspect writing some code to strip out the tags before concatenation should be easy, but I have never done this.

The getID3() library (http://getid3.sourceforge.net/) may provide some guidance, too. It has been a while since I have used it, but it may also support opening MP3s and stripping out the ID3 tags already.

HTH

MPD