pythonwebcamgstreamerv4l2

Programatically send images to v4l2-device


I have a program that generates jpgs. I would like to send these images to my virtual webcam when they are generated. I can create a virtual webcam using v4l2loopback and have been looking at gstreamer, but can't quite get it to work.

I've tried gst-launch with multifilesrc, but that seems to only work when I have images already generated. I've also tried setting gst-launch to just send one image to the webcam and then just overwrite the image when a new one is generated. This, unfortunately, causes gstreamer to crash.

Does anyone know how I might be able to do this? I don't have to use gstreamer, but I am looking to do implement this in Linux, ideally in Python.


Solution

  • the following script will monitor a directory and pass every newly added image to gstreamer which will send it to the video-device (/dev/video0, but change at will).

    call with the path to monitor as the sole argument (e.g. ./pics2v4l2 /tmp/pics)

    #!/bin/sh
    
    DIR=$1
    DEVICE=/dev/video0
    
    inotifywait -m "${DIR}" \
    | awk '$2== "CLOSE_WRITE,CLOSE" {print $1"/"$3; fflush()}' \
    | while read f; do
     gst-launch-0.10 uridecodebin uri="file://${f}" \
     ! ffmpegcolorspace \
     ! videoscale \
     ! imagefreeze \
     ! identity error-after=2 \
     ! v4l2sink show-preroll-frame=false device=${DEVICE}
    done