Skip to content

Extract thumbnails from a video with FFmpeg

After you create a video, one of the most common things you want to do with it is grab thumbnails.

Thumbnails have many uses:

  1. Use them as the poster for your video player.
  2. Analyze them for content moderation, use AI to generate tags to associate with the video
  3. When rendering a list of videos for users to browse
  4. Sharing on social media with the og:image tag

LinkExtract a single thumbnail at a specific timestamp

bash
ffmpeg -i input_video.mp4 \ -vf "select='gte(t,9)',scale=320:-1" \ -frames:v 1 thumbnail.png

Here's a breakdown of the command:

  • -i input_video.mp4: Specifies the input video file.
  • -vf "select='gte(t,9)',scale=320:-1": Applies the thumbnail filter and scales the output image to width 320 pixels while preserving the aspect ratio. Adjust the resolution as needed. gte(t,9) is selecting the 9 second mark to extract the thumbnail. For one minute you would use gte(t,60), you can also be more prices, for example 7.5 seconds: gte(t,7.5)
  • -frames:v 1: Extracts only one frame (you can adjust this to extract more frames).
  • thumbnail.png: The name of the output image file.

LinkExtract a multiple thumbnails at regular intervals

If you want to extract multiple thumbnails at regular intervals, you can modify the command to include a timestamp pattern and specify fps=1 to get 1 thumbnail per second:

bash
ffmpeg -i input_video.mp4 \ -vf "fps=1,scale=320:-1" \ -vsync vfr \ -q:v 2 thumb%04d.png
  • -vsync vfr: Ensures variable frame rate for correct frame extraction.
  • -vf "fps=1,scale=320:-1": Tells FFmpeg to produce 1 frame per second and to scale the thumbnail to 320 pixels wide
  • -q:v 2: Sets the quality of the output thumbnails (lower values mean higher quality).
  • thumb%04d.png: Specifies the output file name pattern where %04d will be replaced by a sequence number (e.g., thumb0001.png, thumb0002.png, etc.).

LinkExtract 1 thumbnail per I-frame or keyframe

The most time and resource efficient way to extract multiple thumbnails from a video would be to extract each I-frame (or keyframe) from the video.

bash
ffmpeg -i input_video.mp4 \ -vf "select='eq(pict_type,I)',scale=320:-1" \ -vsync vfr \ -q:v 2 thumb%04d.png

Here's a breakdown of the command:

  • select='eq(pict_type,I)' selects only I-frames. eq(pict_type,I) checks if the frame is an I-frame

This will output an image for every single I-frame of the video, which might be a lot. You could modify this command to only select every 10th I-frame with another filter:

LinkExtract 1 thumbnail for every 10th I-frame or keyframe

This command is nearly the same but adds select='not(mod(n,10))' which will take the previously selected I-frames and select every 10th one.

bash
ffmpeg -i input_video.mp4 \ -vf "select='eq(pict_type,I)',select='not(mod(n,10))',scale=320:-1" \ -vsync vfr \ -q:v 2 thumb%04d.png

In the select='not(mod(n,10))' command

  • n is the frame number
  • mod(n,10) is the remainder when n is divided by 10
  • not(mod(n,10)) is true when the remainder is 0 (i.e. for every 10th frame)

LinkBenefits and drawbacks of extracting I-frame thumbnails

If you're doing the I-frame approach, there are some benefits and drawbacks to be aware of. Weather you choose this direction or not depends on if your particular use case is sensitive to the limitations of this approach.

LinkBenefits

  • Efficiency: I-frames are complete images that don't depend on any other frames, which means ffmpeg can grab them quickly without a lot of computation.
  • Quality: I-frames typically have the best quality in a video stream because they are self-contained
  • Representativeness: I-frames often occur at scene changes or significant moments in the video, making them good candidates for a set of images that will represent the content contained in the video.

LinkDrawbacks

  • The spacing between I-frames can be irregular and unpredictable
  • Some videos, especially videos optimized for streaming might have very few I-frames, which limits the number of thumbnails that you get.

LinkExtracting thumbnails with Mux

If you have videos hosted with the Mux Video API you can extract thumbnails, gifs and storyboards on-demand with the image API.

No credit card required to start using Mux.