Skip to content

Instantly share code, notes, and snippets.

@matiaspl
Last active November 5, 2023 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matiaspl/26098b31f92efa78deefba919a370a21 to your computer and use it in GitHub Desktop.
Save matiaspl/26098b31f92efa78deefba919a370a21 to your computer and use it in GitHub Desktop.
FFMPEG based live stream delay line for Decklink

What is it for

FFMPEG based delay loop running in RAM for Blackmagic Decklink video cards. It's useful if you need a "profanity protection" buffer, but could also be used as a poormans instant replay.

How does it work

The idea behind this is to use exploit ffmpeg's concat filter as an intermediate buffer. The length of the delay line is set by the -t parameter before anullsrc in the example below ((or d after smptehdbars).

In my case I wanted PAL interlaced input and output. If you want to use different framerate change the format using the aproppriate -format_code parameter.

Until the buffer time is up, SMPTE HD bars and silence is played on the output, and then the picture and sound switches to the start of the recodring buffer.

Usage notice

The filter operates on uncompressed data, so have in mind it's not heavy on the CPU/GPU but at the same time is not viable to use it for tens of minutes because you will eventually run out of RAM which will likely result in OOM error and process termination.

The amount of memory needed depends on the resolution and framerate used. You can roughly estimate the amount that will fit in your RAM knowing that HD-SDI is 1.5 Gb/s (so uses around 190 megabytes) per second and 3G-SDI is 3 Gbps (380 megabytes per second).

./ffmpeg -thread_queue_size 32 -loglevel verbose -f lavfi -i "smptehdbars=s=1920x540:d=10,tinterlace,interlace" -f lavfi -t 1 -i anullsrc=cl=stereo:r=48000 -format_code Hi50 -f decklink -i 'DeckLink SDI (1)' -filter_complex "[0:v][1:a][2:v][2:a]concat=n=2:v=1:a=1[v][a]" -map [v] -map [a] -f decklink -format_code Hi50 -pix_fmt uyvy422 -ac 2 'DeckLink SDI (2)'

The same as above but in two stages. First generate a legit 1080 50i clip:

./ffmpeg -thread_queue_size 16 -t 10 -format_code Hi50 -f decklink -i 'DeckLink SDI (1)' -map 0:v -map 0:a -filter_complex smptehdbars=s=1920x1080:d=10 -flags +ilme+ildct -c:v h264 -c:a opus -strict -2 -top 1 -f matroska i50.mkv

and then play it out as a slate before the delayed source starts playing with accompanying silence:

 ./ffmpeg -thread_queue_size 16 -t 1 -i i50.mkv -f lavfi -t 30 -i anullsrc=cl=stereo:r=48000 -format_code Hi50 -f decklink -i 'DeckLink SDI (1)' -filter_complex "[0:v][1:a][2:v][2:a]concat=n=2:v=1:a=1[v][a]" -map [v] -map [a] -f decklink -format_code Hi50 -pix_fmt uyvy422 -ac 2 'DeckLink SDI (2)'

Interlace filtering

tinterlace=5:flags=vlpf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment