ffmpegvideo-processingv4l2v4l2loopback

ffmpeg: chroma key (greenscreen filter) a short video continuosly loopling into a live video


I know how to chroma key / greenscreen filter a background of a video stream and replace it by an image using v4l2loopback. After running

$ sudo modprobe v4l2loopback

my code is as follows:

$ ffmpeg -i background.jpg -f v4l2 -pix_fmt mjpeg \
    -framerate 30 -video_size 1280x720 -i /dev/video0 \
    -filter_complex \
        "[0:v]scale=1280x720[bg]; \
         [1:v]colorkey=0x00b140:0.5:0.0[ckout];  
         [bg][ckout]overlay" \
    -pix_fmt yuv420p -f v4l2 /dev/video1

Then I can simply choose at whatever video-playing-app I like, the new pseudo webcam as input.

Instead of a still image, I would like to have a short video, looping continuously in the background. I am pretty sure that this is possible, but have no idea how, any hints are greatly appreciated!

Update 1: As requested, here is the full log of the command as suggested in the response

$ ffmpeg -stream_loop 5 -re -i some_gif.gif \
    -f v4l2 -pix_fmt mjpeg -framerate 30 \
    -video_size 1280x720 -i /dev/video0 \
    -filter_complex \
        "[0:v]scale=1280x720[bg]; \ 
         [1:v]colorkey=0x61A051:0.3:0.0[ckout]; \
         [bg][ckout]overlay" \
    -pix_fmt yuv420p -f v4l2 /dev/video1

ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
  configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, gif, from 'some_gif.gif':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: gif, bgra, 640x640, 10 fps, 10 tbr, 100 tbn, 100 tbc
Input #1, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 433330.420280, bitrate: N/A
    Stream #1:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 (gif) -> scale
  Stream #1:0 (mjpeg) -> colorkey
  overlay -> Stream #0:0 (rawvideo)
Press [q] to stop, [?] for help
[swscaler @ 0x563d17c69e80] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0x563d17c69e80] No accelerated colorspace conversion found from yuv422p to argb.
Output #0, v4l2, to '/dev/video1':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1280x720, q=2-31, 110592 kb/s, 10 fps, 10 tbn, 10 tbc (default)
    Metadata:
      encoder         : Lavc57.107.100 rawvideo
Past duration 0.899986 too largeN/A time=00:00:05.10 bitrate=N/A speed=   1x     
frame=   98 fps= 10 q=-0.0 Lsize=N/A time=00:00:09.80 bitrate=N/A dup=0 drop=20 speed=1.03x    
video:132300kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Update 2: After applying an update via

$ sudo add-apt-repository ppa:jonathonf/ffmpeg-4
$ sudo apt-get update
$ sudo apt-get install ffmpeg

I now have the following version installed

ffmpeg version 4.3.1-0york0~18.04 Copyright (c) 2000-2020 the FFmpeg developers

And now the answer works :)


Solution

  • Use -stream_loop to loop and -re for real-time speed since you're streaming (or else it will play super fast):

    $ ffmpeg -stream_loop -1 -re -i input.mp4 -f v4l2 -pixel_format mjpeg \
        -framerate 30 -video_size 1280x720 -i /dev/video0 \
        -filter_complex \
            "[0:v]scale=1280x720[bg]; \
             [1:v]colorkey=0x00b140:0.5:0.0[ckout];  
             [bg][ckout]overlay,format=yuv420p" \
        -f v4l2 /dev/video1
    

    Given that your inputs are YUV consider replacing colorkey (RGB) with chromakey (YUV).