How can I extract a good quality JPEG image from a video file with ffmpeg?

Daniel Gartmann picture Daniel Gartmann · Apr 19, 2012 · Viewed 131.9k times · Source

Currently I am using this command to extract the images:

ffmpeg -i input.mp4 output_%03d.jpeg

But how can I improve the JPEG image quality?

Answer

llogan picture llogan · Apr 19, 2012

Use -qscale:v to control quality

Use -qscale:v (or the alias -q:v) as an output option.

  • Normal range for JPEG is 2-31 with 31 being the worst quality.
  • The scale is linear with double the qscale being roughly half the bitrate.
  • Recommend trying values of 2-5.
  • You can use a value of 1 but you must add the -qmin 1 output option (because the default is -qmin 2).

To output a series of images:

ffmpeg -i input.mp4 -qscale:v 2 output_%03d.jpg

See the image muxer documentation for more options involving image outputs.

To output a single image at ~60 seconds duration:

ffmpeg -ss 60 -i input.mp4 -qscale:v 4 -frames:v 1 output.jpg

This will work with any video input. See below if your input is MJPEG.


MJPEG

If your input is MJPEG (Motion JPEG) then the images can be extracted without any quality loss.

The ffmpeg or ffprobe console output can tell you if your input is MJPEG:

$ ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=nw=1 input.avi
codec_name=mjpeg

Then you can extract the frames using the mjpeg2jpeg bitstream filter:

$ ffmpeg -i input.avi -codec:v copy -bsf:v mjpeg2jpeg output_%03d.jpg

Also see