Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract list of specific frames using ffmpeg

I'm trying to use ffmpeg on a video to extract a list of specific frames, denoted by their frame numbers. So lets say I want to extract just one frame from 'test_video.mp4', frame number 150 to be exact. I can use the following command

ffmpeg -i test_video.mp4 -vf "select=gte(n\, 150)" -vframes 1 ~/test_image.jpg

But what if I want to exact a list of frames, such as [100, 110, 127, 270, 300]? I have been looking at this page (https://ffmpeg.org/ffmpeg-filters.html#select_002c-aselect) and can see that there are ways to extract multiple frames based on parity of frame number, timestamps, etc, but I can't find the syntax for doing what I need to do.

Ideally I would extract the list of given frames under the naming convention out_image%03d.jpg where the %03d is replaced by the given frame number, although a timestamp would also work.

Is there a way to do this?

like image 756
John Allard Avatar asked Jul 07 '16 19:07

John Allard


People also ask

What is N Ffmpeg?

select selects frames, n is the number of the frame (starting from 0), mod is modulo, not negates the modulo since a zero result means discard the frame.


2 Answers

Use

ffmpeg -i in.mp4 -vf select='eq(n\,100)+eq(n\,184)+eq(n\,213)' -vsync 0 frames%d.jpg

FFmpeg is primarily a processor of timed video i.e. media with a cycle rate such as framerate or sample rate. It assumes that the output should be at the same rate as the source. If inadequate frames are supplied, it duplicates unless told not to. -vsync 0 is added which, in this case, tells it to suppress duplication.

like image 119
Gyan Avatar answered Oct 07 '22 08:10

Gyan


A very naive answer...

#! /bin/sh -e
# framegrab v0.1

frames="$1"
filename="$2"
printf '%s' "$frames" |
sed 's:\[\|\]::g; s:[, ]\+:\n:g' |
xargs printf '%03d\n' |
xargs -IFRAME ffmpeg -i "$filename" -vf "select=eq(n\,FRAME)" -vframes 1 out_imageFRAME.jpg

Call it with your argument list:

 framegrab '[100, 110, 127, 270, 300]' test_video.mp4
like image 41
Michael Back Avatar answered Oct 07 '22 06:10

Michael Back