I wrote this script, because the usb webcam I was using with Octoprint / mjpg_streamer was inserting some garbage bytes in the mjpg video stream after some of the frames, and this was preventing Octoprint from generating proper timelapses – the snapshots with the garbage data were ignored by ffmpeg, although some other programs were able to read those jpeg files just fine.
So, I decided to write a simple proxy script in python, that reads the snapshot from mjpg_streamer, processes it via imagemagick (and thus producing healthy jpeg), and serves it back to the user.
The script itself acts as a standalone http server listening on port 8083.
How to use it:
- Download, unzip, and save the script as /home/pi/snapshot.py
- Edit these parameters:
Python123456MJS_AUTH_USER = "the user for your mjpg_streamer stream"MJS_AUTH_PASS = "the password for your mjpg_streamer stream"MJS_SNAPSHOT_ADDRESS = "the mjpg_streamer snapshot url"AUTH_USER = "the user for accessing this snapshot"AUTH_PASS = "the password for accessing this snapshot"PORT_NUMBER = 8083
You don’t have to change the port number.
- To confirm it’s working, start mjpg_streamer (if not already started), and execute this:
- Now load http://your-octoprint-address:8083
- You should be asked for username/pass – just enter the same you used for AUTH_USER/AUTH_PASS. You should see the snapshot afterwards
- You will want this script running always. So, just add this to your /etc/rc.local file, on the line before the “exit 0” command:
You can edit the file by executing
Shell1sudo nano /etc/rc.local
Make your changes, and press Ctrl+O, Enter, and Ctrl+X to save & exit. And done. Reboot your Pi, and update the snapshot url in octoprint to http://AUTH_USER:AUTH_PASS@your_octoprint_address:8083
- If you followed the steps correctly, you should be able to get healthy jpg snapshots from now on.