FFmpeg Segment Muxer to Generate M3U8 Playlist

This article explains how to use the FFmpeg segment muxer to split a video file into HTTP Live Streaming (HLS) segments and generate an M3U8 playlist. You will learn the exact FFmpeg command structure, the key parameters required for segmentation, and how to ensure precise segment splitting by managing keyframes.

The segment muxer in FFmpeg is a highly flexible tool that splits an input stream into temporal chunks (segments) and outputs a list file, such as an M3U8 playlist, to index them.

The Basic Command

To convert an MP4 video into HLS segments using the segment muxer, run the following command in your terminal:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -g 120 -sc_threshold 0 -f segment -segment_time 4 -segment_list playlist.m3u8 -segment_format mpegts output_%03d.ts

Parameter Breakdown

Achieving Precise Segment Lengths (Keyframe Alignment)

FFmpeg can only split a video at a keyframe (I-frame). If there is no keyframe at your designated -segment_time, FFmpeg will wait for the next keyframe, resulting in uneven segment lengths.

To ensure exact 4-second segments, you must force keyframes at regular intervals using the -g (Group of Pictures/GOP) and -sc_threshold flags: