如何使用Python和Gstreamer创建视频缩略图 [英] How to create video thumbnails with Python and Gstreamer
问题描述
我想使用Gstreamer和Python为MPEG-4 AVC视频创建缩略图.本质上:
I'd like to create thumbnails for MPEG-4 AVC videos using Gstreamer and Python. Essentially:
- 打开视频文件
- 搜索到某个时间点(例如5秒)
- 当时抓取框架
- 将框架另存为.jpg文件
我一直在查看这另一个类似的问题,但是我不能完全弄清楚如何在没有用户输入的情况下自动执行搜索和帧捕获.
I've been looking at this other similar question, but I cannot quite figure out how to do the seek and frame capture automatically without user input.
因此,总而言之,如何按照上述步骤使用Gstreamer和Python捕获视频缩略图?
So in summary, how can I capture a video thumbnail with Gstreamer and Python as per the steps above?
推荐答案
要详细说明 ensonic 的答案,这是一个例子:
To elaborate on ensonic's answer, here's an example:
import os
import sys
import gst
def get_frame(path, offset=5, caps=gst.Caps('image/png')):
pipeline = gst.parse_launch('playbin2')
pipeline.props.uri = 'file://' + os.path.abspath(path)
pipeline.props.audio_sink = gst.element_factory_make('fakesink')
pipeline.props.video_sink = gst.element_factory_make('fakesink')
pipeline.set_state(gst.STATE_PAUSED)
# Wait for state change to finish.
pipeline.get_state()
assert pipeline.seek_simple(
gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, offset * gst.SECOND)
# Wait for seek to finish.
pipeline.get_state()
buffer = pipeline.emit('convert-frame', caps)
pipeline.set_state(gst.STATE_NULL)
return buffer
def main():
buf = get_frame(sys.argv[1])
with file('frame.png', 'w') as fh:
fh.write(str(buf))
if __name__ == '__main__':
main()
这将生成一个PNG图像.您可以使用gst.Caps("video/x-raw-rgb,bpp=24,depth=24")
或类似的方法获取原始图像数据.
This generates a PNG image. You can get raw image data using gst.Caps("video/x-raw-rgb,bpp=24,depth=24")
or something like that.
请注意,在GStreamer 1.0(而不是0.10)中,playbin2
已重命名为playbin
,并且convert-frame
信号被命名为convert-sample
.
Note that in GStreamer 1.0 (as opposed to 0.10), playbin2
has been renamed to playbin
and the convert-frame
signal is named convert-sample
.
GStreamer应用程序开发手册的这一章. 0.10 playbin2
文档似乎不再在线,但是1.0的文档是
The mechanics of seeking are explained in this chapter of the GStreamer Application Development Manual. The 0.10 playbin2
documentation no longer seems to be online, but the documentation for 1.0 is here.
这篇关于如何使用Python和Gstreamer创建视频缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!