Forcing FFMPEG to honour segment length in HLS Stream Creator (BASH)

HLS Stream Creator uses FFmpeg under the hood in order to create HLS compatible video segments. However, FFmpeg will try to be smart about where it splits segments, so the interval specified on the command line may not always be honoured (depending on where keyframes are located and scenecut believes there's been a scene change).

This can occasionally lead to an annoying situation where segment lengths are significantly longer than expected, which may impact upon delivery

By exporting a couple of flags, however, it's possible to force FFmpeg to segment far closer to (and usually on) the expected interval. Be aware that this can severely impact video quality (which is why it's not forced by default)

Details

Snippet

# Set GOP size to Desired interval (num of frames)
# and Scenecut threshold to 0
FFMPEG_FLAGS='-crf 22 -g [Desired interval] -sc_threshold 0'
export FFMPEG_FLAGS

# Run HLS Stream Creator 
~/repos/HLS-Stream-Creator/HLS-Stream-Creator.sh -i [input file] -s [segment length] -o [output dir]

Usage Example

# Take a 15fps video. We want 1 second segments
# Set GOP size to 15 (1 seconds worth of 15fps video = 15)
# and Scenecut threshold to 0
FFMPEG_FLAGS='-crf 22 -g 15 -sc_threshold 0'
export FFMPEG_FLAGS

# Run HLS Stream Creator
~/repos/HLS-Stream-Creator/HLS-Stream-Creator.sh -i Kazam_screencast_00003.mp4 -s 1 -o test.mp4.hls2

Video