【发布时间】:2017-12-19 04:12:03
【问题描述】:
我正在尝试构建一个可以即时生成音频和视频并将其流式传输到 rtmp 服务器(如 Twitch 或 Youtube)的程序或机器人。我发现我可以使用 Gstreamer 提取视频和音频流。我还发现 Gstreamer 有一个 Python 库,这很好,因为我的机器人已经用 Python 编写了。
问题是,在使用 testvideosrc 输入进行测试时,我可以使命令在命令行上同时处理音频和视频都正常工作,但在尝试运行时它会立即退出。
代码是
# Command Trying to Replicate in Python
# gst-launch-1.0 videotestsrc is-live=true ! videoconvert ! x264enc bitrate=1000 tune=zerolatency ! queue ! flvmux name=mux ! rtmpsink location='rtmp://live.twitch.tv/app/STREAM_KEY_HERE' audiotestsrc is-live=true ! audioconvert ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=96000 ! audio/mpeg ! aacparse ! audio/mpeg, mpegversion=4 ! mux.
# ORIGINAL (UNEDITED) COMMAND - gst-launch-1.0 videotestsrc is-live=true ! videoconvert ! x264enc bitrate=1000 tune=zerolatency ! video/x-h264 ! h264parse ! video/x-h264 ! queue ! flvmux name=mux ! rtmpsink location='rtmp://live.twitch.tv/app/STREAM_KEY_HERE' audiotestsrc is-live=true ! audioconvert ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=96000 ! audio/mpeg ! aacparse ! audio/mpeg, mpegversion=4 ! mux.
STREAM_URL = "rtmp://REDACTED"
# For StackExchange - gst-launch-1.0 videotestsrc is-live=true ! autovideoconvert ! x264enc bitrate=1000 tune=zerolatency ! queue ! flvmux name=mux ! rtmpsink location='rtmp://live.twitch.tv/app/STREAM_KEY_HERE' audiotestsrc is-live=true ! audioconvert ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=96000
# Imports
import gi
import time
from gi.repository import GObject, Gst
import os
# OS Variables and Requirements
gi.require_version('Gst', '1.0')
os.environ["GST_DEBUG"] = "4" # Enable Debug
# Initialize GStreamer
Gst.init(None) # gst-launch-1.0 !
pipeline = Gst.Pipeline()
# Create Video Source (Video Test Source)
videosrc = Gst.ElementFactory.make("videotestsrc") # videotestsrc is-live=true !
#videosrc.set_property('pattern', 18)
videosrc.set_property('is-live', True)
pipeline.add(videosrc)
# Convert Video (to x264enc?)
videoconvert = Gst.ElementFactory.make('autovideoconvert') # videoconvert
pipeline.add(videoconvert)
# IDK
idk = Gst.ElementFactory.make("x264enc") # x264enc bitrate=1000 tune=zerolatency
idk.set_property('bitrate', 1000)
idk.set_property('tune', 'zerolatency')
pipeline.add(idk)
# Queue Data
queueRTMP = Gst.ElementFactory.make("queue") # queue
pipeline.add(queueRTMP)
# Convert to Mux
flvmux = Gst.ElementFactory.make("flvmux", "mux") # flvmux name=mux
pipeline.add(flvmux)
# Stream to RTMP Server
rtmpsink = Gst.ElementFactory.make("rtmpsink") # rtmpsink location='rtmp://live.twitch.tv/app/STREAM_KEY_HERE'
rtmpsink.set_property("location", STREAM_URL)
pipeline.add(rtmpsink)
# audiotestsrc is-live=true ! audioconvert ! audioresample ! audio/x-raw,rate=48000 ! voaacenc bitrate=96000 ! audio/mpeg ! aacparse ! audio/mpeg, mpegversion=4 ! mux.
videosrc.link(videoconvert)
videoconvert.link(idk)
idk.link(queueRTMP)
queueRTMP.link(flvmux)
flvmux.link(rtmpsink)
pipeline.set_state(Gst.State.PLAYING)
我试图修复它的是,最初我无法让流工作,因为我有一个额外的“接收器”。此文件与具有“接收器”问题的文件之间的唯一区别是“autovideoconvert”是“videoconvert”。该命令在命令行中运行正常。
接收器问题错误消息:
0:00:00.038202264 25199 0x272a370 INFO GST_ELEMENT_PADS gstelement.c:892:gst_element_get_static_pad: no such pad 'sink' in element "videotestsrc0"
现在,“autovideoconvert”没有错误消息。相反,即使流处于播放状态,程序也会退出。
日志中显示此消息可以解决接收器问题:
0:00:00.039500044 25214 0x2568d40 INFO GST_PADS gstpad.c:2388:gst_pad_link_full: linked videoconvert0:src and '':sink_internal, successful
日志中显示的最后一条状态更改消息如下:
0:00:00.043316535 25214 0x2568d40 INFO GST_STATES gstelement.c:2328:gst_element_continue_state:<videoscale0> completed state change to PLAYING
0:00:00.043341987 25214 0x2568d40 INFO GST_STATES gstelement.c:2233:_priv_gst_element_state_changed:<videoscale0> notifying about state-changed PAUSED to PLAYING (VOID_PENDING pending)
这些都是日志级别设置为4时。我无法理解的是,我做错了什么才能使命令起作用,而不是命令的Python版本。如果有人碰巧知道如何更改关键帧间隔,那就太好了。除非我可以将其设置为 4 秒或更快,否则 Youtube 不想合作。谢谢!
【问题讨论】:
标签: python python-3.x command-line stream gstreamer