使用ffmeg提取视频关键帧

x33g5p2x  于2021-11-18 转载在 其他  
字(2.4k)|赞(0)|评价(0)|浏览(311)

一. 什么是关键帧

参考维基百科Key frame的解释:
  关键帧,是指动画中一个绘图,定义任何的起点和终点平滑过渡。一系列关键帧定义了观看者将看到的运动,而关键帧在电影,视频或动画上的位置定义了运动的时间。

Computer Hope 的这篇文章 What is a Key Frame?说的更好理解:

In media production, a key frame or keyframe is a location on a timeline which marks the beginning or end of a transition. It holds special information that defines where a transition should start or stop. The intermediate frames are interpolated(插值) over time between those definitions to create the illusion of motion. In computer animation, like 3D animation or non-linear video editing, this interpolation is performed mathematically by the CPU.
  
In the following screenshot from Adobe After Effects, the keyframes are highlighted with red arrows. Each of the keyframes defines a position of the animated logo (first on the left, and then one second later on the right). When the animation is played, the logo transitions smoothly from the start position to the end position. Key frames can be used in any time-based media production process where transitions are defined, including audio production and high-end digital video compression.
           

二. 提取关键帧

通过ffmpeg提取关键帧,参考 视频关键帧提取 :

ffmpeg -i video_name.mp4 -vf select='eq(pict_type\,I)' -vsync 2 -s 1920*1080 -f image2 keyframe-%02d.jpeg

各参数解释:
-i :输入文件,这里的话其实就是视频;
-vf:是一个命令行,表示过滤图形的描述。选择过滤器select会选择帧进行输出:pict_type和对应的类型:PICT_TYPE_I 表示是I帧,即关键帧;
-vsync 2:阻止每个关键帧产生多余的拷贝;
-f image2 name_%02d.jpeg:将视频帧写入到图片中,样式的格式一般是: “%d” 或者 “%0Nd”
-s:分辨率,1920*1080

这样保存下来的关键帧的命名顺序是从1开始的,数字表示第几个关键帧。
若需要保存关键帧在原始视频中所有帧的位置,参考Extracting the index of key frames from a video using ffmpeg中提到的命令:

ffprobe -i text.mp4 -select_streams v -show_frames -show_entries frame=pict_type -of csv | grep -n I | cut -d ':' -f 1 > frame_index.txt

-i 后面跟输入文件,这里就是视频;
-select_streams v 表示只选择查看音频流的信息;
-show_frames 表示显示视频帧信息;
-show_entries frame=pict_type 表示从frame section把pict_type的entry过滤出来;
-of csv 表示输出格式是csv,相当于 -print_format csv;
grep -n I 表示编号并过滤出I帧;
cut -d ‘:’ -f 1 表示以’:'作为分隔符,输出第一项。

然后会生成一个 frame_index.txt 的文件。其中,保存的即为关键帧在视频中的帧的索引位置,例如:

1
110
240
390
517
613
763
913
1063

然后再将生成的关键帧与索引对应起来:

ls -1 keyframe*.jpeg | awk -F ' ' '{print$9}' > keyframe.txt
paste frame_index.txt  keyframe.txt> result.txt

生成的 result.txt 格式为:关键帧在所有帧中的序号 提取出来的关键帧名字。例如,

1	thumbnails-01.jpeg
110	thumbnails-02.jpeg
240	thumbnails-03.jpeg
390	thumbnails-04.jpeg
517	thumbnails-05.jpeg
613	thumbnails-06.jpeg
763	thumbnails-07.jpeg
913	thumbnails-08.jpeg
1063	thumbnails-09.jpeg
1213	thumbnails-10.jpeg

引用

视频关键帧提取 https://blog.csdn.net/qingyuanluofeng/article/details/45375647
ffmpeg 提取关键帧, https://blog.csdn.net/u011394059/article/details/78728809
What is a Key Frame? https://www.computerhope.com/jargon/k/key-frame.htm

相关文章

微信公众号

最新文章

更多