Extract keyframes from a video

I need Need to Extract key frames from a video / stream. Is there any standard implementation. I use an open resume. (I am currently retrieving frames every second, which is slower, I need to improve performance.) Therefore, if someone has an optimized implementation, answer here.

+10
source share
5 answers

Using ffmpeg, you can extract all key frames using the following code:

ffmpeg -vf select="eq(pict_type\,PICT_TYPE_I)" -i yourvideo.mp4 -vsync 2 -s 160x90 -f image2 thumbnails-%02d.jpeg 

What follows - vf at the ffmpeg command line is a description of Filtergraph. A selection filter selects frames for output. The filter constant is pict_type and the value is PICT_TYPE_I. Thus, ffmpeg only passes keyframes to output.

-vsync 2 prevents ffmpeg from creating more than one copy for each keyframe.

-f image2 writes video frames to image files. The output file names are determined by a pattern that can be used to create sequentially numbered series of files. The template may contain the string "% d" or "% 0Nd".

Link: http://www.videoproductionslondon.com/blog/scene-change-detection-during-encoding-key-frame-extraction-code

+16
source

I assume that the keyframe is a frame representing content that is very different from the previous ones (this is not a formal definition, but it does). Take frames me and I + 1. Use cv2.absDiff to calculate the difference between frames, and cv2.sumElems to get the sum of all the pixel differences. Do it for all frames i. This will reduce your video stream to a one-dimensional signal. Look for peaks in this signal and select keyframes corresponding to these peaks. To find peaks, select the threshold value for this signal either manually, finding the frame that you consider to be the key, and assuming that its error is the threshold error value, or automatically using statistics (for example, any frame I + 1, where the error exceeds 1 standard deviation from the average error).

+7
source

If something is wrong with the code above, try this argument instead.

 ffmpeg -i yourVideo.mp4 -vf select='eq(pict_type\,I)' -vsync 2 -s 160x90 -f image2 thumbnails-%02d.jpeg 
+2
source

Ffmpeg solution should work well.

For someone who is having problems with the selection filter "eq (pict_type \, PICT_TYPE_I)", you can try the filter like "eq (pict_type \, I)". This was interrupted for some time, so some versions of ffmpeg might not recognize the constant. The correction can be seen here .

The last team that worked for me was:

 ffmpeg -vf select='eq(pict_type\,I)' -i yourVideo.mp4 -vsync 2 -s 160x90 -f image2 thumbnails-%02d.jpeg 
+1
source

You can use ffprobe to extract keyframes. This is a tool in ffmpeg.

Use the command:

 ffprobe in.mp4 -select_streams v -show_entries frame=key_frame,pkt_pts_time -of csv=nk=1:p=0 | findstr "1," 
0
source

All Articles