Skip to content

Instantly share code, notes, and snippets.

@spectra
Last active August 29, 2015 14:03
Show Gist options
  • Save spectra/125ce6ef60408a865ce0 to your computer and use it in GitHub Desktop.
Save spectra/125ce6ef60408a865ce0 to your computer and use it in GitHub Desktop.
Simple MJPEG Server
#!/bin/bash
# Simple MJPEG Server
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (commit 34973274ccef6ab4dfaaf86599792fa9c3fe4689):
# <pablo@propus.com.br> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return Pablo Lorenzzoni
# ----------------------------------------------------------------------------
# MJPEG Stream frame
function echo_frame {
FILE=$1
FILESIZE=`stat -c%s $FILE`
echo -ne "--jpegframe\r\n"
echo -ne "Content-Type: image/jpeg\r\n"
echo -ne "Content-Length: $FILESIZE\r\n\r\n"
cat $FILE
echo -ne "\r\n\r\n\r\n"
}
# MJPEG Stream header
function echo_header {
echo -ne "Status: 200 OK\r\n"
echo -ne "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n"
echo -ne "Pragma: no-cache\r\n"
echo -ne "Connection: close\r\n"
echo -ne "Server: MJPEG-Bash-Streamer/0.1\r\n"
echo -ne "Content-Type: multipart/x-mixed-replace; boundary=--jpegframe"
echo -ne "\r\n\r\n"
}
DIR='/home/cameras/sources/default'
# Open MJPEG stream
echo_header
# Wait for a new frame and spill it out
inotifywait -e close_write -m --format "%f" $DIR | while read FILE; do
echo_frame $DIR/$FILE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment