Monday, May 26, 2014

How to automate video screen grabs using ffmpeg

Part 1 of this 2-part series explains how to use VLC, a GUI-based video player, to capture single still frames from a video file. If you are taking many snapshots, you may find the manual VLC method too laborious. This post shows how to automate the capture process using ffmpeg, a command-line tool.

Extract frame at a given instant

The first scenario is to simply take a single snapshot at a given time instant.

$ ffmpeg -i P5080821.MP4 -vframes 1 -ss 00:01:39 $(date +%Y%m%d-%H%M%S).png

Notes:

  • -i

    P5080821.MP4 is the input video file.

  • -vframes

    The number of frames or snapshots to capture is 1.

  • -ss

    Skip ahead the input video for the given time interval from the start of the video. The time interval can be specified in the number of seconds, or in the "hh:mm:ss[.xxx]" format. The above example takes a snapshot at 1 minute 39 seconds from the start.

  • PNG file name

    The name of the output file contains the file creation timestamp formatted using the date command.

Auto extract multiple frames

Suppose you want to extract a series of frames taken at a regular time interval starting from a specific time instant. The following example extracts one frame per second starting at 1 minute 39 seconds into the video.

$ ffmpeg -i P5080821.MP4 -r 1 -ss 00:01:39 example-%03d.png

Notes:

  • -r

    This is the frame rate per second (e.g., 1 per second).

  • %03d

    The output files are sequentially numbered starting at 001. Output files from the above example will be named example-001.png, example-002.png, example-003.png, etc.

If the video is long, you may end up with too many pictures. You may limit the number of frames by specifying the absolute number of frames (-vframes) to extract or the absolute time duration (-t).

$ ffmpeg -i P5080823.MP4 -r 1 -ss 00:01:39 -t 00:02:00 example-%03d.png

In the above example, snapshots are taken at a rate of 1 frame per second, starting at 1 minute 39 seconds into the video, and only for a duration of 2 minutes.

No comments: