Can not get right data from appsink in python
black
wangyawei_dlut at 163.com
Fri Jan 12 04:13:11 UTC 2018
OK I got it. I can use signal callback function to get the stream data. like below:
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstApp', '1.0')
from gi.repository import GObject, Gst, GstApp
GObject.threads_init()
Gst.init(None)
class Example:
def __init__(self):
self.mainloop = GObject.MainLoop()
self.pipeline = Gst.Pipeline()
self.bus = self.pipeline.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message::eos', self.on_eos)
self.bus.connect('message::error', self.on_error)
# Create elements
self.src = Gst.ElementFactory.make('v4l2src', None)
self.encoder = Gst.ElementFactory.make('x264enc', None)
self.parse = Gst.ElementFactory.make('h264parse', None)
self.mux = Gst.ElementFactory.make('flvmux', None)
self.sink = Gst.ElementFactory.make('appsink', None)
# Add elements to pipeline
self.pipeline.add(self.src)
self.pipeline.add(self.encoder)
self.pipeline.add(self.parse)
self.pipeline.add(self.mux)
self.pipeline.add(self.sink)
# Set properties
self.src.set_property('device', "/dev/video0")
self.sink.set_property('emit-signals', True)
# turns off sync to make decoding as fast as possible
self.sink.set_property('sync', False)
self.sink.connect('new-sample', self.on_new_buffer)
# Link elements
self.src.link(self.encoder)
self.encoder.link(self.parse)
self.parse.link(self.mux)
self.mux.link(self.sink)
def on_new_buffer(self, appsink):
appsink_sample = GstApp.AppSink.pull_sample(self.sink)
# with open('example.h264', 'a+') as streamer:
buff = appsink_sample.get_buffer()
size, offset, maxsize = buff.get_sizes()
frame_data = buff.extract_dup(offset, size)
print(size)
# flag, info = buff.map(Gst.MapFlags.READ)
# streamer.write(info.data)
# print(info.size)
return False
def run(self):
self.pipeline.set_state(Gst.State.PLAYING)
self.mainloop.run()
def kill(self):
self.pipeline.set_state(Gst.State.NULL)
self.mainloop.quit()
def on_eos(self, bus, msg):
print('on_eos()')
self.kill()
def on_error(self, bus, msg):
print('on_error():', msg.parse_error())
self.kill()
example = Example()
example.run()
At 2018-01-12 11:42:50, "black" <wangyawei_dlut at 163.com> wrote:
How I get the continue stream data? Coulud you give me a support?
At 2018-01-12 11:10:14, "Matthew Waters" <ystreet00 at gmail.com> wrote:
>On 12/01/18 12:56, Yawei Wang wrote:
>> I use python to get data from appsink. My code is like below:
>> ```
>> import gi
>> gi.require_version('Gst', '1.0')
>> gi.require_version('GstApp', '1.0')
>> from gi.repository import GObject, Gst, GstApp
>>
>> GObject.threads_init()
>> Gst.init(None)
>>
>>
>> class Example:
>> def __init__(self):
>> self.mainloop = GObject.MainLoop()
>> self.pipeline = Gst.Pipeline()
>> self.bus = self.pipeline.get_bus()
>> self.bus.add_signal_watch()
>> self.bus.connect('message::eos', self.on_eos)
>> self.bus.connect('message::error', self.on_error)
>>
>> # Create elements
>> self.src = Gst.ElementFactory.make('v4l2src', None)
>> self.encoder = Gst.ElementFactory.make('x264enc', None)
>> self.parse = Gst.ElementFactory.make('h264parse', None)
>> self.mux = Gst.ElementFactory.make('flvmux', None)
>> self.sink = Gst.ElementFactory.make('appsink', None)
>>
>> # Add elements to pipeline
>> self.pipeline.add(self.src)
>> self.pipeline.add(self.encoder)
>> self.pipeline.add(self.parse)
>> self.pipeline.add(self.mux)
>> self.pipeline.add(self.sink)
>>
>> # Set properties
>> self.src.set_property('device', "/dev/video0")
>>
>> # Link elements
>> self.src.link(self.encoder)
>> self.encoder.link(self.parse)
>> self.parse.link(self.mux)
>> self.mux.link(self.sink)
>>
>> def run(self):
>> self.pipeline.set_state(Gst.State.PLAYING)
>> # self.mainloop.run()
>> appsink_sample = GstApp.AppSink.pull_sample(self.sink)
>
>This needs to be in the while loop below.
>
>What the code currently says is:
>1. Pull one sample
>2. While true, get the buffer from the sample (this will always return
>the same buffer).
>
>
>What you want is to pull_sample() inside the while loop
>> while True:
>> buff = appsink_sample.get_buffer()
>> size, offset, maxsize = buff.get_sizes()
>> frame_data = buff.extract_dup(offset, size)
>> print(frame_data)
>>
>> def kill(self):
>> self.pipeline.set_state(Gst.State.NULL)
>> self.mainloop.quit()
>>
>> def on_eos(self, bus, msg):
>> print('on_eos()')
>> self.kill()
>>
>> def on_error(self, bus, msg):
>> print('on_error():', msg.parse_error())
>> self.kill()
>>
>>
>> example = Example()
>> example.run()
>> ```
>>
>> But I got the same data like "FLV\0x01\0x01". Someone could answer the
>> question?
>>
>>
>>
>> --
>> Sent from: http://gstreamer-devel.966125.n4.nabble.com/
>> _______________________________________________
>> gstreamer-devel mailing list
>> gstreamer-devel at lists.freedesktop.org
>> https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/gstreamer-devel/attachments/20180112/5d5b81b4/attachment.html>
More information about the gstreamer-devel
mailing list