将eos发送到管道后无法保存mp4视频

Can't save mp4 video after sending eos to pipeline

我尝试了很多不同的方法,但是当按下 ctrl+c 时我无法发送 eos 消息并正确停止我的管道并获取视频文件。为什么我在这里不见了?

def bus_call(bus, message, loop):
    t = message.type
    if t == Gst.MessageType.EOS:
        logger.info("End-of-stream\n")
        loop.quit()
    elif t==Gst.MessageType.WARNING:
        err, debug = message.parse_warning()
        logger.info("Warning: %s: %s\n" % (err, debug))
    elif t == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        logger.info("Error: %s: %s\n" % (err, debug))
        loop.quit()
    return True

# Set up pipeline
logger.info("GStreamer initialization")
GObject.threads_init()
Gst.init(None)

logger.info("Creating Pipeline")
pipeline = Gst.Pipeline()

# create and add elements
....

loop = GObject.MainLoop()
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_call, loop)

logger.info("Starting pipeline")
self.gst_pipeline.set_state(Gst.State.PLAYING)

try:
    loop.run()
except KeyboardInterrupt:
    pipeline.send_event(Gst.Event.new_eos())

pipeline.set_state(Gst.State.NULL)

我终于解决了我的问题。答案在此线程中 https://github.com/beetbox/audioread/issues/63.

所以基本上我将我的 pygobject 版本更新到 3.38.0 并使用新的构造函数 GObject.MainLoop.new(None, False) 来避免默认的 sigint 问题。

之后,我用信号处理程序替换了 KeyboardInterrupt 异常:

signal.signal(
                signal.SIGINT,
                lambda n, f: self.gst_pipeline.send_event(Gst.Event.new_eos()),
            )
    
    loop = GObject.MainLoop()
    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect("message", bus_call, loop)
    
    logger.info("Starting pipeline")
    self.gst_pipeline.set_state(Gst.State.PLAYING)
    
    loop.run()


pipeline.set_state(Gst.State.NULL)

这非常有效!