[FFmpeg-user] Generating HLS chunks on demand
Vincent Deconinck
vdeconinck at gmail.com
Wed Jan 25 20:24:24 EET 2023
Hi German,
Thanks for your reply.
> Do not use -ss and -t parameters simultaneously. FFmpeg cannot form an
> output container well in that case.
>
I also think the -ss and -t options are the root cause of my issues, but I
cannot see another way to generate only some segments (or are there
"start/end segment index" options ?)
> If you just need to create HLS segments from some source file you can use
> the "segment" muxer.
> ffmpeg -i source.mp4 -vcodec copy -acodec copy -f ssegment -segment_list
> chunklist.m3u8 -segment_time 4 media_%04d.ts
>
Oh, I didn't know about the "segment" muxer. Thanks
> If you want to create HLS segments of exact length, then you must
> transcode your source file and provide exact frame rate -r and exact GOP -g
> .
>
As you can see from my previous example, I was already reencoding the
streams, with -g 25, but I tried the command you suggested, as follows:
ffmpeg -i source.ts -r 25 -g 25 -c:v libx264 -preset fast -crf 24 -c:a aac
-b:a 128k -f segment -segment_list sample_seg.m3u8 -segment_time 4
sample_seg-%%04d.ts
The result is playable but strangely enough, it has audio drops a bit
before the segment change (every 4 seconds), while the version with the hls
muxer hadn't.
I tried to compare the files generated by the 2 muxers using almost
identical commands, namely:
ffmpeg -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k -r 25 -g 25
-f hls -hls_time 4 -hls_playlist_type vod out_hls.m3u8
ffmpeg -i source.ts -c:v libx264 -b:v 1000k -c:a aac -b:a 128k -r 25 -g 25
-f segment -segment_time 4 -segment_list out_seg.m3u8 out_seg%%d.ts
and indeed, the generated segments are different:
582424 out_hls0.ts
613068 out_seg0.ts
764972 out_hls1.ts
805580 out_seg1.ts
401004 out_hls2.ts
421120 out_seg2.ts
662700 out_hls3.ts
697856 out_seg3.ts
524520 out_hls4.ts
454396 out_seg4.ts
The ffmpeg doc for the segment muxer says the hls muxer "provides a more
specific implementation for HLS segmentation", but it seems to be
completely different. Even the durations of the files are different:
a) with -f hls:
#EXT-X-TARGETDURATION:5
#EXTINF:4.160000,
out_hls0.ts
#EXTINF:4.640000,
out_hls1.ts
#EXTINF:3.320000,
out_hls2.ts
#EXTINF:4.200000,
out_hls3.ts
#EXTINF:4.320000,
out_hls4.ts
b) with -f segment:
#EXT-X-TARGETDURATION:5
#EXTINF:4.240000,
out_seg0.ts
#EXTINF:4.640000,
out_seg1.ts
#EXTINF:3.320000,
out_seg2.ts
#EXTINF:4.200000,
out_seg3.ts
#EXTINF:3.640000,
out_seg4.ts
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 ?
Kind regards,
Vincent
More information about the ffmpeg-user
mailing list