8

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

Simone Margaritelli
  • 4,584
  • 10
  • 45
  • 70

2 Answers2

1

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.

XZS
  • 2,374
  • 2
  • 19
  • 38
0

With the option thumbnail=num_frame you can choose when you extract a frame, but I don't know if it is possible extract the first non balck keyframe. http://ffmpeg.org/ffmpeg.html#thumbnail

Álvaro
  • 2,872
  • 4
  • 20
  • 25