首页 > 解决方案 > 如何使用 Gstreamer 将实时音频和视频混合到 mpegts?

问题描述

目标:

在 Raspberry Pi 上,从连接的摄像头和麦克风获取输入,合并为单个流,使用 VLC 应用程序跨网络播放。

更多详细信息:
我正在尝试使用 Raspberry Pi Zero W、USB 麦克风和通过 Pi 的摄像头端口连接的摄像头来制作相当于 IP 摄像头的东西。这个想法是让实时音频和视频只能在我的本地网络上从任何可以运行 VLC 播放器的设备上查看。

视频正在运行

我决定使用 Gstreamer 的命令行工具来构建这个应用程序,因为据报道它的延迟比使用类似cvlcffmpeg. 根据此链接中的代码,我构建了一个仅可工作的视频管道。

#!/bin/bash

dstAdr=${1:-192.168.0.255}

raspivid -n -w 640 -h 480 -fps 20 -b 2000000 -a 12 -t 0 -o - \
       | gst-launch-1.0 -v fdsrc \
       ! video/x-h264, width=640, height=480, framerate=20/1 \
       ! h264parse config-interval=-1 \
       ! mpegtsmux \
       ! udpsink host=$dstAdr port=5004

udp://@:5004通过在 VLC 中打开网络流,可以从我的 PC 和智能手机查看此流。

如何包含音频?

我有一个 USB 麦克风,我希望用它来捕获实时音频并将其包含在 MPEG 流中。我通过录制一段短片并在我的电脑上播放文件来确认麦克风可以正常工作。

根据此答案中对 Gstreamer muxes 的描述,我创建了以下管道以尝试将音频包含在流中:

raspivid -n -w 640 -h 480 -fps 20 -b 2000000 -a 12 -t 0 -o - \
        | gst-launch-1.0 -e -v fdsrc \
        ! video/x-h264, width=640, height=480, framerate=20/1 \
        ! h264parse config-interval=-1 \
        ! mpegtsmux name=mux \
        ! rtpmp2tpay \
        ! udpsink host=$dstAdr port=5004 \
          alsasrc device=hw:1,0 \
        ! audio/x-raw, channels=1, rate=44100 \
        ! queue \
        ! lamemp3enc \
        ! mux.

麦克风只有1个通道,而且速率也经过了双重检查。管道不会立即抛出错误消息,但其他设备上的 VLC 无法播放流。一段时间后,将显示此错误消息:

ERROR: from element /GstPipeline:pipeline0/GstAlsaSrc:alsasrc0: Internal data stream error.
Additional debug info:
gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:pipeline0/GstAlsaSrc:alsasrc0:
streaming stopped, reason not-negotiated (-4)
EOS on shutdown enabled -- waiting for EOS after Error
Waiting for EOS...
ERROR: from element /GstPipeline:pipeline0/GstQueue:queue0: Internal data stream error.
Additional debug info:
gstqueue.c(988): gst_queue_handle_sink_event (): /GstPipeline:pipeline0/GstQueue:queue0:
streaming stopped, reason not-negotiated (-4)

问题

标签: raspberry-pigstreamervlcalsa

解决方案


推荐阅读