What is the right ffmpeg output format for a v4l2-loopback device?

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

Following the guide in this question I did:

$ sudo modprobe v4l2loopback
$ ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0

The device /dev/video0 shows up as expected but the ffmpeg command fails with following output:

$ ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f v4l2 /dev/video0
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
...
[x11grab @ 0x559767794cc0] Stream #0: not enough frames to estimate rate; consider increasing probesize
....
[v4l2 @ 0x55976779e6c0] ioctl(VIDIOC_G_FMT): Invalid argument
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:0 -- 
Conversion failed!

I already tried to follow the suggestions below the answer to no avail.
I noticed that v4l2 is listed as demux only in ffmpeg -formats. Does that mean I can’t use it for output to /dev/video0? If so, what should I use instead? I tried h264 but that crashes after ~1000 frames with following error message:

$ ffmpeg -f x11grab -r 15 -s 1280x720 -i :0.0+0,0 -vcodec rawvideo -pix_fmt yuv420p -threads 0 -f h264 /dev/video0
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
...
[x11grab @ 0x55da61e29cc0] Stream #0: not enough frames to estimate rate; consider increasing probesize
...
File '/dev/video0' already exists. Overwrite ? [y/N] y
....
Error writing trailer of /dev/video0: Invalid argument
....
Conversion failed!

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

v4l2loopback v0.11.0 has a bug which is now fixed, so there is no need to manually change the source code as shown in the bug report. As of now there is no newer release version with the fix, so upgrade to the master branch which contains the fix.

Installing

Arch Linux

Until a new release version comes out you can use the AUR package v4l2loopback-dkms-git instead of v4l2loopback-dkms.

Debian & Ubuntu

sudo apt-get remove v4l2loopback-dkms
sudo apt-get install build-essential libelf-dev linux-headers-$(uname -r) unzip
wget https://github.com/umlaeute/v4l2loopback/archive/master.zip
unzip master.zip
cd v4l2loopback-master
make
sudo make install
sudo depmod -a
sudo modprobe v4l2loopback

ffmpeg examples

Grab desktop:

ffmpeg -f x11grab -framerate 25 -video_size 1280x720 -i :0.0+0,0 -f v4l2 /dev/video0

Send file:

ffmpeg -re -i input.mp4 -f v4l2 /dev/video0

Playing the video

ffplay

ffplay -f v4l2 /dev/video0

mpv

mpv --demuxer-lavf-format v4l2 /dev/video0

VLC

MediaOpen Capture Device

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