GStreamer How to extract video frame from the flow

2019-04-14 01:30发布

问题:

This is python code for capturing streaming video from server. but I need to write a function to extract one frame from the flow. It will be a button. On click it will show current frame. I have no ideas. Can anyone help me with this???

    self.player = gst.Pipeline("player")
    self.source = gst.element_factory_make("uridecodebin", "video-source")
    #self.source = gst.element_factory_make("playbin2", "video-source")
    sink = gst.element_factory_make("xvimagesink", "video-output")
    colorspace = gst.element_factory_make("ffmpegcolorspace")
    scale = gst.element_factory_make("videoscale")

    self.source.set_property("uri",\
    "http://10.10.25.4:12345/webcam.flv")

    caps = gst.Caps("video/x-raw-yuv, width=640, height=480, framerate=20/1")
    myfilter = gst.element_factory_make("capsfilter", "myfilter")
    myfilter.set_property("caps", caps)  # ################

    clr_sink = colorspace.get_pad("sink")
    self.source.connect("pad-added", self.on_pad_added, clr_sink)

    self.player.add(self.source, colorspace, scale, myfilter, sink)
    gst.element_link_many(colorspace, scale, myfilter, sink)

    self.bus = self.player.get_bus()
    self.bus.add_signal_watch()
    self.bus.connect('message', self.__on_message)

    self.player.set_state(gst.STATE_PLAYING)

回答1:

you want to use the imagefreeze element. something like:

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst

player = gst.Pipeline("player")
source = gst.element_factory_make("videotestsrc", "testsource")
effect = gst.element_factory_make("clockoverlay", "clock")
freeze = gst.element_factory_make("imagefreeze", "freeze")
colorspace = gst.element_factory_make("ffmpegcolorspace", "colorspace")
sink = gst.element_factory_make("ximagesink", "imagesink")

player.add(source, effect, freeze, colorspace, sink)
gst.element_link_many(source, effect, freeze, colorspace, sink)
player.set_state(gst.STATE_PLAYING)

while True:
  inp = raw_input("Press enter:")
  player.set_state(gst.STATE_READY)
  player.set_state(gst.STATE_PLAYING)

whenever you hit "enter" in the console a new screenshot will be taken (from the videotest with clockoverlay) and displayed.



回答2:

If you can use playbin2, you can use the "convert-frame" action signal. Otherwise look at the implementation and reuse.