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.

1 comment:

Unknown said...

I am hoping you can help

Based on your JPG to MJPEG post, I did something similar with a Trednet camera.

#!/bin/bash

echo "Content-Type: multipart/x-mixed-replace;boundary=jpeg"
echo "Cache-Control: no-cache"
echo ""
while :
do
echo "--jpeg"
curl -is --anyauth "http://admin:admin@192.168.100.131/ISAPI/Streaming
/channels/101/picture"
sleep 0.2
done

that link, when I just put it in a regular browser gives me a JPG image

when I execute the script, I get

HTTP/1.1 200 OK Content-Type: image/jpeg; charset="UTF-8" Connection: close Content-Length:82877 яШяа JFIF яЫC 2! =,.$2I@LKG@FEPZsbPUmVEFd€emw{

from the command line, I get

HTTP/1.1 200 OK
Content-Type: image/jpeg; charset="UTF-8"
Connection: close
Content-Length:83005

--jpeg
HTTP/1.1 401 Unauthorized
Date: Fri, 10 Dec 2021 21:12:15 GMT
Server: webserver
X-Frame-Options: SAMEORIGIN
Content-Length: 178
Content-Type: text/html
Connection: close
WWW-Authenticate: Digest qop="auth", realm="IP Camera(86384)", nonce="4d7a49325a
44497a4d7a67365954597a4d44646d5932593d", stale="FALSE"

Any idea how I can tweak this to work?