1

我正在尝试如何在 python 3.x 中使用 gstreamer 进行音频编码,然后再进行流式传输,但不幸的是被卡住了。

我找到了这个简单的音频播放器,效果很好:

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

# Initializing threads used by the Gst various elements
GObject.threads_init()
#Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Gst.init(None)

class Main:
    def __init__(self):
        self.mainloop = GObject.MainLoop()
        #Creating the gst pipeline we're going to add elements to and use to play the file
        self.pipeline = Gst.Pipeline()

        #creating the filesrc element, and adding it to the pipeline
        self.filesrc = Gst.ElementFactory.make("filesrc", "filesrc")
        self.filesrc.set_property("location", "beatles.mp3")
        self.pipeline.add(self.filesrc)

        #creating and adding the decodebin element , an "automagic" element able to configure itself to decode pretty much anything
        self.decode = Gst.ElementFactory.make("decodebin", "decode")
        self.pipeline.add(self.decode)
        #connecting the decoder's "pad-added" event to a handler: the decoder doesn't yet have an output pad (a source), it's created at runtime when the decoders starts receiving some data
        self.decode.connect("pad-added", self.decode_src_created)

        #setting up (and adding) the alsasin, which is actually going to "play" the sound it receives
        self.sink = Gst.ElementFactory.make("alsasink", "sink")
        self.pipeline.add(self.sink)

        #linking elements one to another (here it's just the filesrc - > decoder link , the decoder -> sink link's going to be set up later)
        self.filesrc.link(self.decode)

    #handler taking care of linking the decoder's newly created source pad to the sink
    def decode_src_created(self, element, pad):
        pad.link(self.sink.get_static_pad("sink"))

    #running the shit
    def run(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        self.mainloop.run()

start=Main()
start.run()

然后我尝试在中间添加一个 speexenc 和一个 speexdec 元素。参见“# ADDED”指示的 6 行和“# MODIFIED”指示的 1 行。我知道在这种特殊情况下,通过 speex 进行编码和解码没有实际意义,但由于 python 3.x 中缺乏这方面的教程和示例,我想在阅读基本的 gstreamer 文档和常见问题解答后以某种方式开始试验。没有错误消息,但最终它什么也没播放,扬声器完全沉默:

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

# Initializing threads used by the Gst various elements
GObject.threads_init()
#Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Gst.init(None)

class Main:
    def __init__(self):
        self.mainloop = GObject.MainLoop()
        #Creating the gst pipeline we're going to add elements to and use to play the file
        self.pipeline = Gst.Pipeline()

        #creating the filesrc element, and adding it to the pipeline
        self.filesrc = Gst.ElementFactory.make("filesrc", "filesrc")
        self.filesrc.set_property("location", "beatles.mp3")
        self.pipeline.add(self.filesrc)

        #creating and adding the decodebin element , an "automagic" element able to configure itself to decode pretty much anything
        self.decode = Gst.ElementFactory.make("decodebin", "decode")
        self.pipeline.add(self.decode)
        #connecting the decoder's "pad-added" event to a handler: the decoder doesn't yet have an output pad (a source), it's created at runtime when the decoders starts receiving some data
        self.decode.connect("pad-added", self.decode_src_created)

        self.speexenc = Gst.ElementFactory.make("speexenc", "speexenc") # ADDED
        self.pipeline.add(self.speexenc) # ADDED

        self.speexdec = Gst.ElementFactory.make("speexdec", "speexdec") # ADDED
        self.pipeline.add(self.speexdec) # ADDED

        #setting up (and adding) the alsasin, which is actually going to "play" the sound it receives
        self.sink = Gst.ElementFactory.make("alsasink", "sink")
        self.pipeline.add(self.sink)

        #linking elements one to another
        self.filesrc.link(self.decode)
        self.speexenc.link(self.speexdec) # ADDED
        self.speexdec.link(self.sink) # ADDED

    #handler taking care of linking the decoder's newly created source pad to the speexenc
    def decode_src_created(self, element, pad):
        pad.link(self.speexenc.get_static_pad("sink")) # MODIFIED

    #running the shit
    def run(self):
        self.pipeline.set_state(Gst.State.PLAYING)
        self.mainloop.run()

start=Main()
start.run()

任何使它最终起作用的建议都将受到高度赞赏!提前谢谢了!

编辑: GST_DEBUG="*:3" 的输出

** (GsTest04.py:2500): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
0:00:00.310035438  2500  0x25b7290 FIXME                  id3v2      gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.310425594  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.310597677  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.310753458  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.310931687  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.311063614  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.311322155  2500  0x25b7290 FIXME                  id3v2 gstid3tag.c:142:gst_tag_from_id3_tag: Cannot map ID3v2 tag 'PRIV' to GStreamer tag
0:00:00.376435699  2500 0x741014f0 WARN                    alsa conf.c:4544:parse_args: alsalib error: Unknown parameter AES0
0:00:00.376732365  2500 0x741014f0 WARN                    alsa conf.c:4704:snd_config_expand: alsalib error: Parse arguments error: No such file or directory
0:00:00.376897261  2500 0x741014f0 WARN                    alsa pcm.c:2217:snd_pcm_open_noupdate: alsalib error: Unknown PCM default:{AES0 0x02 AES1 0x82 AES2 0x00 AES3 0x02}
0:00:00.378137573  2500 0x741014f0 WARN               baseparse gstbaseparse.c:3188:gst_base_parse_loop:<mpegaudioparse0> error: streaming stopped, reason not-linked
4

1 回答 1

2

我看不出有什么明显的错误。我建议使用 GST_DEBUG="*:3" 运行您的程序,以查看日志中是否有任何警告。您可能还想仔细检查所有 api 调用是否有效。(例如那个

self.speexenc = Gst.ElementFactory.make(...)

没有导致None。我还要检查 decode_src_created 中 pad.link() 的返回值。如果解码器产生的原始音频不是 speexenc 喜欢的,则链接将失败。尝试在您的 speexenc 之前添加一个 audioconvert 元素。

于 2015-09-15T15:54:54.650 回答