Search code examples
shellvideoffmpegwebcam

Overlay image over video stream using ffmpeg


I am trying to overlay an image over my webcam's video stream (/dev/video0) and send the output to another video stream (/dev/video2). So far, I have adapted a solution from this question to overlay (and scale) an image over an existing video and stream it to the proper output.

ffmpeg -stream_loop -1 -re -i video.mp4 -i image.png \
    -filter_complex "[1][0]scale2ref[i][m];[m][i]overlay[v]" \
    -map "[v]" -f v4l2 /dev/video2

What can I use instead of -stream_loop to take the video from /dev/video0 (a webcam) and overlay the same image on it in real time?


Solution

  • I was able to solve this by removing the -stream_loop parameter and specifying the format of the video stream using -pix_fmt yuv420p.

    ffmpeg -re -i /dev/video0 -i image.png -pix_fmt yuv420p \
        -filter_complex "[1][0]scale2ref[i][m];[m][i]overlay[v]" \
        -map "[v]" -f v4l2 /dev/video2
    

    More refining from llogan's comment leads to the following final command:

    ffmpeg -i /dev/video0 -i image.png -filter_complex \
        "[1][0]scale2ref[i][m];[m][i]overlay=format=auto,format=yuv420p[v]" \
        -map "[v]" -f v4l2 /dev/video2