python Gstreamer play video files encoded in different formats

python Gstreamer play video files encoded in different formats

  I wrote how to find a video in the video and audio encoding format, and selected elements according to the encoding format for video playback in the previous blog. However, in the same package as the video file format, not necessarily video, audio encoding format are the same (e.g., encapsulation format mkv video file format may be encoded Audio AAC, also possibly MPEG). So when we want to play a MKV file, you do not know which encoding format how to do it?
  As used herein, decodebin element to achieve. The following code is the first edition, although the results no problem, but the understanding of decodebin incorrect. may be not only decoded decodebin decapsulates video and AV shunt. At first I just regard it as a simple decoding device.
The new code address: Use Gstreamer unknown format video playback (python)

First Edition python gstreamer code is as follows:

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

Gst.init(None)
def cb_demuxer_newpad(src, pad, dst,dst2):
    if pad.get_property("template").name_template == "video_%u":
        vdec_pad = dst.get_static_pad("sink")
        pad.link(vdec_pad)
    elif pad.get_property("template").name_template == "audio_%u":
        adec_pad = dst2.get_static_pad("sink")
        pad.link(adec_pad)
def cb_decodebina_newpad(src, pad, dst):
    if pad.get_property("template").name_template == "src_%u":
        qv_pad = dst.get_static_pad("sink")
        pad.link(qv_pad)

pipe = Gst.Pipeline.new("test")
src = Gst.ElementFactory.make("filesrc", "src")
demuxer = Gst.ElementFactory.make("matroskademux", "demux")

decodebin = Gst.ElementFactory.make("avdec_h264", "decode")
queuev = Gst.ElementFactory.make("queue", "queue")
conv = Gst.ElementFactory.make("videoconvert", "conv")
sink = Gst.ElementFactory.make("xvimagesink", "sink")

decodebina = Gst.ElementFactory.make("decodebin", "decodea")
queuea = Gst.ElementFactory.make("queue", "queuea")
conva = Gst.ElementFactory.make("audioconvert", "conva")
sinka = Gst.ElementFactory.make("autoaudiosink", "sinka")

src.set_property("location", "a.mkv")
demuxer.connect("pad-added", cb_demuxer_newpad, queuev,queuea)

pipe.add(src)
pipe.add(demuxer)
pipe.add(queuev)
pipe.add(decodebin)
pipe.add(conv)
pipe.add(sink)

pipe.add(queuea)
pipe.add(decodebina)
pipe.add(conva)
pipe.add(sinka)

src.link(demuxer)
queuev.link(decodebin)
decodebin.link(conv)
conv.link(sink)

queuea.link(decodebina)
#decodebina.link(conva)
decodebina.connect("pad-added", cb_decodebina_newpad, conva)

conva.link(sinka)

pipe.set_state(Gst.State.PLAYING)

mainloop = GLib.MainLoop()
mainloop.run()

Guess you like

Origin blog.csdn.net/qq_32188669/article/details/94021810