[FFmpeg-user] Generating HLS chunks on demand
Vincent Deconinck
vdeconinck at gmail.com
Fri Jan 27 16:51:34 EET 2023
Sorry, I got my tests mixed-up in the previous email :-(
The command
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 mpegts
mpegts_fixed-0.ts
creates a TS with *no missing frames*:
mpegts_fixed-0.ts:
IBBBPPBBPBPBPPBPBPPBPBBPP
IPPBBBPBBPBBBPBBBPBPBBBPP
IBBBPBBBPBBBPBBBPBBBPBBBP
IBBPBPBBBPBBBPBBPBBPBBBPP
but yet a playlist including that segment (and subsequent segments created
the same way) is not playable.
So I think the splicing is now correct, but somehow the generated TS is not
compatible with an HLS playback.
For comparison, the first segment produced by 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 hls_fixed.m3u8
has the exact same gop structure:
hls_fixed0.ts:
IBBBPPBBPBPBPPBPBPPBPBBPP
IPPBBBPBBPBBBPBBBPBPBBBPP
IBBBPBBBPBBBPBBBPBBBPBBBP
IBBPBPBBBPBBBPBBPBBPBBBPP
But when included in a m3u8 playlist, it plays flawlessly.
The files themselves are different (different size) and a simple ffprobe on
both returns:
Input #0, mpegts, from 'mpegts_fixed-0.ts':
Duration: 00:00:04.02, start: 1.458667, bitrate: 1178 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B),
yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn
Stream #0:1[0x101](fra): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000
Hz, stereo, fltp, 130 kb/s
Input #0, mpegts, from 'hls_fixed0.ts':
Duration: 00:00:04.02, start: 1.458667, bitrate: 1128 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B),
yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 90k tbn
Stream #0:1[0x101](fra): Audio: aac (LC) ([15][0][0][0] / 0x000F), 48000
Hz, stereo, fltp, 130 kb/s
Apart from the bitrate, I see no difference.
So I believe the difference lies in the hls muxer details, but I have no
idea how to get a single slice using the hls muxer.
Any idea ?
Kind regards,
Vincent
On Fri, Jan 27, 2023 at 1:38 PM Vincent Deconinck <vdeconinck at gmail.com>
wrote:
> Hi all,
>
> I tried analyzing the differences between the ts files generated "all at
> once" (using the hls muxer) and the ones generetad individually using -ss,
> -t and the mpegts muxer.
> I extracted the gop structure of the first ts segment using ffprobe then
> reformated it in IBBP format, and here are the results
> -f hls (works):
> IBBBPPBBPBPBPPBPBPPBPBBPP
> IPPBBBPBBPBBBPBBBPBPBBBPP
> IBBBPBBBPBBBPBBBPBBBPBBBP
> IBBPBPBBBPBBBPBBPBBPBBBPP
> -ss -t -f mpegts (does not work):
> IBBBPPBBPBPBPPBPBPPBPBBPP
> IPPBBBPBBPBBBPBBBPBPBBBPP
> IBBBPBBBPBBBPBBBPBBBPBBBP
> IBBPBPBBBPBBBPBBPBBP
> (and that's it !)
>
> In other words, the structure looks ok but 5 frames seem to be missing
> from the second segment ! No wonder it looks choppy...
> And indeed, I had overlooked that a simple ffprobe on the non-working
> segment shows a duration of 00:00:03.82 while the working one has a
> duration of 00:00:04.02 (don't know where the 0.02 comes from).
>
> So it seems it all boils down to frame accurate splicing indeed.
> Any help in that regard would be greatly appreciated...
>
> KR,
>
> Vincent
>
>
> On Thu, Jan 26, 2023 at 5:42 PM Vincent Deconinck <vdeconinck at gmail.com>
> wrote:
>
>> 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