Parse pipeline to Python

Ormi mi.ormos at gmail.com
Tue Apr 4 07:02:09 UTC 2017


I think there is a problem with raw text formatting in this forum.
Once again. Or something/someone block my raw text code samples.

__

That's my whole output from terminal. 

test4.py:6: PyGIWarning: Gtk was imported without specifying a version
first. Use gi.require_version('Gtk', '3.0') before import to ensure that the
right version gets loaded.
  from gi.repository import Gst, GObject, Gtk
Error: gst-stream-error-quark: decoding error (7) gstmpeg2dec.c(1134):
gst_mpeg2dec_handle_frame (): /GstPipeline:player/GstMpeg2dec:video-decoder:
Reached libmpeg2 invalid state

At the begging I sent my pipeline prototype which works in terminal. 

gst-launch-1.0 filesrc location=speaker.MTS ! decodebin name=demuxer
demuxer. ! \
queue ! audioconvert ! audioresample ! autoaudiosink demuxer. ! \
queue ! videoscale ! videoconvert ! videobox ! videomixer ! xvimagesink

And I showed you my Python code, which is best implementation of my pipeline
I can get. 
Because exactly implement the working pipeline prototype to Python failed.
So I used this tutorials. 

If anyobody can show me little example how to implement this my pipeline to
Python I will be grateful. 
I have a little mess in all pad, src, sinks and separate video/audio
processing, so I am not sure If I have right approach. 

#!/usr/bin/env python

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

class GTK_Main(object):

    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title("Mpeg2-Player")
        window.set_default_size(500, 400)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        vbox = Gtk.VBox()
        window.add(vbox)
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        self.entry = Gtk.Entry()
        hbox.add(self.entry)
        self.button = Gtk.Button("Start")
        hbox.pack_start(self.button, False, False, 0)
        self.button.connect("clicked", self.start_stop)
        self.movie_window = Gtk.DrawingArea()
        vbox.add(self.movie_window)
        window.show_all()

        self.player = Gst.Pipeline.new("player")
        source = Gst.ElementFactory.make("filesrc", "file-source")
        demuxer = Gst.ElementFactory.make("mpegpsdemux", "demuxer")
        demuxer.connect("pad-added", self.demuxer_callback)
        self.video_decoder = Gst.ElementFactory.make("mpeg2dec",
"video-decoder")
        self.audio_decoder = Gst.ElementFactory.make("mad", "audio-decoder")
        audioconv = Gst.ElementFactory.make("audioconvert", "converter")
        audiosink = Gst.ElementFactory.make("autoaudiosink", "audio-output")
        videosink = Gst.ElementFactory.make("autovideosink", "video-output")
        self.queuea = Gst.ElementFactory.make("queue", "queuea")
        self.queuev = Gst.ElementFactory.make("queue", "queuev")
        colorspace = Gst.ElementFactory.make("videoconvert", "colorspace")

        self.player.add(source) 
        self.player.add(demuxer) 
        self.player.add(self.video_decoder) 
        self.player.add(self.audio_decoder) 
        self.player.add(audioconv) 
        self.player.add(audiosink) 
        self.player.add(videosink) 
        self.player.add(self.queuea) 
        self.player.add(self.queuev) 
        self.player.add(colorspace)

        source.link(demuxer)

        self.queuev.link(self.video_decoder)
        self.video_decoder.link(colorspace)
        colorspace.link(videosink)

        self.queuea.link(self.audio_decoder)
        self.audio_decoder.link(audioconv)
        audioconv.link(audiosink)

        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

    def start_stop(self, w):
        if self.button.get_label() == "Start":
            filepath = self.entry.get_text().strip()
            if os.path.isfile(filepath):
                filepath = os.path.realpath(filepath)
                self.button.set_label("Stop")
               
self.player.get_by_name("file-source").set_property("location", filepath)
                self.player.set_state(Gst.State.PLAYING)
            else:
                self.player.set_state(Gst.State.NULL)
                self.button.set_label("Start")

    def on_message(self, bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print ("Error: %s" % err, debug)
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        if message.get_structure().get_name() == 'prepare-window-handle':
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            xid = self.movie_window.get_property('window').get_xid()
            imagesink.set_window_handle(xid)

    def demuxer_callback(self, demuxer, pad):
        if pad.get_property("template").name_template == "video_%02x":
            qv_pad = self.queuev.get_static_pad("sink")
            pad.link(qv_pad)
        elif pad.get_property("template").name_template == "audio_%02x":
            qa_pad = self.queuea.get_static_pad("sink")
            pad.link(qa_pad)

Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()


Thanks in advance!
Ormi



--
View this message in context: http://gstreamer-devel.966125.n4.nabble.com/Parse-pipeline-to-Python-tp4682430p4682505.html
Sent from the GStreamer-devel mailing list archive at Nabble.com.


More information about the gstreamer-devel mailing list