Fake a webcam using a video loopback device?

All we need is an easy explanation of the problem, so here it is.

I want to use a looped video clip as my webcam source in a Google+ Hangout. I installed v4l2loopback and created a video loopback device. But I haven’t figured out how to write the video frames to the device. The given example is:

gst-launch videotestsrc ! v4l2sink device=/dev/video1

So after reading the manual page for gst-launch and trying to understand what was going on, I made my feeble attempt:

sudo gst-launch-0.10 filesrc location=/home/briankb/Videos/darthvaderdancing.mp4 ! v4l2sink device=/dev/video0

Which resulted in:

Setting pipeline to PAUSED ...
libv4l2: error getting pixformat: Invalid argument
Pipeline is PREROLLING ...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...

(gst-launch-0.10:12622): GStreamer-CRITICAL **: gst_caps_get_structure: assertion `GST_IS_CAPS (caps)' failed
New clock: GstSystemClock

(gst-launch-0.10:12622): GStreamer-CRITICAL **: gst_structure_get_name: assertion `structure != NULL' failed
Caught SIGSEGV accessing address (nil)
#0  0x00007fc1890f0b03 in poll () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007fc18962bff6 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#2  0x00007fc18962c45a in g_main_loop_run ()
#3  0x00007fc189b5c4cb in gst_bus_poll ()
#4  0x0000000000404587 in ?? ()
#5  0x0000000000403c34 in ?? ()
#6  0x00007fc18902b76d in __libc_start_main ()
#7  0x00000000004043bd in ?? ()
#8  0x00007fff88253ed8 in ?? ()
#9  0x000000000000001c in ?? ()
#10 0x0000000000000006 in ?? ()
#11 0x00007fff882548c4 in ?? ()
#12 0x00007fff882548d4 in ?? ()
#13 0x00007fff882548dc in ?? ()
#14 0x00007fff88254904 in ?? ()
#15 0x00007fff88254906 in ?? ()
#16 0x00007fff8825490f in ?? ()
#17 0x0000000000000000 in ?? ()
Spinning.  Please run 'gdb gst-launch 12622' to continue debugging, Ctrl-C to quit, or Ctrl-\ to dump core.

The clip I’m trying to use an h264 video at 29.97 fps, 320×240, and in an mp4 container (it’s from Youtube).

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The gst-launch manpages tells us that we need to decode the video first, and the easiest way to do so is like this:

gst-launch-0.10 filesrc location=[location] ! decodebin ! v4l2sink device=/dev/video0

gst-launch does not need to be run as root, and [location] can be relative too!

Method 2

Just use v4l2loopback with mplayer.

  1. Download it,
  2. compile it (make and su -c 'make install'),
  3. load the module with su -c 'modprobe v4l2loopback',
  4. then change one line in the file examples/yuv4mpeg_to_v4l2.c of the v4l2loopback source folder from

    v.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
    

    to

    v.fmt.pix.pixelformat = V4L2_PIX_FMT_YVU420;
    
  5. and do make in this folder.

  6. Then run it from the examples directory like this:

    mkfifo /tmp/pipe  # only needed once, as long as you do not delete the file /tmp/pipe
    ./yuv4mpeg_to_v4l2 < /tmp/pipe &
    mplayer movie.mp4 -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe
    

    where you replace movie.mp4 with the name of your video file. And replace /dev/video0 with your loopback device.

MPlayer is able to play any webstreams, all kind of video files and so on. I just tested it with a file from http://www.tagesschau.de which is a german news site.

TS=$(wget 'http://www.tagesschau.de'$(wget http://www.tagesschau.de -q -O - | grep 'Letzte Sendung' | sed -e 's%.*href="%%' -e 's%" rel="nofollow noreferrer noopener".*%%') -q -O - | grep '\.webm\.webm' | sed -e 's%.*href="%%' -e 's%\.webm\.webm" rel="nofollow noreferrer noopener".*%.webm.webm%')
./yuv4mpeg_to_v4l2 < /tmp/pipe &
mplayer $TS -vf scale=480:360 -vo yuv4mpeg:file=/tmp/pipe

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply