Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

FFmpeg can capture images from videos that can be used as thumbnails to represent the video. Most common ways of doing that are captured in the FFmpeg Wiki.

But, I don't want to pick random frames at some intervals. I found some options using filters on FFmpeg to capture scene changes:

The filter thumbnail tries to find the most representative frames in the video:

ffmpeg -i input.mp4 -vf  "thumbnail,scale=640:360" -frames:v 1 thumb.png

and the following command selects only frames that have more than 40% of changes compared to previous (and so probably are scene changes) and generates a sequence of 5 PNGs.

ffmpeg -i input.mp4 -vf  "select=gt(scene\,0.4),scale=640:360" -frames:v 5 thumb%03d.png

Info credit for the above commands to Fabio Sonnati. The second one seemed better as I could get n images and pick the best. I tried it and it generated the same image 5 times.

Some more investigation led me to:

ffmpeg -i input.mp4 -vf "select=gt(scene\,0.5)" -frames:v 5 -vsync vfr  out%02d.png

-vsync vfr ensures that you get different images. This still always picks the first frame of the video, in most cases the first frame is credits/logo and not meaningful, so I added a -ss 3 to discard first 3 seconds of the video.

My final command looks like this:

ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.5)" -frames:v 5 -vsync vfr out%02d.jpg

This was the best I could do. I have noticed that since I pick only 5 videos , all of them are mostly from beginning of the video and may miss out on important scenes that occur later in the video

I would like to pick your brains for any other better options.

share|improve this question
    
Nice command examples. FWIW, I didn't run into any issues with FFmpeg-generated JPEG pictures on OS X (10.8, FFmpeg 1.1 and below). Your second to last command works fine for me—so does the last—and none of these results in blank JPG files. I did compile with libopenjpeg.. not sure if that makes a difference. –  slhck Jan 18 '13 at 8:46
    
Thanks slhck. Edited the question with ffmpeg config/version details. I have not upgraded to 1.1 on this machine. I will do that and see if it changes any results. –  d33pika Jan 18 '13 at 8:51
1  
So you're on Ubuntu? Can you try the latest Git Master version from a static build or compiling yourself and running again? Or the latest stable. I just checked, it uses the mjpeg encoder for me as well, and I also checked jpegoptim and exiv2, both of which work fine for me with all the JPG results from your example commands. –  slhck Jan 18 '13 at 8:59
1  
I updated, and it works now! I guess the previous version had some bugs. –  d33pika Jan 18 '13 at 9:37
    
Can you go ahead and post the solution- new version, preferably with link to changelog showing the bug you encountered and subsequently fixed with new version? –  Lizz Mar 16 '13 at 7:15
show 1 more comment

2 Answers

up vote 4 down vote accepted

How about looking for, ideally, the first >40%-change frame within each of 5 time spans, where the time spans are the 1st, 2nd, 3rd, 4th, and 5th 20% of the video.

...and maybe you could split it into 6 time spans and disregard the 1st one to avoid credits.

In practice, this would probably mean setting the fps to a low number while also applying your scene change check and your argument to throw out the first bit of the video.

...something like:

ffmpeg -ss 3 -i input.mp4 -vf "select=gt(scene\,0.4)" -frames:v 5 -vsync vfr fps=fps=1/600 out%02d.jpg
share|improve this answer
    
Yes, this does help in picking thumbnails from different sections of the Video. thanks! –  d33pika Jul 18 '13 at 5:56
    
Completely black or white images get picked up.How do I avoid those? –  d33pika Jul 18 '13 at 5:57
    
There are filters to detect black frames and sequences of them (ffmpeg.org/ffmpeg-filters.html#blackframe or ffmpeg.org/ffmpeg-filters.html#blackdetect). You make not be able to work either of them into your growing one-liner, but you should definitely be able to strip out the black frames (separate step) and extract thumbnails from the resulting video. –  A.M. Jul 18 '13 at 12:05
2  
As for white frames, well now it's getting complicated, but it still looks like there is a way: 1. strip out black frames 2. negate (white turns to black) 3. strip out white frames 4. negate again 5. extract thumbnails (If you do manage to strip out the black or white frames, especially on one line, can you post it back here? I can add it to the answer for future readers. ...or actually you could create a new question and answer it. I would definitely upvote it.) –  A.M. Jul 18 '13 at 12:09
    
Would it be possible to also capture a few seconds worth of frames after the beginning of each selected scene? –  Liam Aug 20 '13 at 17:09
add comment

I once did something similar, but I exported all frames of the video (in 1 fps) and compared them with a perl utility I found which computes the difference between images. I compared each frame to previous thumbnails, and if it was different from all thumbnails, I added it to the thumbnails collection. The advantage here is that if your video moves from scene A to B and them returns to A, ffmpeg will export 2 frames of A.

share|improve this answer
    
What factors did you use to compare 2 images? –  d33pika Mar 18 '13 at 2:09
    
Unfortunately I don't remember, it was quite a long ago. You need to decide first which compare method to use, and then do some tests to find correct factor. This shouldn't take long. –  Eran Ben-Natan Mar 18 '13 at 9:11
    
I'm just thinking out loud here but isn't this worse option? When you compare 2 exported images from video you've already lost some information from it. I mean, it is easier to calculate similarity from codec information than it is from 2 pictures, isn't it? Recently I've been looking into some algorithms / libraries to compare images and they don't work as well as one may require. –  Samuel Oct 28 '13 at 10:03
    
Well, with ffmpeg you can extract frames without quality loss using the same_quality flag. If you use codec information you need to check that you don't just get the Iframes. Anyway that perl utility worked just fine for me, and is very configurable. Look here: search.cpan.org/~avif/Image-Compare-0.3/Compare.pm –  Eran Ben-Natan Oct 29 '13 at 11:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.