WEBカメラの自動起動設定

OctoPrint

Ubuntuでmjpg-streamerを自動起動

mjpg-streamerを起動時に自動的に起動させたい場合:

(例: 実行)で、次の内容の新しいファイルを作成します。

/home/****/scripts/webcamDaemon

webcamDaemonの内容

#!/bin/bash

MJPGSTREAMER_HOME=/home/****/mjpg-streamer/mjpg-streamer-experimental
MJPGSTREAMER_INPUT_USB="input_uvc.so"
MJPGSTREAMER_INPUT_RASPICAM="input_raspicam.so"

# init configuration
camera="auto"
camera_usb_options="-r 640x480 -f 10"
camera_raspi_options="-fps 10"

if [ -e "/boot/octopi.txt" ]; then
    source "/boot/octopi.txt"
fi

# runs MJPG Streamer, using the provided input plugin + configuration
function runMjpgStreamer {
    input=$1
    pushd $MJPGSTREAMER_HOME
    echo Running ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    LD_LIBRARY_PATH=. ./mjpg_streamer -o "output_http.so -w ./www" -i "$input"
    popd
}

# starts up the RasPiCam
function startRaspi {
    logger "Starting Raspberry Pi camera"
    runMjpgStreamer "$MJPGSTREAMER_INPUT_RASPICAM $camera_raspi_options"
}

# starts up the USB webcam
function startUsb {
    logger "Starting USB webcam"
    runMjpgStreamer "$MJPGSTREAMER_INPUT_USB $camera_usb_options"
}

# we need this to prevent the later calls to vcgencmd from blocking
# I have no idea why, but that's how it is...
vcgencmd version

# echo configuration
echo camera: $camera
echo usb options: $camera_usb_options
echo raspi options: $camera_raspi_options

# keep mjpg streamer running if some camera is attached
while true; do
    if [ -e "/dev/video0" ] && { [ "$camera" = "auto" ] || [ "$camera" = "usb" ] ; }; then
        startUsb
    elif [ "`vcgencmd get_camera`" = "supported=1 detected=1" ] && { [ "$camera" = "auto" ] || [ "$camera" = "raspi" ] ; }; then
        startRaspi
    fi

    sleep 120
done

ファイルが実行可能であることを確認

ファイルが実行可能であることを確認してください。

chmod +x /home/****/scripts/webcamDaemon

webcamd.serviceを作成

次に、次の行を使用して、()に別の新しいファイルを作成します。

/etc/systemd/system/webcamd.service

webcamd.serviceの内容

[Unit]
Description=Camera streamer for OctoPrint
After=network-online.target OctoPrint.service
Wants=network-online.target

[Service]
Type=simple
User=****
ExecStart=/home/****/scripts/webcamDaemon

[Install]
WantedBy=multi-user.target

サービスの有効化

新しいファイルを読み取るようにシステムに指示します。

sudo systemctl daemon-reload

最後に、サービスを有効にします。

sudo systemctl enable webcamd

Webカメラサーバーの起動および停止

OctoPrintのシステムメニューからWebカメラサーバーを起動および停止できるようにする場合は、以下をに追加します。

.octoprint/config.yaml

config.yamlの内容

system:
  actions:
   - action: streamon
     command: sudo systemctl start webcamd
     confirm: false
     name: Start video stream
   - action: streamoff
     command: sudo systemctl stop webcamd
     confirm: false
     name: Stop video stream