Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pipe video from DVR to GStreamer
#!/usr/bin/env python3
from gi import require_version
require_version('Gst', '1.0')
require_version('Gtk', '3.0')
from gi.repository import GObject, Gst, Gtk
from os.path import abspath
from sys import argv, stderr
from threading import Thread
Gst.init(None)
def player():
pipeline = Gst.Pipeline.new('player')
source = Gst.ElementFactory.make('appsrc', 'source')
source.props.caps = Gst.Caps.from_string('video/x-h264')
pipeline.add(source)
parser = Gst.ElementFactory.make('h264parse', 'parser')
parser.props.config_interval = -1
pipeline.add(parser)
decoder = Gst.ElementFactory.make('avdec_h264', 'decoder')
pipeline.add(decoder)
convert = Gst.ElementFactory.make('videoconvert', 'convert')
pipeline.add(convert)
sink = Gst.ElementFactory.make('gtksink', 'sink')
pipeline.add(sink)
source.link(parser)
parser.link(decoder)
decoder.link(convert)
convert.link(sink)
return pipeline
pipeline = player()
source = pipeline.get_by_name('source')
sink = pipeline.get_by_name('sink')
def stream():
from dvrip import DVRIP_PORT
from dvrip.io import DVRIPClient
from dvrip.monitor import Stream
from socket import AF_INET, SOCK_STREAM, socket as Socket
conn = DVRIPClient(Socket(AF_INET, SOCK_STREAM))
conn.connect((argv[1], DVRIP_PORT), 'admin', '')
sock = Socket(AF_INET, SOCK_STREAM)
sock.connect((argv[1], DVRIP_PORT))
file = conn.monitor(sock, 0, Stream.HD)
while True:
conn.keepalive()
data = file.read(256)
source.emit('push-buffer', Gst.Buffer.new_wrapped(data))
#sample = sink.props.last_sample
#if sample is not None:
# struct = sample.get_caps().get_structure(0)
# print(struct.get_int('width'), struct.get_int('height'))
streamer = Thread(None, stream, daemon=True)
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
window.set_default_size(400, 300)
window.connect('destroy', Gtk.main_quit, 'WM destroy')
header = Gtk.HeaderBar()
header.set_title('monitor:0;hd')
header.set_subtitle(argv[1])
header.set_show_close_button(True)
window.set_titlebar(header)
window.add(sink.props.widget)
window.show_all()
pipeline.set_state(Gst.State.PLAYING)
streamer.start()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment