How to Output fMP4 Segments in FFmpeg HLS

This article explains how to configure the FFmpeg HLS (HTTP Live Streaming) muxer to generate fragmented MP4 (fMP4) segments instead of the default MPEG-2 Transport Stream (TS) format. You will learn the specific command-line options required to switch the segment type, define initialization files, and structure a complete FFmpeg command for modern HLS delivery.

By default, FFmpeg's HLS muxer outputs media segments in the TS format (.ts). To output fragmented MP4 (.m4s) segments, you must use the -hls_segment_type option and set its value to fmp4.

The Core Configuration Option

The primary setting required to switch the muxer to fMP4 is:

-hls_segment_type fmp4

When this option is enabled, FFmpeg will generate an initialization segment (containing the track metadata) and a sequence of media fragment files containing the actual video and audio data.

Essential Parameters for fMP4 HLS

When configuring fMP4 output, you should also define how the initialization file and the individual fragments are named:

  • -hls_fmp4_init_filename: Sets the filename for the initialization segment. This file is required by players to initialize the demuxer before playing the fragments. The default is init.mp4.
  • -hls_segment_filename: Defines the naming pattern for the subsequent fragmented data files (typically using the .m4s extension).
  • -hls_time: Sets the target segment duration in seconds.

Complete Command Example

Below is a complete command that transcodes an input file into HLS-compatible H.264 video and AAC audio, segmenting it into 6-second fMP4 fragments:

ffmpeg -i input.mp4 \
  -c:v libx264 -c:a aac \
  -f hls \
  -hls_time 6 \
  -hls_playlist_type vod \
  -hls_segment_type fmp4 \
  -hls_fmp4_init_filename "init.mp4" \
  -hls_segment_filename "segment_%03d.m4s" \
  playlist.m3u8

Explanation of the Command

  • -f hls: Forces the output format to be HLS.
  • -hls_playlist_type vod: Creates a static VOD playlist containing all segments from start to finish.
  • -hls_segment_type fmp4: Instructs FFmpeg to use fragmented MP4 instead of TS.
  • -hls_fmp4_init_filename "init.mp4": Creates the critical initialization file named init.mp4 in the output directory.
  • -hls_segment_filename "segment_%03d.m4s": Names the resulting fragments sequentially (e.g., segment_000.m4s, segment_001.m4s).
  • playlist.m3u8: The main index file that players load to locate the initialization file and the media fragments.