MJPEG - Motion JPEG

Table of contents
  1. MJPEG - Motion JPEG
    1. MJPEG
    2. Recommended Flags
      1. qscale:v Comparison
    3. Alternative approach

Motion JPEG has historically been quite common in the VFX industry, and is part of the default Quicktime codecs, so is well supported on players, although not typically supported in web browsers.

While it does compress quickly, and maintains its quality fairly well, we would recommend other codecs at higher bit-depths such as VP9, H.264 or H.265 (or even ProRes or DNxHD), which will also compress better than MJPEG.

MJPEG

MJPEG has a limited range of pixel formats: yuvj420p yuvj422p yuvj444p yuv420p yuv422p yuv444p

Example encoding:

ffmpeg -r 24 -start_number 1 -i inputfile.%04d.png -frames:v 200 \
   -vf "setparams=color_primaries=bt709:color_trc=iec61966-2-1" -colorspace bt470bg \
   -c:v mjpeg -qscale:v 2 outputfile.mov

setparams sets the primaries and transfer function, and -colorspace bt470bg states the BT.601 matrix that MJPEG uses anyway; it does not change the pixels. Both are needed for the MOV to get a colr atom: without -colorspace the muxer leaves out the primaries and transfer as well, and without setparams the file has neither (tested on 6.1.6, 7.1.5 and 9.0.2). The JPEG data itself always says full range BT.601.

Leave -pix_fmt off, as above, or use a yuvj format such as -pix_fmt yuvj444p. Both give correct full range files on 6.1.6, 7.0, 7.1.5, 8.1, 9.0.2 and a 9.1 development build. On 7.0 and earlier, a plain -pix_fmt yuv444p does not encode, and adding -color_range pc to make it encode writes limited range pixels into a file tagged full range. From 7.1 it is converted correctly.

Flag Description
-qscale:v 2 This is the compression factor, which goes from 2 to 31 where 2 is the best quality. See the alternative approach for higher quality.

qscale:v Comparison

Below is a comparison of different Qscale rates

This is showing qscale:v values against encoding time.
This is showing qscale:v values against file size.
This is showing qscale:v values against VMAF harmonic mean
This is showing qscale:v values against PSNR-Y harmonic mean

Alternative approach

You can generate the individual jpeg files using another tool, and then use FFmpeg to stitch them together. For example, to convert a series of png frames to jpeg, you can use oiiotool:

oiiotool -v --parallel-frames -i PNGFILES.%05d.png --compression jpeg:95 -o JPEGFILES.%05d.jpg

and then stitch them together with:

ffmpeg -f image2 -r 24 -i JPEGFILES.%05d.jpg -vcodec copy outputfile.mov

This gives you more fine grain control over the quality setting, although it gives you less control over the jpeg compression, since it will always be YUV420, where you do get more control with FFmpeg. It’s actually hard to get an exact mapping between the FFmpeg -qscale setting and the jpeg quality. The highest FFmpeg setting is 2, and that roughly maps to a jpeg quality setting of 86%. So if you require higher quality than 86% then use this approach. Settings below 95% do produce artifacts, so this is the recommended approach if you have to do this, a 10-bit encoder would be better though.

Below shows a comparison of jpeg compression to filesize for a HD sized image sequence of 200 frames.


Copyright © 2022 ORI Contributors. Distributed under a CC BY 4.0 license.

This site uses Just the Docs, a documentation theme for Jekyll.