如何使用Python和Gstreamer创建视频缩略图

Dav*_*lla 9 python video gstreamer

我想使用Gstreamer和Python为MPEG-4 AVC视频创建缩略图.实质上:

  1. 打开视频文件
  2. 寻找某个时间点(例如5秒)
  3. 那时抓住框架
  4. 将帧保存为.jpg文件

我一直在看这个类似的问题,但我无法弄清楚如何在没有用户输入的情况下自动进行搜索和帧捕获.

总而言之,我如何按照上述步骤使用Gstreamer和Python捕获视频缩略图?

daf*_*daf 8

要详细说明ensonic的答案,这是一个例子:

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()
Run Code Online (Sandbox Code Playgroud)

这会生成PNG图像.您可以使用gst.Caps("video/x-raw-rgb,bpp=24,depth=24")或类似的方式获取原始图像数据.

请注意,在GStreamer 1.0(而不是0.10)中,playbin2已重命名为playbin并且convert-frame信号已命名convert-sample.

GStreamer应用程序开发手册的这一章解释了搜索的机制.0.10 playbin2文档似乎不再在线,但1.0的文档就在这里.