Configure HLS Segment Duration and Playlist Size in FFmpeg
This article provides a quick guide on how to configure HTTP Live Streaming (HLS) segment duration and maximum playlist size using FFmpeg. You will learn the specific command-line flags required to control individual chunk lengths, limit the number of active segments in your M3U8 playlist, and manage disk space during live streaming.
To configure these settings in FFmpeg, you primarily use the
-hls_time and -hls_list_size options. Below is
a practical command example followed by a detailed breakdown of how
these parameters work.
Example FFmpeg Command
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 6 -hls_list_size 5 -hls_flags delete_segments output.m3u8Parameter Breakdown
1. Configuring Segment
Duration (-hls_time)
The -hls_time option sets the target segment duration in
seconds.
- Syntax:
-hls_time <seconds> - Example:
-hls_time 6creates segments that are approximately 6 seconds long. - Important Note: FFmpeg splits segments on video
keyframes (I-frames). If your keyframe interval (GOP size) is too large
or irregular, the segment duration may not be exact. To ensure precise
segment sizes, force a constant keyframe interval using the
-gflag (e.g.,-g 60for a 30fps video ensures a keyframe every 2 seconds).
2. Configuring
Maximum Playlist Size (-hls_list_size)
The -hls_list_size option defines the maximum number of
segment files kept in the .m3u8 playlist file.
- Syntax:
-hls_list_size <number> - Live Streaming: Setting
-hls_list_size 5keeps only the 5 most recent segments in the playlist. As new segments are created, older ones are removed from the list. - Video on Demand (VOD): Set
-hls_list_size 0to keep all segments in the playlist, allowing users to watch the entire video from start to finish.
3. Managing
Old Segments (-hls_flags delete_segments)
When running a live stream with a limited playlist size, older
segment files will be removed from the .m3u8 index, but
they will remain on your disk by default.
To automatically delete expired segment files from your storage and
prevent your disk from filling up, append
-hls_flags delete_segments to your command.