bentasker / HLS-Stream-Creator

Simple Bash Script to take a media file, segment it and create an M3U8 playlist for serving using HLS
BSD 3-Clause "New" or "Revised" License
273 stars 101 forks source link

Accuracy of Segment length (seconds)? #35

Open jaunt opened 4 years ago

jaunt commented 4 years ago

Hello,

Running on a 6 minute 36 second mp4 with -s 3, I got 55 segments, in other words average of 7.2 seconds per segment? Is that expected?

Perhaps there is a lower limit involved?

Also curious if you've had a look at: https://gist.github.com/mrbar42/ae111731906f958b396f30906004b3fa

It seems to do things in a different way. Which way is best do you think?

Thanks for the great work.

bentasker commented 4 years ago

Running on a 6 minute 36 second mp4 with -s 3, I got 55 segments, in other words average of 7.2 seconds per segment? Is that expected?

Yes.

FFMPEG will only be able to split into a new segment where there's a keyframe, so if your input video has one roughly every 7 seconds you'll get a split at that point.

Anecdotally, ffmpeg does sometimes seem to miss the opportunity for a split too. As far as I've been able to work out/repro, this tends to be because scenecut doesn't believe the scene has changed.

It is possible to inject keyframes, and disable scenecut so that the split happens more or less exactly on the interval you've specified. The procedure for that can be found here - https://snippets.bentasker.co.uk/page-1904031648-Forcing-FFMPEG-to-honour-segment-length-in-HLS-Stream-Creator-BASH.html

However, results from doing that can be quite mixed. You'll be increasing the size of your video either way (as keyframes are much larger), but if your injected KF lands on a transition you can sometimes get some unpleasant results (which is why we're not doing it by default).

This is something I played around with a couple of years ago when creating ultra-low latency HLS streams though, so you can get some good results out of it too

Also curious if you've had a look at: https://gist.github.com/mrbar42/ae111731906f958b396f30906004b3fa

Not until this morning no.

It seems primarily concerned with creating vod, which is a common use-case but a fairly small part of what HLS gets used for.

As far as it applies to your original question though, the approach that script is taking is to look at what the keyframe interval is and then ensure KFs are injected at that interval. Because the output interval will mirror the input, it's quite likely the same issue will be reported (at least some of the time), particularly as they've left scenecut enabled.

jaunt commented 4 years ago

Thank you, your explanation was very helpful.

Note, it seems to me this is an excellent tool you've created. I thought your comment in the readme was interesting: "There are numerous tools out there which are far better suited to the task". I consider myself a decent google-searcher and it's been very hard to find tools for this. Even the wikipedia entry on hls only mentions two tools for this sort of thing.

Your tool seems to fit perfectly in my pipeline. A combination of calling ccexractor and srt-to-vtt is all I needed to add at the end, giving me subtitles I can easily add to my video.js based front end. Speaking of that, I had to set FFMPEG_FLAGS to "-sn" using your script or my files would get an error due to the subtitle track.