I have downloaded all segment files for the MPEG-DASH video stream and now I want to merge all these m4s files into a single mp4 file, how can do that using FFmpeg?
Hi Henry,
An M4S file is a segment of a video streamed using the MPEG-DASH or HLS video streaming technique. It seems that you are trying to download or store HLS or MPEG-DASH video in an mp4 format. If you have the master playlist URL for the HLS/MPEG-DASH video stream then you can directly convert it into mp4 using FFmpeg, you don’t have to download every segment (m4s) file and then combine it into a single mp4 file.
Option 1: Converting HLS/MPEG-DASH URLs directly to MP4
You can use FFmpeg to immediately convert an HLS or MPEG-DASH video stream into an MP4 format if you know the master playlist URL. This approach saves time and eliminates the need to download and merge separate segment files. This is the command to execute:
ffmpeg -i <HLS/MPEG-DASH URL> -map 0:v -map 0:a -c copy -bsf:a aac_adtstoasc -f mp4 output.mp4
In the above command,
* <HLS/MPEG-DASH URL>
: Replace this with the URL of the master playlist for the video stream.
-map 0:v -map 0:a
: These options specify to include the video and audio streams in the output.-c copy
: This option tells FFmpeg to copy the streams without re-encoding, preserving the original quality.-bsf:a aac_adtstoasc
: This filter is used to convert the audio stream to a format suitable for MP4.-f mp4
: Specifies the output format as MP4.output.mp4
: This is the name you choose for the resulting MP4 file.
Option 2: Install and combine M4S Segment Files
You can take the following actions if you would rather download separate M4S segment files and subsequently merge them into an MP4 file:
-
Download every M4S segment file available from the video stream for a specific resolution.
-
Make a text file (mylist.txt, for example) and put the file locations and/or filenames of the M4S segment files you downloaded in it. Every file ought to be on its line. For instance:
file 'segment1.m4s'
file 'segment2.m4s'
file 'segment3.m4s'
- Execute the following FFmpeg command to merge the M4S files into a single MP4 file:
ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mp4
-f concat
: Specifies the format as “concat” for file concatenation.-safe 0
: Allows FFmpeg to read files with relative paths from the text file.-i mylist.txt
: Reads the list of input files from themylist.txt
file.-c copy
: Instructs FFmpeg to copy the audio and video streams without re-encoding.output.mp4
: The name of the output MP4 file.
You can combine the downloaded M4S segment files using these settings to create a single MP4 file, which is very helpful for offline viewing or additional editing. Which option you choose will rely on your unique needs and whether the master playlist URL is available.
You can also:
Convert Mp4 to WebM video
Convert mp4 video into fragmented mp4 files
I’m focused on DASH and trying to improve the user’s QoE.
All is OK and the video can be played with a dash after I used FFmpeg to compress it at various bitrates.
What I want to do is join the user-received parts into a single m4s before turning it into an mp4.
Despite my best efforts, ffmpeg consistently produced the following error:
mov,mp4,m4a,3gp,3g2,mj2 @ 0x9a9e500] could not find corresponding track id 1 [mov,mp4,m4a,3gp,3g2,mj2 @ 0x9a9e500] could not find corresponding trex [mov,mp4,m4a,3gp,3g2,mj2 @ 0x9a9e500] error reading header test2.m4s: Invalid data found when processing input
segment_1.m4s is there.
How can I resolve this issue?
I believe you missed the initialization fragment at the start of the file.
I encoded the videos manually, and I only managed to get 38 portions. Where should the initialization segment be if they have all been integrated into that m4s file?
Make sure the file order is accurate before combining the individual m4s segments into a single file.
E.g.
cat video-0.m4s >> all.m4s cat video-1.m4s >> all.m4s cat video-2.m4s >> all.m4s cat video-3.m4s >> all.m4s cat video-4.m4s >> all.m4s cat video-5.m4s >> all.m4s cat video-6.m4s >> all.m4s cat video-7.m4s >> all.m4s cat video-8.m4s >> all.m4s cat video-9.m4s >> all.m4s cat video-10.m4s >> all.m4sAnd then do all your conversions at once.
ffmpeg -i all.m4s -c copy video.mp4