28

I want to send video to webcam device on Ubuntu which is loaded on /dev/video0

I've already seen this command that send desktop to it but is there any way to send video to it?

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

I should mention that i specifically want to use ffmpeg command.

dsddd
  • 383
  • 1
  • 3
  • 4
  • It's my understanding that a webcam is an video input device so I would say no. What webcam do you have that supports receiving video? – Elder Geek Feb 08 '17 at 19:12
  • @ElderGeek there is no webcam.it is virtual one which need to have video on it. – dsddd Feb 08 '17 at 19:15
  • HAve you tried using a pipe? – Elder Geek Feb 08 '17 at 19:25
  • Information is available [here](https://www.ffmpeg.org/ffmpeg-devices.html#Output-Devices) – Elder Geek Feb 08 '17 at 19:29
  • @ElderGeek yes and i face the error "pipe broken". – dsddd Feb 08 '17 at 19:29
  • @ElderGeek i saw that information, it is confusing for me and i think i need some ffmpeg expert to help me for the command. – dsddd Feb 08 '17 at 19:31
  • 2
    Possible duplicate of [Fake a webcam using a video loopback device?](http://askubuntu.com/questions/139239/fake-a-webcam-using-a-video-loopback-device) (although not necessarily using `ffmpeg`). – llogan Feb 08 '17 at 19:39
  • Based on a quick review of the link I provided you it appears that you would have to compile a configured ffmpeg build from source to enable the specific output devices that you intend to use as outlined in the section I linked you to. I understand that you want to accomplish this with ffmpeg, however you might wish to ease your requirements in order to obtain an answer that is within your capabilities. – Elder Geek Feb 08 '17 at 19:40

1 Answers1

42

You can do this with v4l2loopback. First you need to install it:

Install v4l2loopback

Method 1: Install v4l2loopback from the repository

sudo apt install v4l2loopback-dkms
sudo modprobe v4l2loopback

This is easy but older versions of v4l2loopback have some known bugs, so consider compiling it instead if you encounter any issues.

Method 2: Compile v4l2loopback

If it's not in the repository for your Ubuntu version, or you want the latest version, you can compile it:

sudo apt-get install build-essential checkinstall
wget https://github.com/umlaeute/v4l2loopback/archive/main.zip
unzip main.zip
cd v4l2loopback-main
make
sudo checkinstall --pkgname=v4l2loopback --pkgversion="$(date +%Y%m%d%H%M)-git" --default
sudo modprobe v4l2loopback

Uninstalling

If you want to remove the package you compiled:

sudo apt-get remove v4l2loopback

Examples

Note that the actual video number may vary depending if an existing device is already using /dev/video0. Check output of ls /dev/video* or v4l2-ctl --list-devices.

Desktop to virtual camera

Now run ffmpeg. Example for desktop using x11grab:

ffmpeg -f x11grab -framerate 15 -video_size 1280x720 -i :0.0 -f v4l2 /dev/video0

Video file (MP4) to virtual camera

ffmpeg -re -i input.mp4 -map 0:v -f v4l2 /dev/video0

Image to virtual camera

ffmpeg -re -loop 1 -i input.jpg -vf format=yuv420p -f v4l2 /dev/video0

Webcam → ffmpeg → Virtual webcam

Add text

With drawtext filter:

ffmpeg -f v4l2 -i /dev/video0 -vf "drawtext=text='Hello World':fontsize=12:fontcolor=white:font=Arial:x=w-tw-10:y=h-th-10,format=yuv420p" -f v4l2 /dev/video1

See How to position drawtext text?

Greenscreen / chroma key / replace background

Using chromakey, overlay, and format filters:

ffmpeg -re -i background.jpg -f v4l2 -i /dev/video0 -filter_complex "[1]chromakey=color=#326964:similarity=0.07:blend=0.02[fg];[0][fg]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2:format=auto,format=yuv420p" -f v4l2 /dev/video1

See How to position overlay?

Preview with ffplay

ffplay /dev/video0

Common errors

  • Unable to open V4L2 device '/dev/video0'
  • Failed to open /dev/video0: No such file or directory
  • Unknown V4L2 pixel format equivalent for yuvj422p

See this answer for solutions.

llogan
  • 11,518
  • 43
  • 54
  • where can i specify my video ? – dsddd Feb 08 '17 at 21:08
  • @dsddd Example updated. – llogan Feb 08 '17 at 21:13
  • 5
    I installed the package, but I'm stuck with an error on both examples, either the x11grab or the the mp4 video give me the same `[v4l2 @ 0x55ebc0cfdc40] 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! ` Is there something else I should do? some dependencies I missed? – Sabbin Jan 29 '19 at 16:43
  • @Sabbin Please show your command and provide a link to a pastebin of the log. – llogan Jan 30 '19 at 22:27
  • 1
    @llogan following your example above, I have this command `ffmpeg -re -i 1.mp4 -map 0:v -f v4l2 /dev/video0` https://pastebin.com/9eDK0NTN, if I try the command to run for desktop `ffmpeg -f x11grab -framerate 15 -video_size 1280x720 -i :1 -f v4l2 /dev/video0` I get this error https://pastebin.com/xN6uZJR6, also the folder /dev/video0 has rights for write – Sabbin Feb 04 '19 at 07:37
  • @Sabbin I guess it didn't compile/install correctly, but there is now a package available so compiling isn't required anymore. Do this: `sudo modprobe -r v4l2loopback && sudo apt remove v4l2loopback && sudo apt install v4l2loopback-dkms && sudo modprobe v4l2loopback` and try again with `ffmpeg`. – llogan Feb 04 '19 at 18:57
  • @llogan I had the `v4l2loopback-dkms` installed from before. I tried the above command and I still get the same results – Sabbin Feb 04 '19 at 19:02
  • @Sabbin I can't duplicate the issue. Works for me on 18.04 using v4l2loopback-dkms and ffmpeg from repository. – llogan Feb 04 '19 at 19:03
  • @llogan, I will remove everything and try to get it from 0 again, maybe I missed or have done something wrong. Thank you for your assistance! – Sabbin Feb 04 '19 at 19:06
  • 1
    @sabbin try with /dev/video1 – dǝɥɔS ʇoıןןƎ Jul 03 '19 at 10:45
  • Now how do I get the device to show up to e.g. Chrome software? – Michael May 11 '20 at 18:21
  • @Michael Worth asking that as a new question. – llogan May 11 '20 at 18:23
  • @llogan Thanks, [done](https://askubuntu.com/questions/1238346/how-to-get-v4l2-loopback-device-to-be-recognized) – Michael May 11 '20 at 18:33
  • another error I had was that `snap` installed `ffmpeg` didn't have access to video feed. The command `snap connect ffmpeg:camera` fixed it – Petr Doležal Jan 13 '21 at 21:25
  • wayland compatible solution? – Scrooge McDuck Mar 09 '21 at 10:21
  • @ScroogeMcDuck Sorry, but I don't know anything about wayland. – llogan Mar 09 '21 at 20:32
  • maybe I found something here, will try later https://wiki.archlinux.org/index.php/Screen_capture#Wayland – Scrooge McDuck Mar 10 '21 at 10:13