Advertisement
Guest User

VLC Set Callback example

a guest
Jan 28th, 2019
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import vlc
  2. import ctypes
  3. import time
  4. import sys
  5. import numpy as np
  6.  
  7. from PIL import Image
  8.  
  9. VIDEOWIDTH = 1920
  10. VIDEOHEIGHT = 1080
  11. URI0 = 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov'
  12. CorrectVideoLockCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p))
  13.  
  14. # global hastable to look up video readers
  15. VLCReader_Table = {}
  16.  
  17. class VLCReader:
  18.     def __init__(self,uri=URI0):
  19.         global VLCReader_Table
  20.         _params = '-I dummy --ignore-config --no-audio --verbose 0 --rtsp-tcp'
  21.         self.instance = vlc.Instance(_params)
  22.         self.mp = self.instance.media_player_new()
  23.         self.media = self.instance.media_new(uri)
  24.         self.mp.set_media(self.media)
  25.         self.size = VIDEOHEIGHT*VIDEOWIDTH*3
  26.         self.buf = (ctypes.c_ubyte*self.size)()
  27.         self.buf_p = ctypes.cast(self.buf, ctypes.c_void_p)
  28.         self.frame_num = 0
  29.         mem_addr = self.buf_p.value
  30.         VLCReader_Table[mem_addr] = self
  31.         vlc.libvlc_video_set_callbacks(self.mp, self._lockcb, None, self._display, self.buf_p)
  32.         self.mp.video_set_format("RV24", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 3)
  33.  
  34.     @CorrectVideoLockCb
  35.     def _lockcb(opaque, planes):
  36.         global VLCReader_Table
  37.         _obj = VLCReader_Table[opaque]
  38.         planes[0] = _obj.buf_p
  39.  
  40.     @vlc.CallbackDecorators.VideoDisplayCb
  41.     def _display(opaque, picture):
  42.         global VLCReader_Table
  43.         _obj = VLCReader_Table[opaque]
  44.         _obj.frame_num += 1
  45.         img = Image.frombuffer("RGB", (VIDEOWIDTH, VIDEOHEIGHT), _obj.buf, "raw", "BGR", 0, 1)
  46.         _obj.img = np.array(img)
  47.  
  48.     def play(self,showtime=10):
  49.         self.mp.play()
  50.  
  51.     def frame(self):
  52.         return self.img
  53.  
  54. if __name__ == "__main__":
  55.     import cv2
  56.    
  57.     lk = URI0
  58.  
  59.     vr = VLCReader(uri=lk)
  60.  
  61.     vr.play()
  62.     time.sleep(5)
  63.  
  64.     while True:
  65.         m = vr.frame()
  66.         if m is None:
  67.             print("wtf")
  68.         else:
  69.             print(m.shape)
  70.             print(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement