9

Is there a way make a pipeline that will play any video file (which will contain audio too)? I have tried linking elements like:

filesrc -> decodebin

along with

queue -> audioconvert -> autoaudiosink

and

queue -> autovideoconvert -> autovideosink

This causes two problems:

  1. A queue cannot be linked to an autovideoconvert.
  2. I have no idea how to implement a pad with the "pad-added" event, especially when the pipeline supports both audio and video.

I would like to know how to do this without the need for gst.parse_launch. Also, I want the pieline to work with any format I throw at it (like playbin), but cannot use a playbin as I will need to link other elements (level and volume).

Alternatively, is there a way to connect elements (such as level) to a playbin?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
D K
  • 5,530
  • 7
  • 31
  • 45

5 Answers5

3

queue is not a source element, you need to have either uridecodebin or decodebin or something simliar as the source element.

This is an example pipeline in the gst-launch format.

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

it means that in the pipeline there is

  • uridecodebin - A decoding bin, capable of decoding whatever source file is compatible with GStreamer, with the uri property set to file:///home/joar/Dropbox/Music/04 - Deadmau5 - Clockwork (Jonas Steur Remix).mp3.
  • audioconvert - Converts audio between different formats
  • autoaudiosink

If needed, you could add a queue element between the uridecodebin and the audioconvert.


Update

I can do what you describe using the following gst-launch command

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
Community
  • 1
  • 1
joar
  • 15,077
  • 1
  • 29
  • 54
  • 1
    Helpful, but it doesn't really answer my question. I have updated the pipeline layout to make things clearer. – D K Nov 20 '11 at 15:33
  • I see, I have an idea on how to solve this, unfortunately I'm out of time for now. Please add a comment after this so that I get a notification, then I'll respond as soon as I'm in front of my PC. – joar Nov 20 '11 at 15:37
  • All right, added a new section, if you're not using the gst-launch format, tell me and I'll help you perform it with `gst.element_factory_make()`. – joar Nov 20 '11 at 16:40
  • Can you help me do this with `gst.element_factory_make()` please? Also, what exactly is the `dmux.` doing? Is it linking the sinks to the decodebin? – D K Nov 20 '11 at 20:40
3

I've built an example video player that makes use of the elements you described.

It should show you how to connect the pads to eachother dynamically.

'''
Copyright (c) 2011 Joar Wandborg <http://wandborg.se>

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()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
joar
  • 15,077
  • 1
  • 29
  • 54
  • Perfect, but I still have one more question: `new-decoded-pad` is deprecated. How do I use `pad-added`? – D K Nov 21 '11 at 02:36
  • Thanks for the heads-up, I've updated the code but haven't had time to test it yet, it should be very similar though - just another method. – joar Nov 21 '11 at 10:11
  • It works but you have to put `if not sink_pad.is_linked():` before linking the pads. Also, I tested this with just audio, and it never plays. Do you know why? – D K Nov 21 '11 at 12:43
  • It might be that the video part of the pipeline is pausing it all. You could skip the linking of the videos and audio elements based on if the source file contains audio or video. – joar Nov 21 '11 at 12:56
  • Is there a way to do this without reconstructing the whole pipeline? If I just `add` the video elements to the pipeline without connecting them, it still has the same issue. – D K Nov 21 '11 at 21:00
  • Helpful resources for debugging GStreamer; http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/gst-running.html – joar Nov 22 '11 at 08:26
  • 1
    I have decided to use `Discoverer` to determine whether or not the media contains video, and create a video or audio pipeline based of the result. I'll mark this question as solved, but if there is a better way to make a dynamic pipeline like this, please let me know. Thank you for all your help! – D K Nov 23 '11 at 01:00
  • Good choice on `Discoverer`. I really don't know if there are easier ways. I have seen some things about autoplugs on certain elements, but haven't looked into them yet - You're welcome :) – joar Nov 23 '11 at 09:34
1

Alternatively you could use GStreamer 1.0 by now.

There you'll find the new properties audio-filter and video-filter, which can be utilized to connect elements (such as level) to a playbin.

With Python GObject Introspection this could be done as easy as:

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

playbin = Gst.ElementFactory.make("playbin")
playbin.props.audio_filter = level
tynn
  • 38,113
  • 8
  • 108
  • 143
0

I am sure you would have implemented this by now. But for others who looks at your question, my answer might help . The code is as follows

 #!/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()  
Thothadri Rajesh
  • 522
  • 7
  • 16
0

Pipeline to play audio & video on local:

gst-launch-1.0 -v filesrc location=random-file.mpeg ! decodebin name=demux demux. ! queue ! videoconvert ! xvimagesink demux. ! queue ! audioconvert ! pulsesink

we can use autovideosink in place of xvimagesink.