Programster's Blog

Tutorials focusing on Linux, programming, and open-source

FFMPEG - Extract Images

Sometimes it's useful to extract images from videos. The most common need for this is for creating thumbnails for your video files on a website. The examples below show you various ways to extract images from videos using FFmpeg.

Setting Image Quality/Compression

For all the examples below, when outputting as a jpg file, you can specify -q:v 1 to tell FFmpeg to take the highest quality image possible. You can specify a value up to 31 for more compressed images, but I go with the highest quality and perform my image manipulation later with other tools.

Examples

Extract 1 Image At Specific Point

The code below will extract 1 frame from an input file at the specified time.

ffmpeg \
  -ss hh:mm:ss \
  -i $INPUT_FILE \
  -vframes 1 \
  output.jpg

By placing -ss parameter ahead of the input, FFmpeg will seek to the closest point in the file first, and then take the screenshot. There is a chance this may result in artifacts, but it is much faster. I have not noticed any artifacts so far. The -q:v 1 tells FFmpeg to take the highest quality image possible. You can specify a value up to 31 for more compressed images, but I go with the highest quality and perform my image manipulation later with other tools.

Extract Image Every Second

ffmpeg \
  -i input.flv \
  -vf fps=1 \
  image_%d.png

Outputs will be image_1.png, image_2.png, image_3.png

If you want to prefix with 0's on the output images, use image_%03d.jpg for image_001.jpg, image_002.jpg etc.

Extract Image Every X Seconds

You can extract images every time interval by using fractions. E.g the example below will output 1 image every 450 seconds

ffmpeg \
  -i input.flv \
  -vf fps=1/450 \
  image_%d.png

Extract Every I-frame (Keyframes)

Use the command below to extract the keyframes (the frames that contain all the data necessary for the image and are not interpolated).

ffmpeg \
  -i input.flv \
  -vf "select='eq(pict_type,PICT_TYPE_I)'" \
  -vsync vfr \
  image_%d.png

References

Last updated: 28th February 2021
First published: 16th August 2018