Python 中的 Gstreamer 立即退出,但在命令行上正常
Gstreamer in Python exits instantly, but is fine on command line
我正在尝试构建一个可以动态生成音频和视频并将其流式传输到 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)
出于我试图修复的原因,最初我无法让流工作,因为我在其中有一个额外的 "sink"。此文件与有 "sink" 问题的文件之间的唯一区别是 "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 版本。如果有人碰巧知道如何更改关键帧间隔,那就太好了。 Youtube 不想合作,除非我能把它设置到 4 秒和更快。谢谢!
pad 的错误似乎很明显。不查看管道是否正确 - no such pad 'sink' in element "videotestsrc0"
:所以这不是 s sink 而是 src 元素。当然,这些没有水槽垫,因为它们是来源。
我不知道您的应用程序的其余部分是什么样子。但是 "running" 管道是非阻塞调用。因此,如果您没有适当的应用程序循环或只是在通过其他方式调用后等待,应用程序可能会立即退出。
x264enc key-int-max=<max-number-of-frames>
。或 idk.set_property('key-int-max', 60)
.
我正在尝试构建一个可以动态生成音频和视频并将其流式传输到 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)
出于我试图修复的原因,最初我无法让流工作,因为我在其中有一个额外的 "sink"。此文件与有 "sink" 问题的文件之间的唯一区别是 "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 版本。如果有人碰巧知道如何更改关键帧间隔,那就太好了。 Youtube 不想合作,除非我能把它设置到 4 秒和更快。谢谢!
pad 的错误似乎很明显。不查看管道是否正确 -
no such pad 'sink' in element "videotestsrc0"
:所以这不是 s sink 而是 src 元素。当然,这些没有水槽垫,因为它们是来源。我不知道您的应用程序的其余部分是什么样子。但是 "running" 管道是非阻塞调用。因此,如果您没有适当的应用程序循环或只是在通过其他方式调用后等待,应用程序可能会立即退出。
x264enc key-int-max=<max-number-of-frames>
。或idk.set_property('key-int-max', 60)
.