Search code examples
pythongstreamerpython-gstreamer

Can't save mp4 video after sending eos to pipeline


I have tried many different things but I am not able to send and eos message when ctrl+c is pressed and properly stop my pipeline and obtain a video file. Whay am I missing here?

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)


Solution

  • I finally solved my problem. Answer was in this thread https://github.com/beetbox/audioread/issues/63.

    So basically I updated my pygobject version to 3.38.0 and used the new constructor GObject.MainLoop.new(None, False) to avoid the default sigint problem.

    After that I replaced the KeyboardInterrupt exception with a signal handler:

    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)
    

    And this worked like a charm!