Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import vlc
- import ctypes
- import time
- import sys
- import numpy as np
- from PIL import Image
- VIDEOWIDTH = 1920
- VIDEOHEIGHT = 1080
- URI0 = 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov'
- CorrectVideoLockCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p))
- # global hastable to look up video readers
- VLCReader_Table = {}
- class VLCReader:
- def __init__(self,uri=URI0):
- global VLCReader_Table
- _params = '-I dummy --ignore-config --no-audio --verbose 0 --rtsp-tcp'
- self.instance = vlc.Instance(_params)
- self.mp = self.instance.media_player_new()
- self.media = self.instance.media_new(uri)
- self.mp.set_media(self.media)
- self.size = VIDEOHEIGHT*VIDEOWIDTH*3
- self.buf = (ctypes.c_ubyte*self.size)()
- self.buf_p = ctypes.cast(self.buf, ctypes.c_void_p)
- self.frame_num = 0
- mem_addr = self.buf_p.value
- VLCReader_Table[mem_addr] = self
- vlc.libvlc_video_set_callbacks(self.mp, self._lockcb, None, self._display, self.buf_p)
- self.mp.video_set_format("RV24", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 3)
- @CorrectVideoLockCb
- def _lockcb(opaque, planes):
- global VLCReader_Table
- _obj = VLCReader_Table[opaque]
- planes[0] = _obj.buf_p
- @vlc.CallbackDecorators.VideoDisplayCb
- def _display(opaque, picture):
- global VLCReader_Table
- _obj = VLCReader_Table[opaque]
- _obj.frame_num += 1
- img = Image.frombuffer("RGB", (VIDEOWIDTH, VIDEOHEIGHT), _obj.buf, "raw", "BGR", 0, 1)
- _obj.img = np.array(img)
- def play(self,showtime=10):
- self.mp.play()
- def frame(self):
- return self.img
- if __name__ == "__main__":
- import cv2
- lk = URI0
- vr = VLCReader(uri=lk)
- vr.play()
- time.sleep(5)
- while True:
- m = vr.frame()
- if m is None:
- print("wtf")
- else:
- print(m.shape)
- print(m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement