axiomatic-systems / Bento4

Full-featured MP4 format, MPEG DASH, HLS, CMAF SDK and tools
http://www.bento4.com
1.97k stars 479 forks source link

Concatenate MP4 #365

Open mlevkov opened 5 years ago

mlevkov commented 5 years ago

Hello,

I have a large list of mp4 files (about 1000 files) that I'm trying to concatenate into one continuous stream. All of these files are from the same file, they were sectioned off and now I need to put them back together as if they were one file. I tried to use MP4Mux but it adds them as each individual tracks. Do any of the tools in Bento4 set have the ability to join multiple mp4 files into 1 mp4 file?

Thank you

barbibulle commented 5 years ago

If they're from the same file, then the sample descriptions are identical, so you should be able to do this in at least one of two ways: 1/ Extract the elementary streams from your files (mp42avc, mp42hevc for video and mp42aac for audio depending on the codecs used), concatenate the elementary streams (simple file concatenation): all the video into one file, all the audio in another file. Then use mp4mux to remux the audio and video. 2/ make sure all your individual files are in "fragmented mp4" form (use 'mp4fragment' if that's not the case. Then for each file, extract the 'init segment' and 'media segments' using 'mp4split'. Then create a final file by concatenating one of the init segments (they should be all the same) followed by all the media segments, ordered by time.

mlevkov commented 5 years ago

This approach is rather interesting. Though I found the solution already but I definitely want to try this method out. Will let you know how it goes. Thank you so much for the explanation and feedback. Stay tuned.

maudit-4047 commented 4 years ago

@mlevkov what solution u found?

mqudsi commented 2 years ago

@barbibulle is it a fundamental limitation of the mp4 format that the sample descriptions must match for lossless concatenation to be performed? As I understand it, there could technically be multiple sample descriptions, right? Or even each input could be included as a separate but gapless track in an output fragmented mp4?

barbibulle commented 2 years ago

The MP4 format does indeed allow you to specify multiple sample descriptions at the start of the file. The individual audio/video frames then refer to the sample descriptions by index. That still requires that you know ahead of time what sample descriptions you're going to have in the rest of the content.