Configure FFmpeg HLS Segment Names Using strftime
This article explains how to configure the segment naming format in
FFmpeg’s HTTP Live Streaming (HLS) muxer using the strftime
library. You will learn how to enable the time-based naming option,
understand the common formatting specifiers, and implement a practical
command-line example to generate uniquely timestamped video
segments.
To use time-based naming for HLS segments in FFmpeg, you must
explicitly enable the strftime option and define a naming
template using standard time format specifiers in the segment filename
option.
Required FFmpeg Flags
To configure this setup, you must use two key parameters in your FFmpeg command:
-strftime 1: This flag enablesstrftimeprocessing for the segment filenames.-hls_segment_filename: This flag defines the template for the output segment files, utilizing time-based placeholders.
Configuration Example
Here is a standard FFmpeg command that transcodes an input file into HLS segments named after the current system date and time:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls -hls_time 10 -strftime 1 -hls_segment_filename "file_%Y-%m-%d_%H-%M-%S.ts" playlist.m3u8Common strftime Format Specifiers
The string provided to -hls_segment_filename can be
customized using standard C library strftime
specifiers:
%Y: Year with century as a decimal number (e.g., 2023).%m: Month as a decimal number (01-12).%d: Day of the month as a decimal number (01-31).%H: Hour using a 24-hour clock (00-23).%M: Minute as a decimal number (00-59).%S: Second as a decimal number (00-59).
Using the command example above, a segment written on October 25,
2023, at 3:45:10 PM will be named:
file_2023-10-25_15-45-10.ts
Important Usage Notes
- Avoid Sequence Conflict: Standard FFmpeg segment
naming uses
%dfor sequential numbering (e.g.,file0.ts,file1.ts). However, instrftime,%drepresents the day of the month. When-strftime 1is active, any%din your filename template will be interpreted as the day, not a sequence number. - Uniqueness: Ensure your time format is granular
enough (usually down to the second using
%S) to prevent subsequent segments generated within the same minute from overwriting each other.