Extract video frame/s as image

Let's just say i was trying to learn how to better use ffmpeg, and started asking myself "What are the things i needed when working with video content?".

And this is one of the things that came to mind. A simple way to extract one or several video frames from a video file, and exporting that into a usable image file.

Very useful for making thumbnails from video content, showing specific moments and so on.

Here are some ways on how i managed to do exactly that.

Using ffmpeg to extract a single frame from a specific time in the video

#!/usr/bin/env bash

INPUT_FILEPATH="${1}"
FRAME_TIME="${2}"
OUTPUT_FILEPATH="${3:-${INPUT_FILEPATH//.*/.png}}"

ffmpeg \
-i "${INPUT_FILEPATH}" \
-ss "${FRAME_TIME}" \
-frames:v 1 \
"${OUTPUT_FILEPATH}"

The Bash script takes 3 arguments in total, with the last being optional:

  • The 1. one is the filepath to the video file.
  • The 2. one is the starting time, from which to extract the video frame. The expected input format is HH:MM:SS, meaning hours, minutes and seconds in two digits.
  • The 3. is the output filepath. If not set, will use the input filepath and replace the file extension/suffix with .png.

Using ffmpeg to extract multiple frames from a specific time in the video

#!/usr/bin/env bash

INPUT_FILEPATH="${1}"
FRAME_TIME="${2}"
OUTPUT_FILEPATH="${3:-${INPUT_FILEPATH//.*/.png}}"
FRAME_COUNT="${4:-60}"

ffmpeg \
-i "${INPUT_FILEPATH}" \
-ss "${FRAME_TIME}" \
-frames:v ${FRAME_COUNT} \
"${OUTPUT_FILEPATH}"

The Bash script takes 4 arguments in total, with the third being optional:

  • The 1. one is the filepath to the video file.
  • The 2. one is the starting time, from which to extract the video frame. The expected input format is HH:MM:SS, meaning hours, minutes and seconds in two digits.
  • The 3. is the output filepath. If not set, will use the input filepath and replace the file extension/suffix with .png.
  • The 4. one is the amount of frames to extract from the starting time onwards. If not set, will extract a total of 60.

links

social