Use Gstreamer unknown format video playback (python)
1. Play Audio containing unknown format of video
Gstreamer be implemented using the decodebin.
Codes are as follows:
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib
Gst.init(None)
#decodebin产生新pad的回调函数
def cb_decodebin_newpad(src, pad, dst,dst2):
caps = Gst.Pad.get_current_caps(pad)
structure_name = caps.to_string()
if structure_name.startswith("video"):
videorate_pad = dst.get_static_pad("sink")
pad.link(videorate_pad)
elif structure_name.startswith("audio"):
volume_pad = dst2.get_static_pad("sink")
pad.link(volume_pad)
pipe = Gst.Pipeline.new("test")
src = Gst.ElementFactory.make("filesrc", "src")
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")
decodebina.connect("pad-added", cb_decodebin_newpad, queuev,queuea)
pipe.add(src)
pipe.add(queuev)
pipe.add(conv)
pipe.add(sink)
pipe.add(queuea)
pipe.add(decodebina)
pipe.add(conva)
pipe.add(sinka)
src.link(decodebina)
queuev.link(conv)
conv.link(sink)
queuea.link(conva)
conva.link(sinka)
pipe.set_state(Gst.State.PLAYING)
mainloop = GLib.MainLoop()
mainloop.run()
2. Play Audio's unknown whether it contains video
Use liners and detection mechanisms in the blocking Gstreamer to complete.
Currently the program is also a problem: no sound phenomenon may occur within a short time after the video starts playing. We are trying to solve.
Codes are as follows:
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib
Gst.init(None)
a = 0
def cb_add_elements(src,pad):
global a
if a == 0:
a = 1
pipe.add(queuea)
pipe.add(conva)
pipe.add(sinka)
queuea.link(conva)
conva.link(sinka)
return Gst.PadProbeReturn.PASS
else:
return Gst.PadProbeReturn.DROP
def cb_decodebin_newpad(src, pad):
caps = Gst.Pad.get_current_caps(pad)
structure_name = caps.to_string()
if structure_name.startswith("video"):
videorate_pad = queuev.get_static_pad("sink")
pad.link(videorate_pad)
elif structure_name.startswith("audio"):
Gst.Pad.add_probe(pad, Gst.PadProbeType.IDLE, cb_add_elements)
volume_pad = queuea.get_static_pad("sink")
pad.link(volume_pad)
pipe = Gst.Pipeline.new("test")
src = Gst.ElementFactory.make("filesrc", "src")
queuev = Gst.ElementFactory.make("queue", "queue")
conv = Gst.ElementFactory.make("videoconvert", "conv")
sink = Gst.ElementFactory.make("xvimagesink", "sink")
decodebin = 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")
decodebin.connect("pad-added", cb_decodebin_newpad)
pipe.add(src)
pipe.add(queuev)
pipe.add(conv)
pipe.add(sink)
pipe.add(decodebin)
src.link(decodebin)
queuev.link(conv)
conv.link(sink)
pipe.set_state(Gst.State.PLAYING)
mainloop = GLib.MainLoop()
mainloop.run()
3. related study materials
Gsreamer dynamic connection information: gstreamer dynamic link
Gstreamer C language with Python control statements: Statement control