2011-11-18 14 views
7

क्या कोई तरीका है कि कोई पाइपलाइन बनाएं जो किसी भी वीडियो फ़ाइल (जिसमें ऑडियो भी शामिल होगा) चलाएगा?Gstreamer (Python) में एक पाइपलाइन के साथ ऑडियो और वीडियो चलाएं

filesrc -> decodebin 

साथ साथ

queue -> audioconvert -> autoaudiosink 

और

queue -> autovideoconvert -> autovideosink 

यह दो समस्याओं का कारण बनता:: मैं जैसे तत्वों को जोड़ने की कोशिश की है

  1. एक queue एक autovideoconvert से जुड़ा हुआ नहीं किया जा सकता ।
  2. मुझे नहीं पता कि "pad-added" ईवेंट के साथ पैड को कैसे कार्यान्वित किया जाए, खासकर जब पाइपलाइन ऑडियो और वीडियो दोनों का समर्थन करे।

मुझे पता है कि कैसे gst.parse_launch की आवश्यकता के बिना यह करने के लिए करना चाहते हैं। साथ ही, मैं चाहता हूं कि पिलिन किसी भी प्रारूप के साथ काम करे (जैसे प्लेबिन), लेकिन प्लेबिन का उपयोग नहीं कर सकता क्योंकि मुझे अन्य तत्वों को जोड़ने की आवश्यकता होगी (level और volume)।

वैकल्पिक रूप से, क्या प्लेबिन में तत्वों (जैसे level) को जोड़ने का कोई तरीका है?

उत्तर

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

बिल्कुल सही, लेकिन मेरे पास अभी भी एक और सवाल है: 'नया-डीकोडेड-पैड' बहिष्कृत किया गया है। मैं 'पैड-एडेड' का उपयोग कैसे करूं? –

+0

हेड-अप के लिए धन्यवाद, मैंने कोड अपडेट किया है लेकिन अभी तक इसका परीक्षण करने का समय नहीं है, यह बहुत समान होना चाहिए - बस एक और तरीका। – joar

+0

यह काम करता है लेकिन पैड को जोड़ने से पहले आपको 'sink_pad.is_linked(): 'नहीं रखना होगा। इसके अलावा, मैंने इसे सिर्फ ऑडियो के साथ परीक्षण किया, और यह कभी नहीं खेलता है। तुम जानते हो क्यों? –

1

queue एक स्रोत तत्व नहीं है, तो आपको या तो uridecodebin या decodebin या स्रोत तत्व के रूप में कुछ समान होना चाहिए।

यह 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

के बीच ऑडियो में कनवर्ट आवश्यक होने पर आप uridecodebin और audioconvert जो queue तत्व जोड़ सकते हैं।


अद्यतन

मैं क्या आप निम्नलिखित जीएसटी-लॉन्च आदेश

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.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 इस्तेमाल कर सकते हैं अब कर रहा है।

वहाँ आप एक playbin (जैसे level) रूप audio-filter और video-filter, जो तत्वों कनेक्ट करने के लिए उपयोग किया जा सकता नए गुणों मिल जाएगा

अजगर GObject आत्मनिरीक्षण के साथ

इस तरह सरल किया जा सकता है:।

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

playbin = Gst.ElementFactory.make("playbin") 
playbin.props.audio_filter = level 
संबंधित मुद्दे