[FFmpeg-user] Generating HLS chunks on demand

Vincent Deconinck vdeconinck at gmail.com
Thu Jan 26 18:42:10 EET 2023


Hi,

OK, I made some progress, so replying to self :

So although I'm forcing keyframes, the durations are actually not 4 seconds
> (the EXT-X-TARGETDURATION field even says 5)
> Is there another way to force all segments to be exactly the same duration
> ?
>

After a bit of reading, the -g option only forces the *maximum* interval
between keyframes, but the scene detection can trigger the creation of
additional keyframes.
E.g. with a -g 25 in a 25fps video, if there is a scene change at frame 12,
the encoder will create a keyframe at 12, and then subsequent keyframes
could be at e.g. 37, 62, etc. , so the last gop would end after the
requested segment length.

The --min-keyint 25 option didn't seem to change anything, so I finally
resorted to using specific x264 options with the following command :
ffmpeg -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k -x264opts
"keyint=25:min-keyint=25:no-scenecut" -f hls -hls_time 4 -hls_playlist_type
vod sample_fixed.m3u8
and then the segments finally are exactly 4 seconds long (and the playback
is OK).
Good.

Now I'm back to my initial problem : Dynamically generating a playable
subset of segments.

But cannot achieve a playable stream (tried -s mpegts, -s segment, -s hls,
to no avail). For example, i ran the following commands :
ffmpeg -ss 0 -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k
-x264opts "keyint=25:min-keyint=25:no-scenecut" -t 4 -f segment
-segment_list sample_segg.m3u8 -segment_time 4 sample0_%%d.ts
ffmpeg -ss 4 -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k
-x264opts "keyint=25:min-keyint=25:no-scenecut" -t 4 -f segment
-segment_list sample_segg.m3u8 -segment_time 4 sample1_%%d.ts
ffmpeg -ss 8 -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k
-x264opts "keyint=25:min-keyint=25:no-scenecut" -t 4 -f segment
-segment_list sample_segg.m3u8 -segment_time 4 sample2_%%d.ts
ffmpeg -ss 12 -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k
-x264opts "keyint=25:min-keyint=25:no-scenecut" -t 4 -f segment
-segment_list sample_segg.m3u8 -segment_time 4 sample3_%%d.ts
ffmpeg -ss 16 -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k
-x264opts "keyint=25:min-keyint=25:no-scenecut" -t 4 -f segment
-segment_list sample_segg.m3u8 -segment_time 4 sample4_%%d.ts
then created by hand a playlist containing the generated segments, but the
playback is choppy and stops after the first segment.

Any idea how to create valid segments ?
Is there another option than -ss and -t to splice the stream ?

Kind regards,

Vincent


More information about the ffmpeg-user mailing list