After playing around with the camera_usb_options
settings in /boot/octopi.txt
, I was able to configure most of my preferences, but there's one parameter the mjpg-streamer interface doesn't seem to have options for, so I had to come up with a different way to get what I want.
I hate the constant focus-hunting behavior of the ancient USB camera I'm using, so I wanted to turn that off and set a constant focus distance instead. My solution was to use the Event System to run some shell commands whenever the server starts up. Fortunately, this was pretty easy to set up. There are just three easy steps.
First, you're going to need a command-line utility that lets you change the settings of your camera.
Linux has one called v4l2-ctl that lets you set all kinds of parameters for any attached USB webcams. That does everything I want, so that's what I'm going to use, but if you have a different command-line tool, you can use yours instead by replacing my commands with your own in the YAML section below.
If you want to use the v4l2-ctl utility, begin by making sure it's actually installed. Do this with:
sudo apt install v4l-utils
.
Next, we need to edit /home/pi/.octoprint/config.yaml and add the following:
events:
subscriptions:
- command:
- v4l2-ctl --set-ctrl=focus_auto=0
- v4l2-ctl --set-ctrl=focus_absolute=17
- v4l2-ctl --set-ctrl=brightness=70
event: Startup
type: system
In this example, I've disabled autofocus, set a constant focus distance of 17 and told OctoPrint to run those v4l2-ctl commands as system (shell) commands whenever the server starts up. And just for demonstration purposes, I've also set the brightness to 70, to show that all the normal camera preferences can be set here as well. (You can run: sudo v4l2-ctl -L
to get a list of all the different parameters that can be controlled with this method.)
Finally, we need to take care of permissions. The v4l2-ctl command requires sudo rights to execute, but by default, OctoPrint runs as the pi user, who does not have those privileges, so we have to grant them. Fortunately, that's also pretty easy. Just create a new file in the /etc/sudoers.d/ directory. You can call it whatever you like, but I called mine octoprint-v4l2. Inside that file, you need a single line that reads:
pi ALL=NOPASSWD: /usr/bin/v4l2-ctl
This will give the pi user the right to run the v4l2-ctl utility as sudo without having to provide a password to do so. (You should never grant sudo permissions lightly, but that's a conversation for a different thread. I'm assuming you know your situation better than I do, and can judge the slight increase in risk for yourself.)
Anyway, that's all there is to it. Reboot your Pi. When OctoPrint comes back up, it should be with all your sweet, sweet camera prefs set just the way you like them.