Skip to content

Instantly share code, notes, and snippets.

@Vertecedoc4545
Last active June 22, 2023 17:57
Show Gist options
  • Save Vertecedoc4545/e1ad0b12a522d7ca5caed538005a4fd7 to your computer and use it in GitHub Desktop.
Save Vertecedoc4545/e1ad0b12a522d7ca5caed538005a4fd7 to your computer and use it in GitHub Desktop.
Use Canon DSLR camera as webcam with minimum latency posible

Resentelly i come across several guides for this but the delay is quite awful and i didnt found any respect to that. so Here we go

First install dependencies i recommend following the guide on archwiki or other guides for this pourpuse as this guide https://opensource.com/article/22/12/old-camera-webcam-linux or https://www.youtube.com/watch?v=TsuY4o2zLVQ

Result ShowCase: video

Ubunutu:

sudo apt-get install gphoto2 v4l2loopback-utils v4l2loopback-dkms ffmpeg build-essential libelf-dev linux-headers-$(uname -r) unzip vlc

Load the kernel module

sudo modprobe v4l2loopback exclusive_caps=1 max_buffer=2 

then connect your camera throught usb, and in ubuntu you will have to kill gcfs-gphoto2 automount

pkill -f gphoto2

now we will use this command:

following this page: https://wiki.archlinux.org/title/Hardware_video_acceleration, you should: Note: I reccomend using the offload environment variables i you have a discrite gpu a.k.a Nvidia, or Dedicated Amd or New Desktop Intel graphics

for nvidia card owners with propietory drivers: depends on your hardware, i recomend using cuda if is too laggy i'd try vdpau, vaapi, vulkan and opencl and choose the one with the least amount of lagg

__NV_PRIME_RENDER_OFFLOAD=1 __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0  __GLX_VENDOR_LIBRARY_NAME=nvidia  __VK_LAYER_NV_optimus=NVIDIA_only  gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel cuda -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

for intel gpu owners: first try vaapi, then if doesnt work try opencl and vulkan, use the one with the least anount of delay, you can check it in any webcam using application

gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel vaapi -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

if doesnt work try: and

gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel opencl -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

or

gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel vulkan -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

for amd owners: you can try vdpau or vaapi, depending on your hardware check: https://wiki.archlinux.org/title/Hardware_video_acceleration

gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel vdpau -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

or

gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel vaapi -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

basically what it does is to take the liveview and process it throught optimised ffmpeg, you can change the image in any mode in the camera and change the image vales and they are going to change in the prossed image in the computer

i recomend at once you have the best command with the least lantency put it in a shell script and on to your system path exmaple:

dslrcam.sh

pkill -f gphoto2
sleep 4
gphoto2 --stdout --capture-movie | ffmpeg -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512  -hwaccel cuda -fflags nobuffer -flags low_delay -strict experimental -probesize 32 -analyzeduration 0 -i - -vcodec rawvideo -preset ultrafast -tune zerolatency  -pix_fmt yuv420p -f v4l2 /dev/video0 -framerate 30

then do: chmod a+rx dslrcam.sh and sudo mv dslrcam.sh /usr/bin

then i recoment to create automatic module loading in ubuntu: sudo nano /etc/modules in file:

dslr-cam

then sudo nano /etc/modprobe.d/dslr-cam.conf in file:

/etc/modprobe.d/dslr-cam.conf

for using simple run the command dslr-cam after have connected your cammera and you'll be good to go

Then if you will you can create a systemd service and configure gvfs backends but for me is better to let it be simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment