back to index

mjpegparse


Problem
Solution approach
Command
      Features
            stdin
            output file names
            remote timestamps
            local timestamps
            stdout
            stderr
            verbosity
            dry run
            total number of frames
            skip initial frames
            skip intermediate frames
Examples
Download
TODO

Problem

Many cameras provide output in MJPEG format, without providing option to acquire single JPEG frames.

Decoding MJPEG streams can be annoying. Debugging the same streams, especially when fiddling with their generation and making the headers, can also be annoying. There is a general lack of lightweight tools. Especially when piping data stream from eg. curl is desired.


Solution approach


Command

mjpegparse - MJPEG stream parser
Reads MJPEG stream from a file, or from stdin ("-")

Usage: mjpegparse [options] <input file>
Options:
   -n <frames>  stop reading after n frames (0=never stop, default=10)
   -o <fname>   name of output file, default frame.jpg; prefix-only with numbers and timestamps, - for stdout
   -q           quiet; do not show headers
   -qq          quieter, do not show anything
   -sa          save all frames, implies numbering of filenames
   -s           save last frame, needs -n
   -N           do not save any file, just output messages
   -xts         use timestamps from X-Timestamp: headers
   -se          output messages to stderr (implied with -o -)
   -skip <n>    skip n frames in the beginning
   -nth <n>     output only each n-th frame
   -h           this help

Example:
curl http://webcam:8000/stream | parsemjpeg -n 5 -o snapsnot.jpg -s
fetches MJPEG stream, saves 5th image to snapshot.jpg

Features

stdin

Data can be fed to the process from a file, or from stdin. The "filename" for stdin is -.

output file names

For single file output, the filename can be directly set.

For multiple filenames, the initial "prefix" part of the filename is provided, with dash, frame number, and .jpg extension appended for each file.

remote timestamps

Some custom systems provide precision timestamps for each frame (X-Timestamp: header). These can be appended to the file name, if chosen by -xts option.

local timestamps

Frame filenames can be timestamped by the local time-of-arrival, using -lts option. Useful with realtime stdin input.

stdout

If - is specified as output filename, the jpeg frames are output to stdout. All other output is automatically sent to stderr.

stderr

Normally, non-error output data are sent to stdout. With -se option they can be sent to stderr instead.

verbosity

Normally, the tool outputs copious amount of messages about what it is doing, to provide operator awareness. This can be suppressed with -q and -qq options, for quiet and quieter, which first won't output the headers, then nothing else.

dry run

To see what will happen, without creating or overwriting files, -N option can be used.

total number of frames

Often it's not desirable to save an unlimited amount of the stream. Often only first few frames are enough, or even just one, but after skipping the first couple (to give the camera some time to run autoexposure and other amenities, if not already having done so), then the process exits. For this, -n <num> option is provided.

By default, n=10.

If n is set as 0,there is no limit.

skip initial frames

Sometimes the camera needs some time to adjust properly - set ADC, brightness... for that, some beginning frames can be skipped by -skip <n>.

skip intermediate frames

Full framerate may also not be desirable. For previews, timelapses, or just plain lowering amount of data to store, only each n-th frame can be saved. For that, use -nth <num>.


Examples

Receive stream from espcam, an ESP32 based wifi camera, on espcam.local; skip first frames, save 20th one to file.jpg and exit

curl http://espcam.local:81/stream | mjpegparse -s -n 20 -o file.jpg -

Receive stream from espcam.local, save first 5 frames to files fileX.jpg, then exit

curl http://espcam.local:81/stream | mjpegparse -sa -n 5 -o file- -

Receive stream from espcam.local, pipe 5th frame to jpegtopnm and convert to png

curl http://espcam.local:81/stream | mjpegparse -s -n 5 -o - - | jpegtopnm | pnmtopng > file.png

Receive stream from espcam.local, save first 100 frames to timestamped files in subdirectory f

curl http://espcam.local:81/stream | mjpegparse -sa -ts -n 100 -o f/file- -

Receive stream from espcam.local, use first 50 frames, skip first 3 frames, save each fifth frame (but do not actually save), local output to stderr, use both remote and local timestamp (remote clock is one hour less than local)

curl -s http://espcam.local:81/stream |./mjpegparse -q -n 50 -sa -se -N -skip 3 -nth 5 -xts -lts -
Reading from stdin...
Would write to file 'frame4-2021-09-01T01:00:19.002--2021-09-01T02:00:19.707.jpg'.
Would write to file 'frame9-2021-09-01T01:00:19.270--2021-09-01T02:00:19.973.jpg'.
Would write to file 'frame14-2021-09-01T01:00:19.518--2021-09-01T02:00:20.221.jpg'.
Would write to file 'frame19-2021-09-01T01:00:19.790--2021-09-01T02:00:20.508.jpg'.
Would write to file 'frame24-2021-09-01T01:00:20.031--2021-09-01T02:00:20.733.jpg'.
Would write to file 'frame29-2021-09-01T01:00:20.286--2021-09-01T02:00:20.995.jpg'.
Would write to file 'frame34-2021-09-01T01:00:20.551--2021-09-01T02:00:21.255.jpg'.
Would write to file 'frame39-2021-09-01T01:00:20.803--2021-09-01T02:00:21.506.jpg'.
Would write to file 'frame44-2021-09-01T01:00:21.070--2021-09-01T02:00:21.784.jpg'.
Would write to file 'frame49-2021-09-01T01:00:21.313--2021-09-01T02:00:22.022.jpg'.


Download

Sources:

Binaries (may not run on earlier or later systems, due to .so dependencies):


TODO


If you have any comments or questions about the topic, please let me know here:
Your name:
Your email:
Spambait
Leave this empty!
Only spambots enter stuff here.
Feedback: