Gstreamer 官方教程 basic-tutorial-7 python版

本文介绍了如何使用Python和Gstreamer库实现音频测试源的多线程复用,将音频流同时用于播放和波形显示。教程详细展示了创建元素、配置、链接和控制Gstreamerpipeline的过程。
摘要由CSDN通过智能技术生成

提供了一个audiotestsrc源,经过tee复用到两路,一路播放声音,一路显示为波形。

中文c程序版见

Basic tutorial 7: Multithreading and Pad Availability - GStreamer中文教程

#!/usr/bin/python3

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


def main():
    # initialize GStreamer
    Gst.init(sys.argv)

    # create the elements
    audio_source = Gst.ElementFactory.make("audiotestsrc", "audio_source")
    tee = Gst.ElementFactory.make("tee", "tee")
    audio_queue = Gst.ElementFactory.make("queue", "audio_queue")
    audio_convert = Gst.ElementFactory.make("audioconvert", "audio_convert")
    audio_resample = Gst.ElementFactory.make("audioresample", "audio_resample")
    audio_sink = Gst.ElementFactory.make("autoaudiosink", "audio_sink")
    video_queue = Gst.ElementFactory.make("queue", "video_queue")
    visual = Gst.ElementFactory.make("wavescope", "visual")
    video_convert = Gst.ElementFactory.make("videoconvert", "video_convert")
    video_sink = Gst.ElementFactory.make("autovideosink", "video_sink")

    # create the empty pipeline
    pipeline = Gst.Pipeline.new("test-pipeline")

    if (not pipeline or not audio_source or not tee or not audio_queue
            or not audio_convert or not audio_resample or not audio_sink
            or not video_queue or not visual or not video_convert
            or not video_sink):
        print("ERROR: Not all elements could be created.")
        sys.exit(1)

    # configure elements
    audio_source.set_property("freq", 215.0)
    visual.set_property("shader", 0)
    visual.set_property("style", 1)

    # link all elements that can be automatically linked because they have
    # always pads
    pipeline.add(audio_source)
    pipeline.add(tee)
    pipeline.add(audio_queue)
    pipeline.add(audio_convert)
    pipeline.add(audio_resample)
    pipeline.add(audio_sink)
    pipeline.add(video_queue)
    pipeline.add(visual)
    pipeline.add(video_convert)
    pipeline.add(video_sink)

    ret = audio_source.link(tee)
    ret = ret and audio_queue.link(audio_convert)
    ret = ret and audio_convert.link(audio_resample)
    ret = ret and audio_resample.link(audio_sink)
    ret = ret and video_queue.link(visual)
    ret = ret and visual.link(video_convert)
    ret = ret and video_convert.link(video_sink)

    if not ret:
        print("ERROR: Elements could not be linked")
        sys.exit(1)

    # manually link the tee, which has "Request" pads
    tee_src_pad_template = tee.get_pad_template("src_%u")
    tee_audio_pad = tee.request_pad(tee_src_pad_template, None, None)
    print(
        "Obtained request pad {0} for audio branch".format(
            tee_audio_pad.get_name()))
    audio_queue_pad = audio_queue.get_static_pad("sink")
    tee_video_pad = tee.request_pad(tee_src_pad_template, None, None)
    print(
        "Obtained request pad {0} for video branch".format(
            tee_video_pad.get_name()))
    video_queue_pad = video_queue.get_static_pad("sink")

    if (tee_audio_pad.link(audio_queue_pad) != Gst.PadLinkReturn.OK
            or tee_video_pad.link(video_queue_pad) != Gst.PadLinkReturn.OK):
        print("ERROR: Tee could not be linked")
        sys.exit(1)

    # one could use link() to link elements with"Reuest" pads automatically
    # instead of manually (see above), as it will internally
    # request the pads
    # tee.link(audio_queue)
    # tee.link(video_queue)

    # start playing
    pipeline.set_state(Gst.State.PLAYING)

    # wait until error or EOS
    terminate = False
    bus = pipeline.get_bus()
    while True:
        try:
            msg = bus.timed_pop_filtered(
                0.5 * Gst.SECOND,
                Gst.MessageType.ERROR | Gst.MessageType.EOS)
            if msg:
                terminate = True
        except KeyboardInterrupt:
            terminate = True

        if terminate:
            break

    pipeline.set_state(Gst.State.NULL)

if __name__ == '__main__':
    main()

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是在Docker容器中执行的一系列命令,用于安装一些软件包和依赖项。具体来说,它执行以下操作: 1. `apt-get clean`:清理apt-get缓存,以释放磁盘空间。 2. `apt-get update`:更新apt-get软件包列表。 3. `apt-get install -y`:安装以下软件包和依赖项: - `python3`:Python 3的主要二进制文件。 - `python3-pip`:Python 3的包管理工具pip。 - `libopencv-dev`:OpenCV开发库的头文件和静态库。 - `python3-opencv`:Python 3的OpenCV绑定。 - `build-essential`:构建软件包所需的基本工具和编译器。 - `yasm`:视频编解码器的汇编器。 - `cmake`:跨平台的构建工具。 - `libtool`:通用库支持脚本工具。 - `libc6`、`libc6-dev`:C标准库的运行时库和开发文件。 - `unzip`:解压缩工具。 - `wget`:网络下载工具。 - `libnuma1`、`libnuma-dev`:NUMA(非统一内存访问)系统的库和开发文件。 - `libgstreamer1.0-0`:GStreamer多媒体框架的核心库。 - `gstreamer1.0-plugins-base`、`gstreamer1.0-plugins-good`、`gstreamer1.0-plugins-bad`、`gstreamer1.0-plugins-ugly`、`gstreamer1.0-libav`:GStreamer插件和解码器。 - `gstreamer1.0-doc`、`gstreamer1.0-tools`、`gstreamer1.0-x`、`gstreamer1.0-alsa`、`gstreamer1.0-gl`、`gstreamer1.0-gtk3`、`gstreamer1.0-qt5`、`gstreamer1.0-pulseaudio`:GStreamer的文档、工具和相关库。 - `libglib2.0-dev`:GLib开发库的头文件。 - `libgstrtspserver-1.0-dev`:GStreamer RTSP服务器库的开发文件。 - `gstreamer1.0-rtsp`:GStreamer的RTSP插件。 这些操作旨在为容器配置一个适合开发的环境,使其能够支持Python编程、OpenCV图像处理和GStreamer多媒体处理等任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值