This will automatically start your webcam once you connect it to your computer.
#!/bin/bash
printf "\n\n\n######### Set the camera to video mode and manual ##########\n\n\n"
# Unmount the camera
# sudo killall gvfs-gphoto2-volume-monitor
# Since ubuntu 22.04 we need to restart all of gvfsd to properly unmount the camera
sudo pkill -e -HUP gvfsd
# Set the aperture setting you want
# To get available settings, run gphoto2 --get-config aperture
gphoto2 --set-config-value aperture=1
# Start video capture
gphoto2 --stdout --capture-movie | ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p -threads 4 -f v4l2 /dev/video0
## Instead of using ffmpeg you could also use gst-launch-1.0, but that doesn't work for me anymore on ubuntu 21.10
# gphoto2 --stdout --capture-movie | gst-launch-1.0 fdsrc fd=0 ! decodebin name=dec ! queue ! videoconvert ! tee ! v4l2sink device=/dev/video0
chmod +x /usr/bin/dslrwebcam.sh
sudo pip3 install pyudev
6. Create a python file in /usr/bin/monitor-webcam.py
to automatically start the webcam once it becomes available:
#!/usr/bin/env python3
import pyudev
import subprocess
import os
def get_product_name(path):
product_name_path = os.path.join(path, "product")
if os.path.exists(product_name_path):
with open(product_name_path, 'r') as file:
content = file.read().strip()
return content
else:
return None
def main():
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
monitor.start()
for device in iter(monitor.poll, None):
# ----------------------------------------------------------------------
# -- Replace the following product name with the name of your camera. --
# ----------------------------------------------------------------------
# You can find out the path by commenting in the following line and running this command manually
print(device.sys_path)
print(device.action)
print(get_product_name(device.sys_path))
if device.action == "add" and get_product_name(device.sys_path) == "Canon Digital Camera":
print("starting")
subprocess.call(['/usr/bin/dslrwebcam'])
if __name__ == '__main__':
main()
chmod +x /usr/bin/monitor-webcam.py
sudo python3 /usr/bin/monitor-webcam.py
- Start your camera
- Check the name of your camera in the script output
- Update the script to check for this name instead of "Canon Digital Camera".
8. Create a service file in /etc/systemd/system/dslr-webcam-monitor.service
which runs the monitor script on startup
[Unit]
Description=Start the dslr webcam monitor
[Service]
Type=oneshot
ExecStart=python3 /usr/bin/monitor-webcam.py
[Install]
WantedBy=multi-user.target
systemctl enable dslr-webcam-monitor.service
systemctl start dslr-webcam-monitor.service
All you have to do now is plug in and turn on your dslr camera to make it available as a webcam.
Thanks for the instructions. This works great. I have a short comment regarding the 'print(device.sys_path)' for figuring out the path --- one needs to switch the device off or on, to see the path. Maybe the code has to be changed to the following:
otherwise the script will be executed also when one switches off the camera.