obs-virtual-cam icon indicating copy to clipboard operation
obs-virtual-cam copied to clipboard

Hello CatxFish... Any way to install OBS-virtual-cam on ubuntu?

Open pixel-one opened this issue 6 years ago • 70 comments

Hello. Hope youre doing fine. I was reading your other post that you said obs works with genymotion. then i realized i can't even stream. I mean it was complicated. Then i found out that youre talking about obs-virtualcam but i have obs studio. are these 2 different softwares? Sorry im alittle noobish.
If not so can i install obs-virtual cam on linux? Or can i do teh same with on obs studio? I have obs studio but i dunno how to stream internal. i want to use it as genymotion camera . but obs studio wants to conenct to a server and wants server keys and such. so i cant start streaming even to test to see if genymotion detects teh cam or not. Could you please help me? Thanks alot

pixel-one avatar Nov 04 '17 17:11 pixel-one

Hi , OBS-Virtualcam is a plugin of OBS-Studio on Windows, which means you have to install OBS-Studio first ,then install this project to get addition features , and it's only on windows. You can check in obs-studio plugins.

And since it is a lack of cross-platform solutions to simulate a device , the project dose not support linux or mac version now. But some simulators on Windows would transform the windows camera device to an inner camera ,like Genymotion.

I am sorry I can't provide a workable solution for you , but I do get an information about someone use v4l2loopback to do a virtual device trick on linux. I don't know how exactly this works, but maybe you can search in this way.

CatxFish avatar Nov 05 '17 06:11 CatxFish

@pixel-one Here's a proof of concept using v4l2loopback on Ubuntu to get output from OBS into a virtual device. The performance is terrible, because of the fifo, and I was only able to detect the device in Firefox, not Chrome, due to some issue with "capabilities" metadata. For better performance, you might try setting up a local RTMP proxy server with nginx; that's the best I could come up with, unless it's possible to write directly to some virtual file created by v4l.

Cheers!

# Install packages
sudo apt install v4l-utils v4l2loopback v4l2loopback-utils ffmpeg obs-studio

# Create loopback device
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
# Note: Chrome doesn't detect this device; exclusive_caps is supposed to fix, but doesn't

# Create fifo to channel obs output to ffmpeg
mkfifo /tmp/pipe

# Open OBS, go to Settings->Output->Recording, set Type to "Custom Output (FFmpeg)", Container Format to "flv"; Apply and Start Recording
obs

# Send obs output to the virtual device
ffmpeg -re -f live_flv -i "/tmp/pipe" -f v4l2 /dev/video1

paulerickson avatar Nov 17 '17 20:11 paulerickson

Thank you paulerickson . could you test it on Genymotion?
i get this error upon installing... Reading package lists... Done Building dependency tree
Reading state information... Done E: Unable to locate package v4l2loopback

pixel-one avatar Nov 17 '17 22:11 pixel-one

Sorry, I don't have that.

I originally did this on an older Ubuntu, so I think you can use v4l2loopback-dkms now. Also, I realized you can write to UDP, so

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms
sudo rmmod v4l2loopback #if you need to reload it for some reason
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1

And then in OBS "record to file" udp://localhost:12345 with flv. Other containers work too, some with worse latency, but I have had no luck with rawvideo.

paulerickson avatar Nov 17 '17 23:11 paulerickson

Hello again, Thanks for teh help. I entered the commands and it stopped at third... Please have a look... Thanks again for helping, I appreciate this really none@none-Latitude:~$ sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms [sudo] password for none: Reading package lists... Done Building dependency tree Reading state information... Done v4l2loopback-dkms is already the newest version. obs-studio is already the newest version. The following extra packages will be installed: libv4l2rds0 The following NEW packages will be installed: libv4l2rds0 v4l-utils v4l2loopback-utils 0 upgraded, 3 newly installed, 0 to remove and 17 not upgraded. Need to get 150 kB of archives. After this operation, 609 kB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libv4l2rds0 amd64 1.0.1-1 [15.9 kB] Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe v4l-utils amd64 1.0.1-1 [123 kB] Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe v4l2loopback-utils all 0.8.0-1 [11.0 kB] Fetched 150 kB in 5s (29.7 kB/s) Selecting previously unselected package libv4l2rds0:amd64. (Reading database ... 1514548 files and directories currently installed.) Preparing to unpack .../libv4l2rds0_1.0.1-1_amd64.deb ... Unpacking libv4l2rds0:amd64 (1.0.1-1) ... Selecting previously unselected package v4l-utils. Preparing to unpack .../v4l-utils_1.0.1-1_amd64.deb ... Unpacking v4l-utils (1.0.1-1) ... Selecting previously unselected package v4l2loopback-utils. Preparing to unpack .../v4l2loopback-utils_0.8.0-1_all.deb ... Unpacking v4l2loopback-utils (0.8.0-1) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Setting up libv4l2rds0:amd64 (1.0.1-1) ... Setting up v4l-utils (1.0.1-1) ... Setting up v4l2loopback-utils (0.8.0-1) ... Processing triggers for libc-bin (2.19-0ubuntu6.13) ... none@none-Latitude:~$ sudo rmmod v4l2loopback rmmod: ERROR: Module v4l2loopback is not currently loaded none@none-Latitude:~$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 modprobe: FATAL: Module v4l2loopback not found. none@none-Latitude:~$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 modprobe: FATAL: Module v4l2loopback not found. none@none-Latitude:~$ sudo rmmod v4l2loopback rmmod: ERROR: Module v4l2loopback is not currently loaded none@none-Latitude:~$

pixel-one avatar Nov 17 '17 23:11 pixel-one

k, I didn't mean to confuse things — just skip the rmmod command, but that is how you would unload the module if you needed to recreate the device. If you can get this device to show up in Firefox (for instance, test at https://appr.tc/) but not in Genymotion, then I bet you'll need to figure out how those capability flags work (I have no clue).

paulerickson avatar Nov 18 '17 00:11 paulerickson

Thanks again Paul. I loaded up OBS after running this command.. ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1 Then tried to follow your post. and i dont understand this part .... >>>>

And then in OBS "record to file" udp://localhost:12345 with flv. Other containers work too, some with worse latency, but I have had no luck with rawvideo.

<<<< Could you please explain again what do i do after i open OBS. i dont see record to file" in obs recording settong. .
Also i just entered your secong set of commands. I didnt do this one ..

mkfifo /tmp/pipe

. Could you please once more type all the steps in one post? I got confused which post to follow. Thanks much

pixel-one avatar Nov 18 '17 00:11 pixel-one

Forget about the fifo. You already did

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms
sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video1

So now go into OBS and click Settings. Click on the Output section, and then choose Advanced from the dropdown. Go to the Recording tab, change the Type to "Custom Output (ffmpeg)", and the FFmpeg Output Type to "Save to URL". In the "File path or URL" input, type "udp://localhost:12345" and choose "flv" from the Container format dropdown. Hit OK and then from the main screen try "Start Recording" (not "Start Streaming").

paulerickson avatar Nov 18 '17 00:11 paulerickson

Thank you again for your time. Here is 2 problem i get... First when i enter the command""" sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1 """" I get modprobe: FATAL: Module v4l2loopback not found. And i ignore it so i contonue doing the next command and then open the OBS and i do the changes in setting too then when i click on start recording OBS hangs and closes fast... Here is teh logs in the terminal... ( Because i should open obs with this command " LIBGL_ALWAYS_SOFTWARE=1 obs"" otherwise it wont open. so anyway...
I get this error when ops crashes..
none@none-Latitude:~$ LIBGL_ALWAYS_SOFTWARE=1 obs Attempted path: share/obs/obs-studio/locale/en-US.ini Attempted path: /usr/share/obs/obs-studio/locale/en-US.ini Attempted path: share/obs/obs-studio/locale.ini Attempted path: /usr/share/obs/obs-studio/locale.ini Attempted path: share/obs/obs-studio/themes/Default.qss Attempted path: /usr/share/obs/obs-studio/themes/Default.qss Attempted path: share/obs/obs-studio/license/gplv2.txt Attempted path: /usr/share/obs/obs-studio/license/gplv2.txt info: CPU Name: Intel(R) Core(TM) i5 CPU M 560 @ 2.67GHz info: CPU Speed: 1599.000MHz info: Physical Cores: 2, Logical Cores: 4 info: Physical Memory: 7781MB Total, 357MB Free info: Kernel Version: Linux 4.4.0-98-generic info: Distribution: "Ubuntu" "14.04" info: Portable mode: false QMetaObject::connectSlotsByName: No matching signal for on_advAudioProps_clicked() QMetaObject::connectSlotsByName: No matching signal for on_advAudioProps_destroyed() info: OBS 20.1.0 (linux) info: --------------------------------- info: --------------------------------- info: audio settings reset: samples per sec: 44100 speakers: 2 info: --------------------------------- info: Initializing OpenGL... info: OpenGL version: 3.3 (Core Profile) Mesa 11.2.0 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 info: --------------------------------- info: video settings reset: base resolution: 680x420 output resolution: 680x420 downscale filter: Bicubic fps: 30/1 format: NV12 info: Audio monitoring device: name: Default id: default info: --------------------------------- libDeckLinkAPI.so: cannot open shared object file: No such file or directory info: No blackmagic support error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: VLC found, VLC video source enabled info: --------------------------------- info: Loaded Modules: info: vlc-video.so info: text-freetype2.so info: rtmp-services.so info: obs-x264.so info: obs-transitions.so info: obs-outputs.so info: obs-libfdk.so info: obs-filters.so info: obs-ffmpeg.so info: linux-v4l2.so info: linux-pulseaudio.so info: linux-jack.so info: linux-decklink.so info: linux-capture.so info: linux-alsa.so info: image-source.so info: frontend-tools.so info: --------------------------------- info: ==== Startup complete =============================================== info: All scene data cleared info: ------------------------------------------------ error: glGetIntegerv(GL_MAX_TEXTURE_ANISOTROPY_MAX) failed, glGetError returned 0x500 error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: pulse-input: Server name: 'pulseaudio 4.0' error: pulse-input: An error occurred while getting the source info! info: pulse-input: Server name: 'pulseaudio 4.0' error: pulse-input: An error occurred while getting the source info! info: [Media Source 'Media Source']: settings: input: /home/none/Videos/WPS Pixie Dust Attack Manual RalinkBroadcom (OLDER).mp4 input_format: (null) is_looping: no is_hw_decoding: yes is_clear_on_media_end: yes restart_on_activate: yes close_when_inactive: no info: Switched to scene 'Scene' info: ------------------------------------------------ info: Loaded scenes: info: - scene 'Scene': info: - source: 'Media Source' (ffmpeg_source) info: - scene 'Scene 2': info: ------------------------------------------------ info: adding 92 milliseconds of audio buffering, total audio buffering is now 92 milliseconds error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed error: glTexParameteri failed, glGetError returned 0x500 error: device_load_texture (GL) failed info: ==== Recording Start =============================================== Segmentation fault (core dumped) none@none-Latitude:~$

pixel-one avatar Nov 18 '17 00:11 pixel-one

Since I couldn't get it to work with the webcam, I also tried it without setting up my webcam to make things simpler.

I have File path or URL set to "udp://localhost:12345" and Container Format as flv. I do these steps:

[mikko@localhost lvdata]$ sudo rmmod v4l2loopback
[mikko@localhost lvdata]$ sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1
[mikko@localhost lvdata]$ ffmpeg -re -f live_flv -i udp://localhost:12345 -f v4l2 /dev/video0
ffmpeg version 3.4.2 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-shared --enable-version3 --enable-omx
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100

Then I start recording in OBS.

However, none of ffplay, Cheese or Chromium seem to recognize the stream.

[mikko@localhost lvdata]$ ffplay /dev/video0
ffplay version 3.4.2 Copyright (c) 2003-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-shared --enable-version3 --enable-omx
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
[video4linux2,v4l2 @ 0x7f0144000b80] Not a video capture device.
/dev/video0: No such device
    nan    :  0.000 fd=   0 aq=    0KB vq=    0KB sq=    0B f=0/0

Have you got any ideas on this?

MikkoMMM avatar May 01 '18 12:05 MikkoMMM

This solution used to work but v4l2 updated and now I'm getting this error

[v4l2 @ 0x55d4d1400fc0] 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!```

Any ideas?

notfood avatar May 20 '18 23:05 notfood

It was due to this issue. Ignore me.

https://github.com/umlaeute/v4l2loopback/issues/172

notfood avatar Jun 03 '18 17:06 notfood

I make a experimental repository for this feature.

CatxFish avatar Jul 25 '18 17:07 CatxFish

I get this crash

Stream mapping:
  Stream #0:1 -> #0:0 (flv1 (flv) -> rawvideo (native))
Press [q] to stop, [?] for help
[v4l2 @ 0x563fbb7b3cc0] 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 --

grigio avatar Feb 07 '19 11:02 grigio

@CatxFish Is it possible to make use of https://github.com/webcamoid/akvcam , which is used as webcamoid's virtual webcam?

tsankuanglee avatar Apr 21 '19 17:04 tsankuanglee

@tsankuanglee Maybe it will work if it implement all the v4l2 standard the output plugin need. I really don't know, why wouldn't you try yourself and share your result?

CatxFish avatar Apr 22 '19 22:04 CatxFish

Reporting back about akvcam I mentioned above:

  • The UDP method by @paulerickson works. I didn't bother with FIFO.
  • The obs-v4l2sink by @CatxFish also works.

However, I can't get the latency below 5 seconds in either method.

@CatxFish, the reason I mentioned akvcam is that webcamoid has very low latency, so I wonder whether there's a way to do the same in your obs-v4l2sink can. I wish I knew enough about this to contribute.

tsankuanglee avatar Apr 24 '19 05:04 tsankuanglee

OK, I made some progress and got the latency down to less than 0.5 sec. Quite practical for real-time streaming. Steps:

  1. Create a V4L2 virtual cam with akvcam. Let's say it's /dev/video2 (for capturing). (I used udev rule to make it /dev/video0 so it's the first choice for software that only supports the first device, but we don't need to do this here.)

  2. In OBS Settings -> output -> Recording, set type to Custom Output (FFmpeg), Output to URL, and use file:///dev/video2 as the URL. Container set to rawvideo (Video), and the codec also set to rawvideo.

Since akvcam works fine with this line, ffmpeg -i video.webm -s 640x480 -r 30 -f v4l2 -vcodec rawvideo -pix_fmt rgb24 /dev/video2

I added video_size=640x480 framerate=30 pixel_format=rgb24 as the Video Encoder Settings. (according to ffmpeg's format doc).

Audio should automatically be disabled.

(Side note: Some times rawvideo (Video) doesn't show up in Container Format drop down. I have no idea why. My trial and error workaround: choose Null as the container, and rawvideo as the encoder. Apply. Quit OBS. Re-open OBS. Now it's in the menu. A bug?)

Now the latency is low, but the resolution is wrong. There are 3x2 of my output in the receiving end. Like this: 3x2 output

I feel we are closer. Hopefully this is the right direction, and somebody can figure this out.

tsankuanglee avatar Apr 24 '19 08:04 tsankuanglee

I don't like changing the recording settings because then I can't stream while recording to disk simultaneously. Here's a solution that allows both.

First install the software.

sudo apt install v4l-utils v4l2loopback-utils obs-studio v4l2loopback-dkms

Create a loopback video device - for example loopback 1.

sudo modprobe v4l2loopback devices=1 card_label="loopback 1" exclusive_caps=1,1,1,1,1,1,1,1

Start ffmpeg as an rtmp server passing video to the loopback 1 video device you just created.

ffmpeg -f flv -listen 1 -i rtmp://localhost:1935/live/test -f v4l2 -vcodec rawvideo /dev/video1

Finally, configure obs to stream to the rtmp server you just created.

image

Now loopback 1 shows up as a video device. Here's it working in discord - there's ~2sec latency.

screen

josephsamela avatar Apr 28 '19 01:04 josephsamela

@JosephSamela Good job!

I pursued the akvcam route because that gave me a <0.5s latency, which doesn't create too big a problem for audio-video syncing, which is essential for video conferencing. However, I got stuck at that the 6 screen problem mentioned above.

Did you figure out a way to deal with the audio sync problem with this method?

I do have a simple workaround under Linux that works for me in video conferencing, although it's resource heavy. http://blog.tklee.org/2019/04/streaming-custom-videos-in-google.html

tsankuanglee avatar Apr 30 '19 06:04 tsankuanglee

Successfully managed to use OBS stream as a webcam source on Linux (Arch to be specific).

First, add another device using modprobe:

sudo modprobe v4l2loopback devices=1 video_nr=1 card_label="RTMP server" exclusive_caps=1

To understand what the parameters are, just see the original documentation.

I put video_nr=1 because /dev/video0 is already used by Droidcam which allows an android device to be used as a webcam. exclusive_caps "seems" to be required also to be detected by browsers

Second, start an RTMP server using ffmpeg forwarding the output to /dev/video1

ffmpeg -listen 1 -i rtmp://127.0.0.1:1935/live -f v4l2 -vcodec rawvideo /dev/video1

Note: I tried @JosephSamela's answer first and was confused why it was not working. For some reason OBS can't find localhost but can access 127.0.0.1

Third, use the created RTMP server (rtmp://127.0.0.1:1935/live) and start streaming on your OBS..

If your application does not find the new virtual device, try restarting it.

xxRockOnxx avatar May 09 '19 11:05 xxRockOnxx

@JosephSamela Your solution is working perfectly, but the image is flipped horizontally

shalkam avatar Jun 25 '19 10:06 shalkam

fixed by adding -vf hflip

ffmpeg -f flv -listen 1 -i rtmp://localhost:1935/live/test -vf hflip -f v4l2 -vcodec rawvideo /dev/video0

shalkam avatar Jun 25 '19 10:06 shalkam

Thanks to the excellent work of @JosephSamela I was able to replicate his setup and get latency down to around 1 second.

ffmpeg -an -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://localhost:1935/live/test -f v4l2 -vcodec rawvideo /dev/video2

I believe the issue is that FFMPEG does a bunch of stuff after the stream starts but before it starts to push out to /dev/videoX -- part of which is it analyzes the stream to figure out how it's encoded. I decreased the amount of time it tries to do that (-probesize 32 -analyzeduration 0), but ideally: how can this probe be disabled? We know the incoming stream. Secondly, disabling audio altogether (-an). For our purposes we only care about video.

KishCom avatar Jul 15 '19 15:07 KishCom

After having tried a lot of different approaches, I never managed to get latency under ~1s... And I think it's impossible, due to some limitations with ffmpeg.

But, latency around 1s isn't really a big issue by itself for video conferencing (for me), as we often see people with bigger network latency. It's problematic only because it makes the sound out of sync with the video. So, why not also use the sound from OBS Studio!

Here is the alias I use for that:

alias obscam="sudo modprobe v4l2loopback devices=1 video_nr=10 card_label=\"OBS Cam\" exclusive_caps=1 \
    && sudo modprobe snd-aloop index=10 id=\"OBS Mic\" \
    && pacmd 'update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\" ' \
    && ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -map 0:1 -f v4l2 -vcodec rawvideo /dev/video10 -map 0:0 -f alsa hw:10,1"

And here is the full step-by-step (on Arch Linux):

0. install required packages

sudo pacman -S obs-studio v4l-utils
git clone https://aur.archlinux.org/v4l2loopback-dkms.git
cd v4l2loopback-dkms
makepkg -scCi

1. add a virtual webcam

sudo modprobe v4l2loopback devices=1 video_nr=10 card_label="OBS Cam" exclusive_caps=1

:point_right: https://github.com/CatxFish/obs-virtual-cam/issues/17#issuecomment-490866572 for parameters details - I use video_nr=10 in order to always avoid conflict

2. add a virtual audio card

sudo modprobe snd-aloop index=10 id="OBS Mic"

Here, again, I use index=10 in order to avoid any conflict with existing sound cards, and id="OBS Mic" in order to identify it more easily in VC softs & pavucontrol.

:point_right: ArchLinux doc :point_right: Module-aloop doc

3. rename your new mic

By default, the new sound card is named "Built-in Audio Analog Stereo" is pulse audio, which makes it complicated to differentiate it from your real build-in sound card in pavucontrol, chrome, or any VC software.

pacmd 'update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\" '

:point_right: https://unix.stackexchange.com/questions/316067/change-what-pulseaudio-calls-a-device

4. Configure OBS

:question: I'm new to OBS, so if any of you think some other settings would permit to reduce latency...

  1. set streaming server

:warning: same thing than https://github.com/CatxFish/obs-virtual-cam/issues/17#issuecomment-490866572: use 127.0.0.1, not localhost

image

  1. streaming output settings

image

  1. enable your mic

image

5. test streaming

:question: any better solution for that? I don't know how to use ffprobe for this kind of use case.

  1. run a "test" ffmpeg server
ffmpeg -an -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -f v4l2 -vcodec rawvideo /dev/video10
  1. start streaming in OBS Studio

image

  1. look at ffmpeg output and identify which streams are used by audio & video
Stream #0:0: Audio: aac, 44100 Hz, stereo, 163 kb/s
Stream #0:1: Video: h264 (Constrained Baseline), yuv420p, 1280x720, 2560 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
  1. stop the ffmpeg server & stop streaming in OBS Studio

6. run the (real) ffmpeg server

ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live/test -map 0:1 -f v4l2 -vcodec rawvideo /dev/video10 -map 0:0 -f alsa hw:10,1

-map permits to differenciate audio and video streams.

:point_right: ffmpeg advanced options :point_right: ffmpeg wiki Map page

7. start streaming in OBS

8. sync audio & video

:information_source: FYI, I'm also using IP Webcam with my Galaxy S8 here, which add ~400ms delay. So, the real delay to consider here should be the one between your mic and your webcam. For me, it's around ~200ms but you can assume it's near 0 for you. There is a good chance you don't need this step if you only need to use OBS as a webcam + mic.

Do some tests (for example using https://webcamera.io and clapping).

A. sound ahead of video

Change your mic sync offset accordingly: image

Advanced Audio Properties

image

B. video ahead of sound

  1. right-click on the video source > filters
  2. click on + > select Video Delay (async)
  3. set your delay value

image

nweldev avatar Jul 24 '19 09:07 nweldev

@noelmace The video stream works fine, but I'm having trouble getting desktop audio to play in my stream in addition to microphone audio after following your steps and making a script for ones that need to be redone:

#!/bin/bash -u

sudo modprobe snd-aloop index=9 id="OBS Mic"
pacmd update-source-proplist alsa_input.platform-snd_aloop.0.analog-stereo device.description=\"OBS Mic\"
ffmpeg -probesize 32 -analyzeduration 0 -listen 1 -i rtmp://127.0.0.1:1935/live -map 0:1 -f v4l2 -vcodec rawvideo /dev/video9 -map 0:0 -f alsa hw:9,1

Under Advanced Audio Properties I've put both sources to output only to track one. I've also tried having the tracks with Monitor and Output. The desktop audio should be unmuted as well, and I do see the bars in Audio Mixer fluctuating when there's desktop audio.

Do you have any ideas? Also, my webcam image comes from a DSLR's live view with another script, but I'm doubting it has anything to do with this.

MikkoMMM avatar Feb 15 '20 15:02 MikkoMMM

Hmm, wait. The desktop audio does seem to work now in the stream. Weird, this thing. Well, if the issue returns maybe I'll get better understanding of what is causing it.

MikkoMMM avatar Feb 15 '20 19:02 MikkoMMM

After trying suggestions in this thread (ffmpeg etc), I found that the "v4l2-sink" plugin works flawlessly (~0 latency) on Ubuntu 19.10 for my use case (Google Meet). Here the blog post about my setup: https://srcco.de/posts/using-obs-studio-with-v4l2-for-google-hangouts-meet.html

hjacobs avatar Mar 16 '20 20:03 hjacobs

@hjacobs Seems to work on Arch Linux as well, though it doesn't magically resolve the latency of (I believe) getting the image from my DSLR's live view to the computer, so it remained at around 300 milliseconds which it was also with the ffmpeg method. And I still needed to use ffmpeg for rerouting the audio output from OBS to a new virtual device.

MikkoMMM avatar Mar 17 '20 13:03 MikkoMMM

Tried to do exactly as @noelmace suggested but I just get this error message: [v4l2 @ 0x55b6349529c0] 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 --

Does anyone have any suggestions as to what might be wrong?

I also tried @hjacobs suggestion, but it fails with the error message "format not support" when I try to set up V4l2sinkProperties

metareven avatar Mar 18 '20 08:03 metareven