gpt4 book ai didi

python - Gstreamer 自动插入 : create demux on "have-type"

转载 作者:太空宇宙 更新时间:2023-11-04 04:05:30 25 4
gpt4 key购买 nike

我正在尝试使用 typefind 元素在运行时选择正确的多路分解器类型。如果我在播放前创建解复用器并在收到“have-type”信号后链接它,一切正常。例如:

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
from gi.repository import GObject

def on_message(bus, message, loop):
mtype = message.type

if mtype == Gst.MessageType.EOS:
print("End of stream")
loop.quit()

return True


n = 0
def new_sample(sink, data):
global n
n += 1
#print('new-sample')
sample = sink.emit('pull-sample')
return Gst.FlowReturn.OK


Gst.init(None)

loop = GObject.MainLoop()

pipeline = Gst.Pipeline()
src = Gst.ElementFactory.make('splitfilesrc')
typefind = Gst.ElementFactory.make('typefind')
demux = Gst.ElementFactory.make('avidemux')
sink = Gst.ElementFactory.make('appsink')

src.set_property('location', sys.argv[1])

pipeline.add(src)
pipeline.add(typefind)
pipeline.add(demux)
pipeline.add(sink)

if not src.link(typefind):
print('Could not link src to typefind.')
exit(1)

def demux_pad_added(element, pad):
stream = pad.query_caps(None).to_string()
print('Found stream: {}'.format(stream))
result = pad.link(sink.get_static_pad('sink'))
if result != Gst.PadLinkReturn.OK:
print()
print('Could not link demux to sink.')
loop.quit()

sink.set_property('emit-signals', True)
sink.set_property('sync', False)
sink.connect('new-sample', new_sample, None)

def have_type(typefind, probability, caps):
print('have-type:', caps.to_string())
demux.connect('pad-added', demux_pad_added)
if not typefind.link(demux):
print('Could not link typefind to demux.')
exit(1)
typefind.connect('have-type', have_type)

bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message', on_message, loop)

pipeline.set_state(Gst.State.PLAYING)
try:
loop.run()
except KeyboardInterrupt:
print()
pipeline.set_state(Gst.State.NULL)
print('total:', n)

如果我运行它,我会得到类似的东西:

$ python foo.py foo.avi
have-type: video/x-msvideo
Found stream: video/x-h264, variant=(string)itu, framerate=(fraction)50/1, max-input-size=(int)1048576, width=(int)1728, height=(int)3072, stream-format=(string)byte-stream,
alignment=(string)au
^C
total: 12144

现在,如果我在“have-type”回调中移动 demux 创建:

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst
from gi.repository import GObject

def on_message(bus, message, loop):
mtype = message.type

if mtype == Gst.MessageType.EOS:
print("End of stream")
loop.quit()

return True


n = 0
def new_sample(sink, data):
global n
n += 1
#print('new-sample')
sample = sink.emit('pull-sample')
return Gst.FlowReturn.OK


Gst.init(None)

loop = GObject.MainLoop()

pipeline = Gst.Pipeline()
src = Gst.ElementFactory.make('splitfilesrc')
typefind = Gst.ElementFactory.make('typefind')
#demux = Gst.ElementFactory.make('avidemux')
sink = Gst.ElementFactory.make('appsink')

src.set_property('location', sys.argv[1])

pipeline.add(src)
pipeline.add(typefind)
#pipeline.add(demux)
pipeline.add(sink)

if not src.link(typefind):
print('Could not link src to typefind.')
exit(1)

def demux_pad_added(element, pad):
stream = pad.query_caps(None).to_string()
print('Found stream: {}'.format(stream))
result = pad.link(sink.get_static_pad('sink'))
if result != Gst.PadLinkReturn.OK:
print()
print('Could not link demux to sink.')
loop.quit()

sink.set_property('emit-signals', True)
sink.set_property('sync', False)
sink.connect('new-sample', new_sample, None)

def have_type(typefind, probability, caps):
print('have-type:', caps.to_string())
demux = Gst.ElementFactory.make('avidemux')
pipeline.add(demux)
demux.connect('pad-added', demux_pad_added)
if not typefind.link(demux):
print('Could not link typefind to demux.')
exit(1)
typefind.connect('have-type', have_type)

bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message', on_message, loop)

pipeline.set_state(Gst.State.PLAYING)
try:
loop.run()
except KeyboardInterrupt:
print()
pipeline.set_state(Gst.State.NULL)
print('total:', n)

注意只有两条 demux = ...pipeline.add(demux) 行被移动到回调中,但这个管道似乎没有做任何东西:

$ python foo.py foo.avi
have-type: video/x-msvideo
^C
total: 0

我可能可以事先创建所有可能的多路分解器,然后在回调中链接我想要的多路分解器,但我想知道为什么这不起作用,我是否可以让它像这样工作?

最佳答案

这里是小细节。由于您延迟将多路分解器添加到管道,因此它仍处于 NULL 状态,因为它没有从管道接收到 PLAYING 的状态更改。它需要处于暂停/播放状态才能真正执行任何操作。

如果我没记错的话,如果你修改你的代码让你的解复用器在连接后进入 PLAYING 状态,它应该可以工作......:

    [..]
demux = Gst.ElementFactory.make('avidemux')
pipeline.add(demux)
demux.connect('pad-added', demux_pad_added)
if not typefind.link(demux):
print('Could not link typefind to demux.')
exit(1)
demux.set_state(Gst.State.PLAYING)
typefind.connect('have-type', have_type)
[..]

关于python - Gstreamer 自动插入 : create demux on "have-type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com