Add webcam to Octoprint

Hi,

I have spent hours trying to work out how to get webcam running on Octoprint. I have worked out that you need to build and install mjpg-streamer from git and I have got this going (just) but I cannot work out how to start automatically and included in Octoprint.

Maybe adding a separate mjpg-streamer software install would be a good alternatively. Unfortunately this is beyond my skill level. The following is what I have managed so far from https://github.com/jacksonliam/mjpg-streamer.

Login as dietpi user on fresh install of DietPi with Octoprint and FFMpeg (under Software Additional):

sudo apt-get install cmake
git clone https://github.com/jacksonliam/mjpg-streamer.git
cd mjpg-streamer/mjpg-streamer-experimental
make
sudo make install

I can then run mjpg_streamer:

./mjpg_streamer -i "input_uvc.so" -o "output_http.so -p 5001"

And access stream or snapshot from:
http://dietpi:5001/?action=stream_0
http://dietpi:5001/?action=snapshot

I then go through the configuration steps for Octopi
http://dietpi:5000/

Under “Webcam & Timelapse Recordings”:

The above at least documents the install steps, which I cannot find anywhere else on the internet.

It works great. have to keep the SSH running. is there a solution for that?

Hi,

there are a couple of ways how you can put thinks into background. There is quite a good explanation on the following link https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and

It describe all the difference between nohup, disown and &

This tutorial uses armbian…but it should work just fine on DietPi

I have it running on an Orange Pi Lite using this method and it works GREAT!

https://www.youtube.com/watch?v=LcA9o6OGfEg

well, DietPi is using Armbian as base image as well for a couple of devices. :wink:

That sounds like a systemd service for running mjpg_streamer -i “input_uvc.so” -o “output_http.so -p 5001” would be smart.

Wasn’t there… ah yes a request for getting this to work: https://github.com/MichaIng/DietPi/issues/854

alaw005 thank you for your tutorial.

I can contribute how to start the script on reboot with systemctl and run in background:

Create a new .service file in /etc/systemd/system/

sudo nano /etc/systemd/system/mjpg-streamer.service

Add the following script to the file:

[Unit]
Description=Run mjpg-streamer

[Service]
Type=simple
ExecStart=/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer -i "/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/input_uvc.so" -o "/home/dietpi/mjpg-streamer/mjpg-streamer-experim$

[Install]
WantedBy=multi-user.target

If you have installed mjpg-streamer in a different location then /home/dietpi/mjpg-streamer/mjpg-streamer-experimental/, tweak the paths.

Save it.

Reload the systemctl daemon:

sudo systemctl daemon-reload

Start mjpg-streamer

sudo systemctl start mjpg-streamer

Check if mjpg-streamer is running without errors:

sudo systemctl status mjpg-streamer

Check:
http://dietpi:5001/?action=stream_0
http://dietpi:5001/?action=snapshot

Enable on system start:

sudo systemctl enable mjpg-streamer

Done!

Many thanks for sharing, I’m gonna add it as dietpi-software with v6.35, so the timing is great.

I was just coming here to request this! Now I see that, thankfully, there is no point. Can’t wait to see it added!

Thanks for the autostart instructions, but I believe the following line of code was cut off (which is causing issues):

ExecStart=/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer -i "/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/input_uvc.so" -o "/home/dietpi/mjpg-streamer/mjpg-streamer-experim$

Got it working with the following as the full contents of the .service file:

[Unit]
Description=Run mjpg-streamer

[Service]
Type=simple
ExecStart=/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/mjpg_streamer -i "/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/input_uvc.so" -o "/home/dietpi/mjpg-streamer/mjpg-streamer-experimental/output_http.so -p 5001"

[Install]
WantedBy=multi-user.target

Please correct it in your (awesomely helpful) comment and I’ll delete this comment.

Thanks for tutorials!
Could you please explain how to add this additional parameters?

-r 1280x960 -f 30

i’ve tried do it in this area:

Run dietpi-services
Select mjpg-streamer
Select Edit

https://dietpi.com/docs/software/camera/#mjpg-streamer

and here:
sudo nano /etc/systemd/system/mjpg-streamer.service

but without any sucsess


Thank you in advance!

First of all you freshly installed mjpg-streamer via dietpi-software, right?

Do not edit /etc/systemd/system/mjpg-streamer.service directly, it would be overwritten by any reinstall/update. Doing it via dietpi-services > mjpg-streamer > Edit was the best approach. Take care to add an empty ExecStart= before the actual new ExecStart line, so that it looks like that:

[Service]
#User=mjpg-streamer
#WorkingDirectory=/opt/mjpg-streamer
ExecStart=
ExecStart=/opt/mjpg-streamer/mjpg_streamer -i 'input_uvc.so -d /dev/video0 -r 1280x960 -f 30' -o 'output_http.so -p 8082 -n'

Thank you MichaIng !
With HD resolution printing freezes from time to time (
640x480 - works fine
according to HTOP - mjpg-streamer consumes less than 15% of CPU.

This doesn’t appear to be working. I had to manually edit /etc/systemd/mjpg-streamer.service to get the camera to change resolutions. mjpg-streamer isn’t using the edited one created in dietpi-services.

You can verify this by looking at the status in the diet-pi services.

Am I doing something wrong?

To see all loaded configs for the service:

systemctl cat mjpg-streamer

The Edit option generally must work.

It is not. :frowning:

Below is my systemctl cat mjpg-streamer result:

[Unit]
Description=mjpg-streamer (DietPi)
Documentation=https://github.com/jacksonliam/mjpg-streamer/tree/master/mjpg-streamer-experimental
Wants=network-online.target
After=network-online.target

[Service]
User=mjpg-streamer
WorkingDirectory=/opt/mjpg-streamer
ExecStart=/opt/mjpg-streamer/mjpg_streamer -i 'input_uvc.so -d /dev/video0 -r 1920x1080 -f 30' -o 'output_http.so -p 8082 -n'

# Hardening
ProtectSystem=strict
PrivateTmp=true
ProtectHome=true
ProtectKernelTunables=true
ProtectControlGroups=true

[Install]
WantedBy=multi-user.target

This is my edited one that is shown in dietpi-services->mjpg-streamer->edit (the only difference is the resolution and this is due to me testing if it was loading and the blank ExecStart as mentioned earlier in this thread).

[Unit]
Description=mjpg-streamer (DietPi)
Documentation=https://github.com/jacksonliam/mjpg-streamer/tree/master/mjpg-streamer-experimental
Wants=network-online.target
After=network-online.target

[Service]
User=mjpg-streamer
WorkingDirectory=/opt/mjpg-streamer
ExecStart=
ExecStart=/opt/mjpg-streamer/mjpg_streamer -i 'input_uvc.so -d /dev/video0 -r 1280x720 -f 30' -o 'output_http.so -p 8082 -n'

# Hardening
ProtectSystem=strict
PrivateTmp=true
ProtectHome=true
ProtectKernelTunables=true
ProtectControlGroups=true

[Install]
WantedBy=multi-user.target

Is there any drop-in file?

ls -l /etc/systemd/system/mjpg-streamer.service.d

When you created the Edit with dietpi-services, did you hit CTRL+O or confirmed on exit to actually store the file?

Hello, I have the same problem. When editing with dietpi-services the changes are not saved in “/etc/systemd/system/mjpg-streamer.service.d/dietpi-services_edit.conf” but in “/dietpi-services_edit.conf”. Moving the file to the correct location fixes the issue.

I am on a Pi 4 with v.8.0.2 freshly installed.

Many thanks, that was a good hint. Indeed a v7.9 regression where we combined multiple variable definitions into one line. But when defining local variables with a single local call, later ones in the same call cannot reference earlier ones, but the calls need to be split. When defining regular/global variables, it works well to defining multiple ones in one line, where later ones reference earlier ones :roll_eyes:.

Also the process tool settings were affected for the same reason.

Fixed with, including removing the ineffective possible two files: https://github.com/MichaIng/DietPi/commit/36d35e9