Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import sys
  3. import gi
  4. gi.require_version('Gst', '1.0')
  5. from gi.repository import Gst
  6. import time
  7. import threading
  8. import os
  9. import stat
  10.  
  11. def mkfifo(fifo):
  12.     if os.path.exists(fifo):
  13.         if stat.S_ISFIFO(os.stat(fifo).st_mode):
  14.             pass
  15.         else:
  16.             os.remove(fifo)
  17.             os.mkfifo(fifo)
  18.     else:
  19.         os.mkfifo(fifo)
  20.  
  21. def control():
  22.     global pads
  23.     FIFO="/tmp/fifo"
  24.     mkfifo(FIFO)
  25.  
  26.     idx = 0
  27.     while True:
  28.         with open(FIFO) as fifo:
  29.             line=fifo.read().strip()
  30.             if "=" not in line:
  31.                 print("Invalid line.")
  32.                 continue
  33.             key = line.split("=")[0]
  34.             val = line.split("=")[1]
  35.  
  36.             if key == "switch":
  37.                 idx = (idx+1) % len(pads)
  38.                 switch(idx)
  39.  
  40. def switch(idx):
  41.     global pads
  42.     global pipeline
  43.     global switcher
  44.  
  45.     newpad = switcher.get_static_pad(pads[idx])
  46.     pipeline.set_state(Gst.State.PAUSED)
  47.     switcher.set_property('active-pad', newpad)
  48.     pipeline.set_state(Gst.State.PLAYING)
  49.  
  50. if __name__ == "__main__":
  51.     # initialization
  52.  
  53.     Gst.init(None)
  54.     Gst.debug_set_active(True)
  55.     Gst.debug_set_default_threshold(3)
  56.  
  57.     img = "multifilesrc name=img location=logo.png ! pngdec ! imagefreeze ! videobox border-alpha=0 top=-180 left=-10 ! mix."
  58.     out = "x264enc ! mpegtsmux ! filesink location=out.mp4"
  59.     out = "autovideosink sync=false "
  60.     inputs = [
  61.                 "videotestsrc do-timestamp=true pattern=0 ! video/x-raw,width=800,height=600",
  62.                 "multifilesrc do-timestamp=true location=regret.png ! pngdec ! imagefreeze",
  63.                 "videotestsrc do-timestamp=true pattern=1 ! video/x-raw,width=800,height=600",
  64.                 "rtmpsrc      do-timestamp=true name=rtmp location=rtmp://192.168.2.120:1935/test/movie ! flvdemux ! decodebin ! videoconvert",
  65.              ]
  66.  
  67.     p = "videomixer name=mix ! videoconvert ! " + out +  " ! in. ".join(inputs) + " ! in. input-selector sync-streams=true sync-mode=1 name=in ! mix. " + img
  68.     print(p)
  69.     pipeline = Gst.parse_launch (p)
  70.     if pipeline == None:
  71.         print ("Failed to create pipeline")
  72.         sys.exit(0)
  73.  
  74.     # run
  75.     switcher = pipeline.get_by_name('in')
  76.     i = pipeline.get_by_name('img')
  77.     #pads = [ p.get_name() for p in switch.pads if p.get_direction() == Gst.PadDirection.GST_PAD_SINK ]
  78.     pads = [ p.get_name() for p in switcher.pads if p.get_name() != "src" ]
  79.  
  80.     pipeline.set_state(Gst.State.PLAYING)
  81.     t = threading.Thread(target=control, daemon=True)
  82.     t.start()
  83.  
  84.     while True:
  85.         time.sleep(10)
  86.     # cleanup
  87.     pipeline.set_state(Gst.State.NULL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement