How to Transcode Video to MJPEG with FFmpeg

This article provides a straightforward guide on how to transcode video files into the Motion JPEG (MJPEG) format using the FFmpeg command-line tool. You will learn the essential commands, how to control video quality, adjust frame rates, resize the video, and handle audio streams to produce compatible and high-quality MJPEG outputs.

The Basic MJPEG Conversion Command

To convert a video to MJPEG, you need to specify the MJPEG encoder using the -c:v mjpeg flag. Since MJPEG is commonly stored in an AVI container, the basic command looks like this:

ffmpeg -i input.mp4 -c:v mjpeg output.avi

Controlling Video Quality

By default, FFmpeg may apply a high compression rate, leading to a loss in visual quality. You can control the output quality using the -q:v (or -qscale:v) option.

The quality scale ranges from 1 to 31, where 1 is the highest quality (largest file size) and 31 is the lowest quality. A value between 2 and 5 is recommended for an optimal balance between quality and file size.

ffmpeg -i input.mp4 -c:v mjpeg -q:v 3 output.avi

Customizing Resolution and Frame Rate

If you need to resize the video or change the frame rate during the transcoding process, you can use the scale filter (-vf scale) and the frame rate flag (-r).

The following command resizes the video to 1280x720 pixels and sets the frame rate to 30 frames per second:

ffmpeg -i input.mp4 -c:v mjpeg -q:v 3 -vf scale=1280:720 -r 30 output.avi

Handling Audio Streams

MJPEG is a video-only format, but the containers that hold it (like AVI or MP4) can support audio. You have three main options for handling audio during the transcode:

  1. Copy the original audio (fastest, no re-encoding):

    ffmpeg -i input.mp4 -c:v mjpeg -q:v 3 -c:a copy output.avi
  2. Convert audio to PCM (uncompressed, highly compatible with legacy players):

    ffmpeg -i input.mp4 -c:v mjpeg -q:v 3 -c:a pcm_s16le output.avi
  3. Disable audio entirely:

    ffmpeg -i input.mp4 -c:v mjpeg -q:v 3 -an output.avi