Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract first non black keyframe with FFMPEG

I'm trying to extract a thumbnail image from a video keyframes using ffmpeg, my command line is:

ffmpeg -i video.mp4 -vframes 1 -s 200x200 -vf select="eq(pict_type\,PICT_TYPE_I)" -vsync 0 -f image2 video.jpg

But the keyframe it extracts is totally black (the video starts with a black frame i guess) ... is there a way to automatically extract the first non black keyframe from a video, without seeking to a specific time (i have to manage multiple videos of many durations) ?

Thanks

like image 288
Simone Margaritelli Avatar asked Mar 12 '12 13:03

Simone Margaritelli


1 Answers

I cannot think of a solution using ffmpeg alone. But if you extract the first few keyframes (by turning up to -vframes 20 for example) they could then be analyzed with ImageMagic. Reducing the image to one grayscale color will it with pick the average gray value from the picture. A command line like

convert avatar.jpeg -colors 1 -type grayscale -format '%c' histogram:info:

which will produce an output like

16384: ( 80, 80, 80) #505050 gray(80)

(I used Simone's avatar picture for an example.) The last number is the most interesting for your case. It expresses how dark the image is, with 0 for ideal black and 255 for pure white. A sed script can easily extract it

convert ... | sed 's/^.*(\(.*\))$/\1/'

Mix it up with some shell scripting to find the first image that has a gray value higher than a given threshold and use it as the thumbnail.

like image 112
XZS Avatar answered Jan 04 '23 00:01

XZS