gpt4 book ai didi

python - 使用 appsink 时让 gstreamer 管道以读取/解码速度运行

转载 作者:行者123 更新时间:2023-12-01 00:40:13 24 4
gpt4 key购买 nike

如果我创建一个简单的管道来将 mkv 文件读入 fakesink,它的运行速度将与机器读取和处理文件的速度一样快:

$ time gst-launch-1.0 filesrc location=bar.mkv ! matroskademux ! fakesink
Setting pipeline to PAUSED ...
Pipeline is PREROLLING ...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Got EOS from element "pipeline0".
Execution ended after 0:00:00.000402211
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...

real 0m0.020s
user 0m0.016s
sys 0m0.004s

相关文件长度为 5 秒,但运行时间不到一秒。现在,如果我想在程序中拥有相同的功能,并使用 appsink 而不是 fakesink,它会以播放速度运行。考虑这个简单的 Python 程序:

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


def on_message(bus, msg, loop):
if msg.type == Gst.MessageType.EOS:
print("End of stream")
loop.quit()

return True


def new_sample(sink, data):
sample = sink.emit('pull-sample')
return Gst.FlowReturn.OK


def main():
Gst.init(None)

loop = GObject.MainLoop()

pipeline = Gst.Pipeline()
src = Gst.ElementFactory.make('filesrc')
demux = Gst.ElementFactory.make('matroskademux')
sink = Gst.ElementFactory.make(sys.argv[1])

src.set_property('location', 'bar.mkv')

if sys.argv[1] == 'appsink':
sink.set_property('emit-signals', True)
sink.connect('new-sample', new_sample, None)

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

src.link(demux)

def pad_added(element, pad):
stream = pad.query_caps(None).to_string()
pad.link(sink.get_static_pad('sink'))
demux.connect('pad-added', pad_added)

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

pipeline.set_state(Gst.State.PLAYING)
loop.run()
pipeline.set_state(Gst.State.NULL)


if __name__ == '__main__':
main()

如果我用 fakesink 运行它,它仍然非常快:

$ time python ./foo.py fakesink
End of stream

real 0m0.095s
user 0m0.078s
sys 0m0.018s

但是如果我使用 appsink,只需要 5 秒多一点:

$ time python ./foo.py appsink
End of stream

real 0m5.068s
user 0m0.099s
sys 0m0.014s

这表明 gstreamer 正在同步帧,以便它们以文件中指定的帧速率运行。我们可以通过 appsink 获得 fakesink 行为吗?

最佳答案

此行为是通过接收器的 sync 属性控制的。默认情况下,fakesink 将此设置为 false。将此属性设置为 true 可以与 appsink 具有相同的行为。

$ time gst-launch-1.0 filesrc location=bar.mkv ! matroskademux ! fakesink sync=true

关于python - 使用 appsink 时让 gstreamer 管道以读取/解码速度运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57415179/

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