Extract video frames as collection of images

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 several video frames from a video file, and exporting those into a directory of my choice.

Very useful when making thumbnails from video content, and needing to choose the best one from a certain timespan.

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

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

#!/usr/bin/env bash

INPUT_FILEPATH="${1}"
FRAME_TIME="${2}"
FRAME_COUNT="${3:-60}"
OUTPUT_DIR_FILEPATH="${4:-./extracted_frames/}"
OUTPUT_FILEPATH_TEMPLATE="${OUTPUT_DIR_FILEPATH}/%d.png"

mkdir \
--parents \
"${OUTPUT_DIR_FILEPATH}"

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

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 with exactly two digits.
  • The 3. one is the amount of frames to extract from the starting time onwards. If not set, will extract a total of 60.
  • The 4. is the output directory path. Gets created if it doesn't exist. If not set, will store the extracted frames in ./extracted_frames/ .

The output frames (saved as PNG images) are enumerated in the output directory, starting with 1 as their filename.

links

social