Advertisement
Guest User

Untitled

a guest
Mar 1st, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import gi
  2. gi.require_version('Gst', '1.0')
  3. from gi.repository import Gst, GLib
  4.  
  5. class GstRelay:
  6.     def __init__(self):
  7.         Gst.init(None)
  8.         self.pipeline1 = Gst.parse_launch(
  9.             "videotestsrc pattern=ball is-live=true ! video/x-raw,format=RGB,width=1280,height=720,framerate=240/1 ! videoconvert ! appsink name=sink"
  10.         )
  11.         self.appsink = self.pipeline1.get_by_name("sink")
  12.         self.appsink.set_property("emit-signals", True)
  13.         self.appsink.connect("new-sample", self.on_new_sample)
  14.         self.pipeline2 = Gst.parse_launch(
  15.             "appsrc name=src ! videoconvert ! autovideosink"
  16.         )
  17.         self.appsrc = self.pipeline2.get_by_name("src")
  18.         self.appsrc.set_property("caps", Gst.Caps.from_string("video/x-raw,format=RGB,width=1280,height=720"))
  19.  
  20.     def on_new_sample(self, sink):
  21.         """Callback when new frame arrives at appsink (Pipeline 1)."""
  22.         sample = sink.emit("pull-sample")
  23.         if not sample:
  24.             return Gst.FlowReturn.ERROR
  25.         buffer = sample.get_buffer()
  26.         self.appsrc.emit("push-buffer", buffer)
  27.         return Gst.FlowReturn.OK
  28.  
  29.     def start(self):
  30.         """Start both pipelines."""
  31.         self.pipeline1.set_state(Gst.State.PLAYING)
  32.         self.pipeline2.set_state(Gst.State.PLAYING)
  33.  
  34.     def stop(self):
  35.         """Stop both pipelines."""
  36.         self.pipeline1.set_state(Gst.State.NULL)
  37.         self.pipeline2.set_state(Gst.State.NULL)
  38.  
  39. def main():
  40.     gst_relay = GstRelay()
  41.     gst_relay.start()
  42.     loop = GLib.MainLoop()
  43.     try:
  44.         loop.run()
  45.     except KeyboardInterrupt:
  46.         print("Stopping pipelines...")
  47.         gst_relay.stop()
  48.  
  49. if __name__ == "__main__":
  50.     main()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement