Saturday, November 11, 2017

OctoPrint video streaming

OctoPrint video streaming
Before you start to make sure your webcam supports MJPEG:
Install v4l-utils (apt-get install v4l-utils) and check the supported formats:
v4l2-ctl -d /dev/video0 --list-formats
There's usually at least one YUV format and hopefully MJPEG as well. The Microsoft Lifecam HD-3000 is - to my knowledge - the cheapest webcam supporting MJPEG at the moment (Q1/2017). The use of YUV will result in a high utilization of CPU and USB bandwidth at higher resolutions, therefore I would recommend to use these a webcam supporting MJPEG with OctoPrint (unless you're fine with 320x200 pixels using YUV).

Setup - short version
lsusb
apt-get update
apt-get upgrade
apt-get install libjpeg62-turbo-dev imagemagick libv4l-dev
ln -s /usr/include/linux/videodev2.h /usr/include/linux/videodev.h
apt-get install subversion
cd ~
svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer/ mjpg-streamer
cd mjpg-streamer
make mjpg_streamer input_file.so input_uvc.so output_http.so
cp mjpg_streamer /usr/local/bin
cp output_http.so input_file.so input_uvc.so /usr/local/lib/
touch /etc/init.d/mjpg_streamer.sh
echo '#!/bin/sh
# /etc/init.d/mjpg_streamer.sh
# v0.2 phillips321.co.uk
# v0.3 Jenny Cash
### BEGIN INIT INFO
# Provides:          mjpg_streamer.sh
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mjpg_streamer for webcam
# Description:       Streams /dev/video0 to http://IP/?action=stream
### END INIT INFO

f_message(){
        echo "[+] $1"
}

# Carry out specific functions when asked to by the system
case "$1" in
        start)
                f_message "Starting mjpg_streamer"
                /usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -r 1280x720 -f 30" -o "/usr/local/lib/output_http.so"
                f_message "mjpg_streamer started"
                ;;
        stop)
                f_message "Stopping mjpg_streamer..."
                killall mjpg_streamer
                f_message "mjpg_streamer stopped"
                ;;
        restart)
                f_message "Restarting daemon: mjpg_streamer"
                killall mjpg_streamer
                /usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -r 1280x720 -f 30" -o "/usr/local/lib/output_http.so"
                sleep 2
                f_message "Restarted daemon: mjpg_streamer"
                ;;
        status)
                pid=`ps -A | grep mjpg_streamer | grep -v "grep" | grep -v mjpg_streamer. | awk '{print $1}' | head -n 1`
                if [ -n "$pid" ];
                then
                        f_message "mjpg_streamer is running with pid ${pid}"
                        f_message "mjpg_streamer was started with the following command line"
                        cat /proc/${pid}/cmdline ; echo ""
                else
                        f_message "Could not find mjpg_streamer running"
                fi
                ;;
        *)
                f_message "Usage: $0 {start|stop|status|restart}"
                exit 1
                ;;
esac
exit 0' > /etc/init.d/mjpg_streamer.sh
chmod 755 /etc/init.d/mjpg_streamer.sh
update-rc.d mjpg_streamer.sh defaults
#to remove it from default startup:
#update-rc.d -f mjpg_streamer.sh remove 

Source (setup - long version):
http://www.phillips321.co.uk/2012/11/05/raspberrypi-webcam-mjpg-stream-cctv/


Manual start of MJPG_Streamer
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -r 1280x720 -f 30" -o "/usr/local/lib/output_http.so"
or
service mjpg_streamer start

service mjpg_streamer stop

service mjpg_streamer restart

Last steps
Reboot and verify that it is started automatically at startup.
Install ffmpeg
apt-get install libav-tools
apt-get install ffmpeg


and configure OctoPrint (Stream URL, snapshot URL, path to FFMPEG):
http://<ip>:8080/?action=stream

http://<ip>:8080/?action=snapshot
/usr/bin/ffmpeg

Stream URL (If your OctoPrint is at 192.168.0.5):
http://192.168.0.5:8080/?action=stream

No comments:

Post a Comment