Use FFMPEG to blend streaming overlay onto second stream

2.9k views Asked by At

I'm trying to build a form of monitoring that can be superimposed onto a live stream.

Monitor Overlay

ffmpeg -i rtmp://localhost/pool/livestream -filter_complex \
  "nullsrc=1024x576[1:v]; \
  [0:a]showvolume=v=0:o=1:t=0:f=0.1,drawbox=x=ih-40:y=0:w=40:h=ih[volume]; \
  [1:v]drawtext=x=(main_w/2)-(text_w/2):y=text_h:fontsize=30:fontcolor=white:borderw=1:text='Stream Label',scale=-1:-1[label]; \
  [label][volume]overlay=x=main_w-40:y=0[output]" \
  -map "[output]" -f flv rtmp://localhost/pool/livestream_overlay

What I would like to accomplish is that this stream be superimposed onto the original stream and pushed to a third RTMP endpoint, like this:

ffmpeg -i rtmp://localhost/pool/livestream -i rtmp://localhost/pool/livestream_overlay \
  -filter_complex "[0:v][1:v]overlay=shortest=1[output]" \
  -f flv rtmp://localhost/pool/livestream_monitor

While the workflow seems to be working, the overlay is not blending (subtracted?) onto the original video:

Actual output

Actual output

Expected output

Expected output

Note: codec options have been removed for brevity's sake.

2

There are 2 answers

2
Gyan On BEST ANSWER

Use

ffmpeg -i rtmp://localhost/pool/livestream -filter_complex \
"[0:a]showvolume=v=0:o=1:t=0:f=0.1,drawbox=x=iw-40:y=0:w=40:h=ih[volume]; \
 [0:v]drawtext=x=(W-tw)/2:y=th:fontsize=30:fontcolor=white:borderw=1:text='Stream Label'[label]; \
 [label][volume]overlay=x=W-40:y=0[output]" \
-map "[output]" -map 0:a -f flv rtmp://localhost/pool/livestream_monitor

None of the usual FLV codecs support alpha, so in the 2nd command, you would have to perform chroma keying, which is usually subpar in ffmpeg. So, you can do it in the same command.

1
Louwrens Benade On

So after playing around all day, I stumbled across the answer purely by accident.

Simply change nullsrc=1024x576[1:v] to pad=1024:576:0:0:black[1:v]. The black background disappears, and the rest remain.

Source: A memory of a documentary long ago about tricks used in broadcasting, talking about how white text on a black background superimposed on film made the black transparent. Hopefully, there's a film buff here that can explain it better