simplitia1 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks: is there a way to join two or more Mp3 files into a single Mp3. I tried opening the files, binmode, and writing back to a single file. The result is strange because the final file would only play the first file that was written, furthermore subsequent files added would only play if its the same file that was written first. Pretty much, it does not work! Would greatly appreciate it if there is a module or someway to do this. Thank very much monks! AL

Replies are listed 'Best First'.
Re: Joing two mp3 files into one
by ww (Archbishop) on Jun 05, 2012 at 23:25 UTC
    Wouldn't it be easier to use a PD or shareware audio editor?

    mp3 files have a header -- so to make one track of multiple tunes, you'll need (I think) to edit the header information so that your player knows it should continue past the end of the first tune (and either move the second tune's minimum-required header info to before that first tune or re-write your player to accept the need to skip over the second header... and so on and so forth... ad nauseum...
    and that doesn't even begin to deal with footers (if any).

    CPAN does, however, hold some promise, despite the fact that most items with mp3 in the name seem to deal with tagging, not editing.

    • Audio::Digest::MP3
    • (possibility?): MP3::Album::Track
    • Audio::MPEG
    • etc, maybe.

    If you're not familiar with CPAN, take a gander; you'll find it invaluable.

Re: Joing two mp3 files into one
by mbethke (Hermit) on Jun 06, 2012 at 01:28 UTC
    Incidentally the MP3 file format is designed so that it should be possible to concatenate files by simply writing one after the other; players must skip frames they can't process so they'd just ignore the intervening header. However, this only works if the encoding is exactly the same regarding sample rate, bps and $DEITY knows what else, so in practice it usually doesn't. Unless it's really necessary to do it in Perl, I'd second ww in recommending some external audio editor such as SOX.
      Thanks monks! this is correct - the only way to combined the files was to completely re-encode each mp3 with audacity. I even tried using a MP3::Tag to strip away existing tags but it was not enough. The only way is to re-encode, then the joining works. After encoding it was just simply opening each of the files, binmode both filehandle, binmode output filehandle and writing it the new file. thanks again. AL

        There is a command-line utility called mp3cat which you should be able to compile on most *nix systems. If you have to do this very often, it may be much faster than firing up a GUI program to re-encode the files.

        Aaron B.
        Available for small or large Perl jobs; see my home node.