0

I'have written a dash player in python that receives chunks from the DASH server using adaptive bitrate algorithm. Once entire video streaming is over, I need to concatenate all following received segments into one single mp4 file.

root@cap31:~/don# ls
chunk0-00001.m4s  chunk2-00015.m4s  chunk2-00035.m4s  chunk2-00055.m4s  chunk2-00075.m4s  chunk3-00010.m4s  chunk3-00030.m4s  chunk3-00050.m4s  chunk3-00070.m4s
chunk0-index.m4s  chunk2-00017.m4s  chunk2-00037.m4s  chunk2-00057.m4s  chunk2-00077.m4s  chunk3-00012.m4s  chunk3-00032.m4s  chunk3-00052.m4s  chunk3-00072.m4s
chunk1-00002.m4s  chunk2-00019.m4s  chunk2-00039.m4s  chunk2-00059.m4s  chunk2-00079.m4s  chunk3-00014.m4s  chunk3-00034.m4s  chunk3-00054.m4s  chunk3-00074.m4s
chunk1-index.m4s  chunk2-00021.m4s  chunk2-00041.m4s  chunk2-00061.m4s  chunk2-00081.m4s  chunk3-00016.m4s  chunk3-00036.m4s  chunk3-00056.m4s  chunk3-00076.m4s
chunk2-00003.m4s  chunk2-00023.m4s  chunk2-00043.m4s  chunk2-00063.m4s  chunk2-00083.m4s  chunk3-00018.m4s  chunk3-00038.m4s  chunk3-00058.m4s  chunk3-00078.m4s
chunk2-00005.m4s  chunk2-00025.m4s  chunk2-00045.m4s  chunk2-00065.m4s  chunk2-00085.m4s  chunk3-00020.m4s  chunk3-00040.m4s  chunk3-00060.m4s  chunk3-00080.m4s
chunk2-00007.m4s  chunk2-00027.m4s  chunk2-00047.m4s  chunk2-00067.m4s  chunk2-index.m4s  chunk3-00022.m4s  chunk3-00042.m4s  chunk3-00062.m4s  chunk3-00082.m4s
chunk2-00009.m4s  chunk2-00029.m4s  chunk2-00049.m4s  chunk2-00069.m4s  chunk3-00004.m4s  chunk3-00024.m4s  chunk3-00044.m4s  chunk3-00064.m4s  chunk3-00084.m4s
chunk2-00011.m4s  chunk2-00031.m4s  chunk2-00051.m4s  chunk2-00071.m4s  chunk3-00006.m4s  chunk3-00026.m4s  chunk3-00046.m4s  chunk3-00066.m4s  chunk3-index.m4s
chunk2-00013.m4s  chunk2-00033.m4s  chunk2-00053.m4s  chunk2-00073.m4s  chunk3-00008.m4s  chunk3-00028.m4s  chunk3-00048.m4s  chunk3-00068.m4s

I tried all the solutions mentioned on following links: https://trac.ffmpeg.org/wiki/Concatenate ffmpeg converting m4s to mp4

Most common solution was to append the cat files in mp4:

for x in *index* *-*[0-9]*.m4s; do cat $x >> output.mp4; done

But this video does not play after the second init segment starts rendering resulting into video getting stuck. I believe maybe because in my case, I have multiple init segments pointing to different chunks.

Hence, I'm looking for some way to get the one single mp4 video file by concatanating all these init segments and chunks correctly.

Umakant
  • 2,106
  • 1
  • 7
  • 12
  • If you are bale to use a library or command line there are various solutions like https://github.com/Viblast/dash-proxy and https://ytdl-org.github.io/youtube-dl/index.html – Mick Nov 17 '22 at 11:17
  • @umakant **(1)** When you say _"using adaptive bitrate algorithm"_ does that mean different resolutions? if yes, then just save as different files. MPEG decoders don't like mixed content (like mixed resolutions or mixed H264 Profiles). **(2)** There should be only one header in the MP4 which lists all the frames & their byte positions and byte sizes. You need to use a **hex editor** (or even a programming language to code an automation of process). This means from each `.m4s` you extract the header and add its information into the one/main header. An MP4 has 2 sections, header + media data. – VC.One Dec 05 '22 at 03:56
  • For header editing you can [get tips from this Answer](https://stackoverflow.com/a/18552833/2057709) **(3)** Alternatively just use a tool like [FFmpeg to concat](https://www.google.co.uk/search?q=ffmpeg+concat+m4s+into+mp4). Try something and ask a new question about your FFmpeg attempt – VC.One Dec 05 '22 at 03:59
  • Thanks everyone for your inputs. I found the answer. So apparently, I had to concatenate init segment with each video chunk. And then concatenate those chunks together to get the full video. – Umakant Feb 27 '23 at 22:28

0 Answers0