2011-11-18 241 views
7

有沒有辦法讓管道播放任何視頻文件(它也會包含音頻)?我曾嘗試連接相同的元素:用管道在Gstreamer(Python)中播放音頻和視頻

filesrc -> decodebin 

隨着

queue -> audioconvert -> autoaudiosink 

queue -> autovideoconvert -> autovideosink 

這會導致兩個問題:

  1. 一個queue不能鏈接到autovideoconvert
  2. 我不知道如何使用"pad-added"事件實現pad,尤其是當管道支持音頻和視頻時。

我想知道如何做到這一點,而不需要gst.parse_launch。另外,我希望pieline可以使用任何格式(比如playbin),但不能使用playbin,因爲我需要鏈接其他元素(levelvolume)。

另外,有沒有辦法將元素(例如level)連接到playbin?

回答

3

我已經建立了一個example video player,使得使用您所描述的元素。

它應該告訴你如何動態地連接墊彼此。

''' 
Copyright (c) 2011 Joar Wandborg 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

--- 

- A response to http://stackoverflow.com/questions/8187257/play-audio-and-video-with-a-pipeline-in-gstreamer-python/8197837 
- Like it? Buy me a beer! https://flattr.com/thing/422997/Joar-Wandborg 
''' 

import gst 
import gobject 
gobject.threads_init() 
import logging 


logging.basicConfig() 

_log = logging.getLogger(__name__) 
_log.setLevel(logging.DEBUG) 


class VideoPlayer(object): 
    ''' 
    Simple video player 
    ''' 

    source_file = None 

    def __init__(self, **kwargs): 
     self.loop = gobject.MainLoop() 

     if kwargs.get('src'): 
      self.source_file = kwargs.get('src') 

     self.__setup() 

    def run(self): 
     self.loop.run() 

    def stop(self): 
     self.loop.quit() 

    def __setup(self): 
     _log.info('Setting up VideoPlayer...') 
     self.__setup_pipeline() 
     _log.info('Set up') 

    def __setup_pipeline(self): 
     self.pipeline = gst.Pipeline('video-player-pipeline') 

     # Source element 
     self.filesrc = gst.element_factory_make('filesrc') 
     self.filesrc.set_property('location', self.source_file) 
     self.pipeline.add(self.filesrc) 

     # Demuxer 
     self.decoder = gst.element_factory_make('decodebin2') 
     self.decoder.connect('pad-added', self.__on_decoded_pad) 
     self.pipeline.add(self.decoder) 

     # Video elements 
     self.videoqueue = gst.element_factory_make('queue', 'videoqueue') 
     self.pipeline.add(self.videoqueue) 

     self.autovideoconvert = gst.element_factory_make('autovideoconvert') 
     self.pipeline.add(self.autovideoconvert) 

     self.autovideosink = gst.element_factory_make('autovideosink') 
     self.pipeline.add(self.autovideosink) 

     # Audio elements 
     self.audioqueue = gst.element_factory_make('queue', 'audioqueue') 
     self.pipeline.add(self.audioqueue) 

     self.audioconvert = gst.element_factory_make('audioconvert') 
     self.pipeline.add(self.audioconvert) 

     self.autoaudiosink = gst.element_factory_make('autoaudiosink') 
     self.pipeline.add(self.autoaudiosink) 

     self.progressreport = gst.element_factory_make('progressreport') 
     self.progressreport.set_property('update-freq', 1) 
     self.pipeline.add(self.progressreport) 

     # Link source and demuxer 
     linkres = gst.element_link_many(
      self.filesrc, 
      self.decoder) 

     if not linkres: 
      _log.error('Could not link source & demuxer elements!\n{0}'.format(
        linkres)) 

     linkres = gst.element_link_many(
      self.audioqueue, 
      self.audioconvert, 
      self.autoaudiosink) 

     if not linkres: 
      _log.error('Could not link audio elements!\n{0}'.format(
        linkres)) 

     linkres = gst.element_link_many(
      self.videoqueue, 
      self.progressreport, 
      self.autovideoconvert, 
      self.autovideosink) 

     if not linkres: 
      _log.error('Could not link video elements!\n{0}'.format(
        linkres)) 

     self.bus = self.pipeline.get_bus() 
     self.bus.add_signal_watch() 
     self.bus.connect('message', self.__on_message) 

     self.pipeline.set_state(gst.STATE_PLAYING) 

    def __on_decoded_pad(self, pad, data): 
     _log.debug('on_decoded_pad: {0}'.format(pad)) 

     if pad.get_caps()[0].to_string().startswith('audio'): 
      pad.link(self.audioqueue.get_pad('sink')) 
     else: 
      pad.link(self.videoqueue.get_pad('sink')) 

    def __on_message(self, bus, message): 
     _log.debug(' - MESSAGE: {0}'.format(message)) 


