This is a series of articles. Follow the link here to get an overview over all articles.

Previously

on Using FFmpeg as a HLS streaming server (Part 3) – Multiple Bitrates

This is the full command until now:

./ffmpeg -listen 1 -i rtmp://martin-riedl.de/stream01 \
    -preset veryfast -g 25 -sc_threshold 0 \
    -map v:0 -c:v:0 libx264 -b:v:0 2000k \
    -map v:0 -c:v:1 libx264 -b:v:1 6000k \
    -map a:0 -map a:0 -c:a aac -b:a 128k -ac 2 \
    -f hls -hls_time 4 -hls_playlist_type event \
    -master_pl_name master.m3u8 \
    -var_stream_map "v:0,a:0 v:1,a:1" stream_%v.m3u8

It produces an HLS livestream with two different video qualities (video bitrates).

Two different bitrates are well, but more helpful would it be to have the lesser bitrate version in a lower resolution.

Video Resolutions using Complex Filter

To achieve this, we need to duplicate the video input signal. Then we can apply different filter to each stream.

-filter_complex "[v:0]split=2[vtemp001][vout002]"

The command above splits the first video input source [v:0] and splits it into two. The first video feed with the name vtemp001 is the one that we want to scale own. The second vout002 is the full HD version. This one is already finished, so we just passt them out (FFmpeg Documentation).

[vtemp001]scale=w=960:h=540[vout001]

Then we add the text above to the filter separated by a semi-colon. Here the vtemp001 video stream is scaled down to a lesser resolution (FFmpeg Documentation).

Now both new video feeds must be replaced with the original input in our command. The first -map v:0  will be replaced with -map [vout001], the second will be replaced with -map [vout002].

Finally

Easy, right? The new command is now:

./ffmpeg -listen 1 -i rtmp://martin-riedl.de/stream01 \
    -filter_complex "[v:0]split=2[vtemp001][vout002];[vtemp001]scale=w=960:h=540[vout001]" \
    -preset veryfast -g 25 -sc_threshold 0 \
    -map [vout001] -c:v:0 libx264 -b:v:0 2000k \
    -map [vout002] -c:v:1 libx264 -b:v:1 6000k \
    -map a:0 -map a:0 -c:a aac -b:a 128k -ac 2 \
    -f hls -hls_time 4 -hls_playlist_type event \
    -master_pl_name master.m3u8 \
    -var_stream_map "v:0,a:0 v:1,a:1" stream_%v.m3u8