Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I seamlessly concatenate MP3 streams?

I'm working on a streaming server that will be capable of broadcasting targetted ads. Basically listeners hear the same music, but every, say, 30 minutes comes a block of ads and every listener has his/her own block. Implementing such streaming server poses various problems and this question is about one of them.

The server will work in a manner similar to Icecast, i.e. it will read the stream over the network from some stream generator and relay it to every listener. When it's time to broadcast ads, the server stops fetching the stream from the generator, reads ads from files and inserts them into each listener's buffer, transmits them and resumes on relaying stream from the generator.

When the server switches from relaying stream to broadcasting ads, it has to concatenate two MP3 streams (we broadcast in MP3). My concern is that simply appending one piece of data after another may produce some audible artifacts. Can it be done seamlessly?

I've already figured out this: - I can make the server be aware of MP3 frames to avoid sync errors. - I'm thinking about appending MP3 frames from the ad file after MP3 frames from the stream. - Since ad is loaded from properly encoded MP3 file, I circumvent the problem of byte reservoir, because the first frame from the file can't use it.

But my concern is the way MDCT works. Listeners have no idea of what my server will do, so their MP3 decoders may produce some artifacts because incorrect MDCT data will be placed one after another in the stream they download. Will zero-padding at the beginning of the file with the ad compensate for this?

Do you know any libraries/tools (open source if possible) that can seamlessly join two MP3 files without decompressing them?

Can you point any good resources describing MP3 format? I searched Internet a lot, found lots of information, but I still miss the overall picture.

Maybe you know that this would be easier if I used another codec like OGG/Vorbis, AAC?

PS. This question is not a duplicate of What is the best way to merge mp3 files?. mp3wrap and tools alike are not an option for me.

like image 419
Jasiu Avatar asked Nov 06 '22 21:11

Jasiu


1 Answers

I believe MP3s can be merged by simply concatenating the files. In some quick testing (cat file1.mp3 file2.mp3 > merged.mp3; mplayer merged.mp3) it seems to work as expected. Streaming from a web server probably will work just as well.

How are you going to handle switching the current input file? You can simply treat the advertisements as short tracks to play.

like image 75
John Millikin Avatar answered Nov 16 '22 17:11

John Millikin