How to create m3u8 playlist and mpeg-ts chunks with constant duration by using FFMPEG?

Oleksandr picture Oleksandr · Oct 15, 2015 · Viewed 17.4k times · Source

I have mp4 file (Big Buck Bunny):
Duration: 00:09:56.50
Bitrate: 2048 kb/s
Size: 1280x720
fps: 29.97
I've set constant keyframes after 2 second.
I want to prepare this video for HLS.
I use this for generate m3u8 playlist and generate ts chunks:

ffmpeg -i input.mp4 -hls_time 2 out.m3u8

But unfortunately I don't understand how it works.
I've thought this command generates 298 chunks of 2 seconds but it generates only 152 chunks with different lengths (3 - 9 seconds).
But the most strange thing it have created m3u8 file with only 5 links to files.

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:9
#EXT-X-MEDIA-SEQUENCE:148
#EXTINF:8.341667,
out148.ts
#EXTINF:7.841167,
out149.ts
#EXTINF:0.967633,
out150.ts
#EXTINF:8.341667,
out151.ts
#EXTINF:7.140467,
out152.ts
#EXT-X-ENDLIST

I've thought m3u8 file have to includes all part of videos. Can somebody explain me how to create 298 chunks each of 2 seconds and fill m3u8 file properly?

Answer

aergistal picture aergistal · Oct 15, 2015

To force a keyframe every 2 seconds you can specify the GOP size using -g:

ffmpeg -i input.mp4 -g 60 -hls_time 2 out.m3u8

Where 29.97 fps * 2s ~= 60 frames, meaning a keyframe each 60 frames.

Otherwise it will wait to split on a keyframe and the minimum duration will vary.

To keep all segments add -hls_list_size 0, otherwise it keeps just the default value of 5.