Python中的Gstreamer立即退出,但在命令行上可以 [英] Gstreamer in Python exits instantly, but is fine on command line

查看:130
本文介绍了Python中的Gstreamer立即退出,但在命令行上可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个程序或机器人,该程序或机器人可以即时生成音频和视频,并将其流式传输到rtmp服务器(例如Twitch或Youtube)。我发现我可以使用Gstreamer引入视频和音频流。我还发现Gstreamer在Python中有一个库,这很好,因为我的机器人已经用Python编写了。

I am trying to build a program or robot that can generate audio and video on the fly and stream it to a rtmp server such a Twitch or Youtube. I have found that I can pull in streams of video and audio using Gstreamer. I also have found that Gstreamer has a library in Python which is good because my robot is already written in Python.

问题是,当使用testvideosrc输入进行测试时,我可以使命令在音频和视频上都能正常工作,但是在尝试运行时会立即退出。

The problem is, when testing with the testvideosrc input, I can make the command work just fine with both audio and video on the command line, but it exits immediately when trying to run it.

代码是

# 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。在命令行中运行时,此命令可以正常工作。

Out of what I tried to fix it is, originally I could not get the stream to work because I had an extra "sink" in it. The only difference between this file and the one with the "sink" problem is that "autovideoconvert" was "videoconvert". This command works fine when run in the command line.

接收器问题错误消息:

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的错误消息。取而代之的是,即使流处于播放状态,程序也只会退出。

Now, there is no error message with having "autovideoconvert". Instead, the program just exits even when the stream is in the playing state.

接收器问题已显示为可通过以下消息在日志中解决:

The sink problem is shown to be solved with this message in the log:

0:00:00.039500044 25214      0x2568d40 INFO                GST_PADS gstpad.c:2388:gst_pad_link_full: linked videoconvert0:src and '':sink_internal, successful

日志中显示的最后一个状态更改消息如下:

The last state changing message that shows up in the log is below:

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不希望合作。谢谢!

These are all when the log level is set to 4. What I cannot understand is, what am I doing wrong to make the command work, but not the Python version of the command. It is bonus if anyone happens to know how to change the keyframe interval. Youtube does not want to cooperate unless I can set it to 4 seconds and faster. Thanks!

推荐答案


  1. 垫子的错误似乎很明显。如果不查看pipleline是否正确-在元素 videotestsrc0中没有这样的垫子'sink':因此,这不是sink而是src元素。当然,这些作为源是没有接收器的。

  1. The error with the pad seems obvious. Without looking at the pipleline if its correct - no such pad 'sink' in element "videotestsrc0": So this is not s sink but a src element. And naturally these don't have sink pads as they are sources.

我不知道应用程序其余部分的样子。但是运行管道是非阻塞调用。因此,如果您没有正确的应用程序循环,或者只是在通过其他方式进行调用之后等待,则应用程序可能会立即退出。

I have no idea how the rest of your app looks like. But "running" a pipeline is a non-blocking call. So if you don't have a proper application loop or just wait after the call by other means the application may immediately exit.

x264enc key-int-max =<最大帧数> 。或 idk.set_property(’key-int-max',60)

这篇关于Python中的Gstreamer立即退出,但在命令行上可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