How can I use FFmpeg to either merge segmented M4S files or locate the init file?

I’m a developer working on a platform for streaming videos. I aim to improve the platform’s performance by maximizing the supply of video material. To facilitate streaming, I have a number of video files that have been split into smaller M4S (MPEG-4) chunks.
I have a problem: I must figure out how to either discover the initialization file, which contains crucial data required for the video stream or combine these segmented files for smooth playback.
Can anyone guide me regarding this?

1 Like

You must use the MP4Box tool, which is a part of the GPAC (Multimedia Framework for Multimedia Packaging) project, to combine segmented M4S (MPEG-DASH) files into a single media file or identify the initialization (init) file using FFmpeg. For use with MP4 files that are fragmented, such as those used in MPEG-DASH, MP4Box was created. The procedures to combine segmented M4S files or obtain the init file are as follows:

1. Install GPAC (MP4Box)

Download GPAC (MP4Box) from the official website at if it isn’t already on your computer. Observe the installation guidelines designed for your particular operating system.

2. Merge Segmented M4S Files

Use the MP4Box command to combine segmented M4S files into a single MP4 file. Go to the directory where your M4S segments are located and run the following command:

MP4Box -dash 10000 -frag 1000 -rap -out merged.mp4 segment1.m4s segment2.m4s ... segmentN.m4s

  • -dash 10000: Specifies the segment duration in milliseconds. Adjust this value as necessary.
  • -frag 1000: Determines the fragment duration in milliseconds. Adapt this value to suit your requirements.
  • -rap: Enforces random access points (keyframes) at the beginning of each fragment.
  • -out merged.mp4: Specifies the output file name, which will contain the amalgamated segments.
  • segment1.m4s, segment2.m4s, ..., segmentN.m4s: Replace these with the actual file names of your segmented M4S files.
  • This command will consolidate the segments into a solitary MP4 file named merged.mp4.

3. The Initialization (Init) File can be found here:

  1. The initialization (init) file for MPEG-DASH commonly ends in.mp4. You can use a simple script or command to locate the init file based on the naming convention or by looking at the contents of the segments. The first segment or initialization information for the media streams is often denoted by the init file.
  2. In Linux or macOS, for example, you can use the ls command to display the files and find the init file based on its naming pattern:

ls -1 *.mp4

The init file should be listed among .mp4 files that this program enumerates in the directory.

4. Validate the Merged File or Init File

Once the segments have been combined or the init file has been located, you may use FFmpeg or another multimedia player to confirm the accuracy of the content and functionality of the resulting media file.

This is how you can merge segmented M4S files into an init file using FFmpeg.

For more queries related to this refer to:

1 Like

That sounds great! Now, how about locating the init file?

1 Like

Hey Roman!

Finding the init file is essential for knowing the operation of segmented media material. You should use the following command to use FFmpeg to extract the init file:

ffmpeg -i input.mpd -map 0 -c copy init.mp4

The segments are referenced in the MPD (Media Presentation Description) file that you enter in this command. Without any transcoding, FFmpeg extracts the init file (init.mp4). By doing this, you can be confident that the important initialization data is kept intact and that the media content’s structure is understood.

1 Like

Thanks for the explanation! I now have a clear idea of how to work with segmented M4S files using FFmpeg.

1 Like

You’re Welcome! FFmpeg has the versatility to handle both circumstances efficiently while preserving the quality and integrity of your media files, whether you need to extract the init file for a deeper understanding of your media content or combine segmented files for continuous playback.
Please do not hesitate to ask for more help or if you require any additional queries.