Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import gi
import os
import logging
import signal
gi.require_version('Gst', '1.0')
gi.require_version('GLib', '2.0')
gi.require_version('GstNet', '1.0')
gi.require_version('GdkX11', '3.0')
gi.require_version('GstVideo', '1.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gst, GObject, GstNet, Gdk, GdkX11, GstVideo, Gtk # noqa
logging.basicConfig()
log = logging.getLogger(__name__)
Gst.init(None)
file_input = "/home/phas/Videos/loop.mp4"
class Player(object):
def __init__(self):
self.pipeline = Gst.Pipeline()
self.videosink = self.get_video_sink()
self.create_decoder_part()
self.offset = 0
self.last_buffer_time = 0
self.stream_started = False
self.segment_started = False
def get_video_sink(self):
queue = Gst.ElementFactory.make('queue')
queue.set_property('name', 'video_queue')
videosink = Gst.ElementFactory.make('vaapisink')
self.pipeline.add(queue)
self.pipeline.add(videosink)
queue.link(videosink)
queue_pad = queue.get_static_pad('sink')
return queue
def pad_added(self, demuxer, pad):
if pad.get_current_caps().get_structure(0).get_name()[0:5] == 'video':
videosink_pad = self.videosink.get_static_pad("sink")
pad.add_probe(Gst.PadProbeType.EVENT_DOWNSTREAM,
self.probe_callback)
pad.link(videosink_pad)
print('dotfile')
Gst.debug_bin_to_dot_file(
self.pipeline, Gst.DebugGraphDetails.ALL, 'debug')
def probe_callback(self, pad, info):
print(info.get_event().type)
type = info.get_event().type
if type == Gst.EventType.EOS:
print("END OF STREAM")
self.offset = self.last_buffer_time
print(self.pipeline.get_by_name('filesrc'))
filesrc = self.pipeline.get_by_name('filesrc')
self.pipeline.remove(filesrc)
decodebin = self.pipeline.get_by_name('decodebin')
self.pipeline.remove(decodebin)
video_queue = self.pipeline.get_by_name('video_queue')
decodebin.unlink(video_queue)
self.create_decoder_part(True)
return Gst.PadProbeReturn.DROP
return Gst.PadProbeReturn.OK
def create_decoder_part(self, set_playing=False):
filesrc = Gst.ElementFactory.make('filesrc')
decodebin = Gst.ElementFactory.make('decodebin')
filesrc.set_property('location', file_input)
filesrc.set_property('name', 'filesrc')
decodebin.connect('pad-added', self.pad_added)
decodebin.set_property('name', 'decodebin')
self.pipeline.add(filesrc)
self.pipeline.add(decodebin)
filesrc.link(decodebin)
if set_playing:
filesrc.set_state(Gst.State.PLAYING)
decodebin.set_state(Gst.State.PLAYING)
return decodebin
def start(self):
self.pipeline.set_state(Gst.State.PLAYING)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gdk.threads_init()
player = Player()
player.start()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment