Thursday, August 12, 2021

Converting individual frames from an HTTP source into MJPEG

Using the same cgi-bin setup as in a couple of posts earlier, this script will repeatedly grab a still frame from a camera, and turn it into a MJPEG stream.

I got myself another camera that claimed to support MJPEG, but it turned out that is what the manufacturer was calling repeatedly fetching individual JPEG frames. Not a neverending stream of them in a single request. *sigh*.

Anyway, we can fix that...

#!/bin/bash

echo "Content-Type: multipart/x-mixed-replace;boundary=boundary12345"
echo "Cache-Control: no-cache"
echo ""
while :
do
    echo "--boundary12345"
    curl -is "http://admin:SECRET@192.168.60.15/mjpgstreamreq/1/image.jpg"
    sleep 0.2
done

Adjust the user:password to suit your device, along with the URL for getting a frame.

This gives a 5 FPS video output. Note that sub-second sleep may not be available on your platform, in which case make it "sleep 1" or find another way to have a shorter day.

It is also intentional that the content-type boundary definition doesn't have the double dash "--", while in the stream it does.

The CPU load of this is very low, as its just copying data without any processing in between.