if __name__ == '__main__': 
    player = VideoPlayer(
     src='/home/joar/Videos/big_buck_bunny_1080p_stereo.avi') 

    player.run()
+0

完美,但我仍然有一個問題:'新解碼墊'已棄用。我如何使用'pad-added'? –

+0

感謝您的提醒,我已經更新了代碼,但還沒有時間來測試它,但它應該非常相似 - 只是另一種方法。 – joar

+0

它的工作原理,但你必須把'如果不是sink_pad.is_linked():'連接墊之前。此外,我只用音頻測試了它,它從不播放。你知道爲什麼嗎? –

1

queue不是源元素,您需要有uridecodebindecodebin或類似於源元素的東西。

這是gst-launch格式的示例流水線。

uridecodebin \ 
    uri="file:///home/joar/Dropbox/Music/04 - Deadmau5 - Clockwork (Jonas Steur Remix).mp3" \ 
! audioconvert ! autoaudiosink 

這意味着,在管道有

  • uridecodebin - 解碼斌,能夠解碼無論源文件是在GStreamer兼容,與uri屬性設置爲file:///home/joar/Dropbox/Music/04 - Deadmau5 - Clockwork (Jonas Steur Remix).mp3的。
  • audioconvert - 轉換不同的格式
  • autoaudiosink

之間的音頻。如果需要,你可以添加uridecodebinaudioconvert之間的queue元素。


更新

我可以做你的描述使用的是什麼以下GST-launch命令

gst-launch-0.10 filesrc \ 
    location="/home/joar/Dropbox/Skrillex vs. Adele - Set Fire to Everybody.mov" \ 
! decodebin name=dmux \ 
dmux. ! queue ! audioconvert ! autoaudiosink \ 
dmux. ! queue ! autovideoconvert ! autovideosink 
+1

有幫助,但它並沒有真正回答我的問題。我更新了管道佈局以使事情更清晰。 –

+0

我明白了,我有一個關於如何解決這個問題的想法,不幸的是我現在已經沒時間了。請在此之後添加評論,以便我收到通知,然後我會回覆就在我在我的電腦前。 – joar

+0

好的,添加了一個新的部分,如果你沒有使用gst-launch格式,告訴我,我會幫你用'gst.element_factory_make()'執行它。 – joar

1

我相信你現在已經實現了。但對於查看你的問題的其他人,我的回答可能會有所幫助。代碼如下所示

#!/usr/bin/python 
import pygst 
pygst.require('0.10') 
import gst 

import pygtk 
pygtk.require('2.0') 
import gtk 

# this is very important, without this, callbacks from gstreamer thread 
# will messed our program up 


def on_new_decoded_pad(dbin, pad, islast): 
    structure_name = pad.get_caps()[0].get_name() 
    decode = pad.get_parent() 
    pipeline = decode.get_parent() 
    if structure_name.startswith("video"): 
      queuev = pipeline.get_by_name('queuev') 
     decode.link(queuev) 
    if structure_name.startswith("audio"): 
      queuea = pipeline.get_by_name('queuea') 
    print queuea 
     decode.link(queuea) 


def main(): 
    pipeline = gst.Pipeline('pipleline') 

    filesrc = gst.element_factory_make("filesrc", "filesrc") 
    filesrc.set_property('location', '/home/thothadri/Videos/nuclear.avi') 

    decode = gst.element_factory_make("decodebin", "decode") 

    queuev = gst.element_factory_make("queue", "queuev") 

    sink = gst.element_factory_make("autovideosink", "sink") 

    queuea = gst.element_factory_make("queue", "queuea") 

    convert = gst.element_factory_make('audioconvert', 'convert') 

    sink_audio = gst.element_factory_make("autoaudiosink", "sink_audio") 

    pipeline.add(filesrc,decode,queuev,queuea,convert,sink,sink_audio) 

    gst.element_link_many(filesrc, decode) 
    gst.element_link_many(queuev,sink) 
    gst.element_link_many(queuea,convert,sink_audio) 

    decode.connect("new-decoded-pad", on_new_decoded_pad) 

    pipeline.set_state(gst.STATE_PLAYING) 



main() 
gtk.gdk.threads_init() 
gtk.main() 
1

或者您可以通過現在使用的GStreamer 1.0。

在那裏,你會發現新的屬性audio-filtervideo-filter,它可以被用來連接元素(如level)到playbin

使用Python的GObject內省可以這樣做一樣簡單:

level = Gst.ElementFactory.make('level') 

playbin = Gst.ElementFactory.make("playbin") 
playbin.props.audio_filter = level