繁体   English   中英

Gstreamer Python:管道未收到 EOS

[英]Gstreamer Python: pipeline not receiving EOS

我是 Gstreamer 的新手。 我正在制作一个简单的管道来记录视频和音频。 管道应运行预定的持续时间,然后随着 EOS 事件正确停止。

我检查了我的日志,发现 EOS 每次都被正确触发。 但是,有时管道不会停止并永远运行。 问题是什么?

我的代码:

import sys

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

# Initializes Gstreamer, it's variables, paths
Gst.init(sys.argv)
GObject.threads_init()


def end_recording(_a, _b, _c, pipeline):
    logger.info('Timer fired, sending EOS...')
    pipeline.send_event(Gst.Event.new_eos())
    return False


def handle_gstreamer_message(_bus: Gst.Bus, message: Gst.Message, loop: GObject.MainLoop):
    """Show ERROR, WARN or message during the capture."""
    message_type = message.type
    # GStreamer Message Types and how to parse
    # https://lazka.github.io/pgi-docs/Gst-1.0/flags.html#Gst.MessageType
    if message_type == Gst.MessageType.EOS:
        logger.info('End of stream')
        loop.quit()
    elif message_type == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        logger.error(err)
        logger.debug(debug)
        loop.quit()
    elif message_type == Gst.MessageType.WARNING:
        err, debug = message.parse_warning()
        logger.error(err)
        logger.debug(debug)
    return True


def capture(
        output_path: str,
        frame_height: int = 0,
        frame_width: int = 0,
        record_time_seconds: int = 0,
) -> int:

    return_code = os.EX_OK
    loop = None
    pipeline = (
        f'v4l2src'
        f' ! videorate'
        f' ! video/x-raw, height={frame_height}, width={frame_width}, framerate={fps}/1'
        f' ! nvvidconv'
        f' ! omxh264enc'
        f' ! mux. '
        f'pulsesrc device={audio_device}'
        f' ! audio/x-raw, rate=44100, channels=2, width=32, depth=32'
        f' ! queue max-size-buffers=0 max-size-time=0 max-size-bytes=0'
        f' ! lamemp3enc bitrate=256'
        f' ! mux. '
        f'qtmux name=mux'
        f' ! filesink location={output_path}'
    )
    logger.debug(pipeline)

    try:
        pipeline = Gst.parse_launch(pipeline)
        bus = pipeline.get_bus()
        clock = pipeline.get_pipeline_clock()
        recording_end_time = clock.get_time() + record_time_seconds * 1000000000
        trigger = Gst.SystemClock.new_single_shot_id(clock, recording_end_time)
        Gst.Clock.id_wait_async(trigger, end_recording, pipeline)

        bus.add_signal_watch()
        pipeline.set_state(Gst.State.PLAYING)
        loop = GObject.MainLoop()
        bus.connect('message', handle_gstreamer_message, loop)

        loop.run()

    except GLib.Error:
        logger.error(f'GLib exception occurred while  capturing {output_path}', exc_info=True)
        return_code = os.EX_CANTCREAT
    except IOError:
        logger.error(f'IO exception occurred while capturing {output_path}', exc_info=True)
        return_code = os.EX_IOERR
    except MemoryError:
        logger.error(f'Memory exception occurred while capturing {output_path}', exc_info=True)
        return_code = os.EX_CANTCREAT
    finally:
        if loop is not None and loop.is_running():
            loop.quit()
        if pipeline is not None:
            pipeline.set_state(Gst.State.NULL)
        if return_code == os.EX_OK:
            logger.info(f'Video saved at {output_path}')
        if return_code != os.EX_OK:
            return return_code

    return return_code

问题似乎来自已弃用的omxh264enc 代码在 NVIDIA Jetson Nano 上运行。 omxh264enc更改为nvv4l2h264enc ,问题就消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM