Use webcam in two applications under Linux simultaneously using v4l2loopback

Submitted by Tim on 20 Aug 2013 - 13:17

For a recent project we wanted to be able to access the webcam trough Adobe Flash but at the same time record the stream as well with VLC. To do this I had to split the video from /dev/video0 to two virtual webcam devices. I used the v4l2loopback and ffmpeg to archieve this. First of all make sure you've installed ffmpeg apt-get install ffmpeg Then load the v4l2loopback to enable two loopback devices modprobe v4l2loopback devices=2 And with the following line I duplicated the webcam. ffmpeg -f video4linux2 -s 800x600 -i /dev/video0 -codec copy -f v4l2 /dev/video1 \ -codec copy -f v4l2 /dev/video2 What it does is use the input of /dev/video and copy the stream to /dev/video1 and /dev/video2 Now you can select /dev/video1 in Flash and /dev/video2 in VLC or any other application you would like to use the webcam at same time. The line I used to record the webcam commandline in VLC is: cvlc v4l2://dev/video2 :v4l2-standard=ALL :input-slave=alsa://hw:1,0 :v4l2-chroma= \ :v4l2-input=0 :v4l2-audio-input=-1 :v4l2-width=800 :v4l2-height=600 \ :v4l2-aspect-ratio=16\:9 :v4l2-fps=0 :v4l2-use-libv4l2 :v4l2-tuner=0 \ :v4l2-tuner-frequency=-1 :v4l2-tuner-audio-mode=1 :no-v4l2-controls-reset \ :v4l2-brightness=-1 :v4l2-brightness-auto=-1 :v4l2-contrast=-1 \ :v4l2-saturation=-1 :v4l2-hue=-1 :v4l2-hue-auto=-1 :v4l2-white-balance-temperature=-1 \ :v4l2-auto-white-balance=-1 :v4l2-red-balance=-1 :v4l2-blue-balance=-1 :v4l2-gamma=-1 \ :v4l2-autogain=-1 :v4l2-gain=-1 :v4l2-sharpness=-1 :v4l2-chroma-gain=-1 \ :v4l2-chroma-gain-auto=-1 :v4l2-power-line-frequency=-1 :v4l2-backlight-compensation=-1 \ :v4l2-band-stop-filter=-1 :no-v4l2-hflip :no-v4l2-vflip :v4l2-rotate=-1 \ :v4l2-color-killer=-1 :v4l2-color-effect=-1 :v4l2-audio-volume=-1 \ :v4l2-audio-balance=-1 :no-v4l2-audio-mute :v4l2-audio-bass=-1 \ :v4l2-audio-treble=-1 :no-v4l2-audio-loudness :v4l2-set-ctrls= \ :live-caching=300 \ --sout="#transcode{vcodec=h264,vb=750,acodec=mp4a,ab=96,channels=2,samplerate=44100}:standard{access=file,dst="OUTPUTFILE.mp4"}"');


user picture>
- 23 Sep 2013 - 18:10

running the ffmpeg command is giving me the following error: Unrecognized option 'codec' Failed to set value 'copy' for option 'codec'
user picture>
- 08 Oct 2013 - 15:46

Robert, what version of ffmpeg do you have? If I remember correctly for older versions you can use -vcodec instead of -codec
user picture>
- 02 Mar 2014 - 04:50

The correct command is -vcodec for ffmpeg. I get the error: "Requested output format 'v4l2' is not a suitable output format"
user picture>
- 15 Mar 2014 - 22:17

Tim, do you know if there is any way to get mjpeg sent to the loopback device? Just spent some time compiling ffmpeg on the raspberry Pi and come to find out it is having problems sending mjpeg. I used the command ffmpeg -f video4linux2 -input_format mjpeg -i /dev/video0 -codec copy -f v4l2 /dev/video1 and it errored stating that V4L2 output device supports only a single raw video stream. Any help would be appreciated.
user picture>
- 27 Nov 2015 - 00:17

Upadate November 2015: With the following line I duplicated the webcam. ffmpeg -f v4l2 -i /dev/video0 -f v4l2 /dev/video1 -f v4l2 /dev/video2
user picture>
- 20 Jan 2016 - 15:34

This works on my Laptop, but I get this message on my Desktop: Requested output format 'v4l2' is not a suitable output format Any Ideas why?

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.