How to Set HLS Segment Type to fMP4 in FFmpeg
This article provides a straightforward guide on how to configure FFmpeg to output HTTP Live Streaming (HLS) using fragmented MP4 (fMP4) segments instead of the traditional MPEG-TS format. You will learn the exact command-line options required, understand the key parameters, and see a practical example to implement this in your video streaming workflows.
By default, the FFmpeg HLS muxer outputs segments as MPEG-TS
(.ts) files. To switch the segment format to fragmented MP4
(.m4s), you must use the -hls_segment_type
option and set its value to fmp4.
The FFmpeg Command
Below is a standard FFmpeg command that converts an input video into an HLS stream with fMP4 segments:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 6 -hls_segment_type fmp4 output.m3u8Parameter Breakdown
-i input.mp4: Specifies the path to your source video file.-c:v libx264: Encodes the video stream using the H.264 codec.-c:a aac: Encodes the audio stream using the AAC codec.-f hls: Forces FFmpeg to use the HLS muxer.-hls_time 6: Sets the target segment duration to 6 seconds.-hls_segment_type fmp4: This is the key setting. It instructs FFmpeg to generate fragmented MP4 segments (.m4sfiles) instead of MPEG-TS files.output.m3u8: The name of the master playlist file generated by FFmpeg.
Why Use fMP4 for HLS?
Using fragmented MP4 segments offers several modern advantages over MPEG-TS:
- Cross-Platform Compatibility: fMP4 allows you to use the exact same media segments for both HLS (for Apple devices) and MPEG-DASH (for Android and web players), eliminating the need to store duplicate video files.
- Lower Storage Costs: Because you only need one set of fMP4 files for multiple streaming protocols, you save significant storage space on your origin servers and Content Delivery Networks (CDNs).
- Modern Codec Support: fMP4 is required if you plan to stream newer video codecs like HEVC (H.265) or AV1 over HLS.