import gi gi.require_version('Gst', '1.0') from gi.repository import Gst, GLib # Initialize GStreamer Gst.init(None) # Define the pipeline pipeline = Gst.parse_launch( "v4l2src device=/dev/video0 ! " "video/x-raw, format=GRAY16_LE, width=358, height=288, framerate=30/1 ! " "videoconvert ! " "x264enc bitrate=500 speed-preset=ultrafast tune=zerolatency ! " "tee name=t " "t. ! queue ! " "hlsdemux ! " "hlssink playlist-root=http://example.com/live location=/var/www/html/live/segment%05d.ts playlist-location=/var/www/html/live/playlist.m3u8 " "t. ! queue ! " "filesink location=/path/to/save/output.h264" ) # Start playing pipeline.set_state(Gst.State.PLAYING) # Wait until error or EOS bus = pipeline.get_bus() # Error handling def on_message(bus, message, loop): mtype = message.type if mtype == Gst.MessageType.ERROR: err, debug = message.parse_error() print("Error: %s" % err, debug) loop.quit() elif mtype == Gst.MessageType.EOS: print("End of stream") loop.quit() loop = GLib.MainLoop() bus.add_signal_watch() bus.connect("message", on_message, loop) try: loop.run() except KeyboardInterrupt: pass # Free resources pipeline.set_state(Gst.State.NULL)