I'm using ffmpeg to create a video transcoding on demand system that serves the media through the HLS format.

Right now I can transcode the video and serve them as they are encoded, but I can't use the seek function of my players because the playlist (.m3u8) is only generated as the chunks get encoded. I've tried using the -g options, setting it to 90 with chunk lengths of 3s and 30fps (29.7fps to be precise). But the actual chunk lengths fluctuate slightly. I've also tried setting the HLS flag round_durations, but the player doesn't work with it on.

Are the chunk sizes dependent on the scenes it is transcoding, or is it possible, by probing the file, to quickly determine it's pattern and generate a virtual .m3u8 complete at the beginning? Or is there any option to make ffmpeg generate the playlist without actually encoding the files?

This is the command I'm using right now: (multi lines for clarity)

ffmpeg.exe -i input.mkv -c:v libx264 -c:a aac -crf 22 -preset faster
-format hls -sn -hls_time 3 -hls_base_url http://localhost/media/
-hls_list_size 0 -hls_flags split_by_time -hls_playlist_type event -g 90 -r 30 index.m3u8

Unless you're manually forcing a keyframe cadence in some way, the output keyframe placement can't be guessed without passing the frames through the encoder.

You can force more precise keyframe placement by dropping the -g and adding -force_key_frames expr:gte(t,n_forced*3)

  • That seems to work, thanks! Although there is a small quirk that I don't know can be fixed. Every chunk's duration is still 3.003200 rather than plain 3. How can I find the reason for that, and will it be the same for every video? – Pedro M. Silva Oct 3 '17 at 18:28
  • Not very familiar with the internals of the HLS muxer, but it's likely due to the audio. Audio frame duration is not the same as the video, the video governs when the segment stops adding frames but the last audio frame duration crosses the limit <-- my guess. – Gyan Oct 3 '17 at 19:12
  • Can I know what is the duration of each audio frame, by probing the original file, so I can predict by how much the last frame is gonna overflow? Anyway, thanks, your help has saved me a lot of work. ;) – Pedro M. Silva Oct 3 '17 at 19:16
  • Audio frame duration = 1024 / audio sampling rate sec – Gyan Oct 3 '17 at 19:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.