def libvlc_video_set_callbacks(self, _lockcb, _display, _unLockcb=None, _opaque=None): vlc.libvlc_video_set_callbacks(self.media, _lockcb, _unLockcb, _display, None)
def read_video_vlc(dq, rtsp_addr): vlcInstance = vlc.Instance() # 机场内 # m = vlcInstance.media_new("") # 机场外 # 记得换url,最好也和上面一样进行测试一下 # url = "rtsp://demo.easydss.com:10554/200825" # url = "rtsp://demo.easydss.com:10554/aidong_demo" # url = "rtsp://*****:*****@192.168.199.220/Streaming/Channels/1" url = rtsp_addr m = vlcInstance.media_new(url) mp = vlc.libvlc_media_player_new_from_media(m) # ***如果显示不完整,调整以下宽度和高度的值来适应不同分辨率的图像*** video_width = 1280 video_height = 720 size = video_width * video_height * 4 buf = (ctypes.c_ubyte * size)() buf_p = ctypes.cast(buf, ctypes.c_void_p) VideoLockCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)) @VideoLockCb def _lockcb(opaque, planes): # print("lock", file=sys.stderr) planes[0] = buf_p @vlc.CallbackDecorators.VideoDisplayCb def _display(opaque, picture): img = Image.frombuffer("RGBA", (video_width, video_height), buf, "raw", "BGRA", 0, 1) opencv_image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) print(opencv_image.shape) # print(np.mean(opencv_image)) if type(img) == type(None): print("=====read viedo error!") else: dq.append(opencv_image) # dq.append(opencv_image) # cv2.imshow('image', opencv_image) # cv2.waitKey(50) vlc.libvlc_video_set_callbacks(mp, _lockcb, None, _display, None) mp.video_set_format("BGRA", video_width, video_height, video_width * 4) while True: mp.play() time.sleep(1)
def run(src, shape): """ init VLC media player :param src: :param shape: :return: """ global VIDEOHEIGHT, VIDEOWIDTH, buf, buf_p, RUN VIDEOWIDTH = shape[1] VIDEOHEIGHT = shape[0] size = VIDEOHEIGHT * VIDEOWIDTH * 4 buf = (ctypes.c_ubyte * size)() buf_p = ctypes.cast(buf, ctypes.c_void_p) vlcInstance = vlc.Instance() m = vlcInstance.media_new(src) mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(mp, _lockcb, None, put_queue, None) mp.video_set_format("RGBA", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4) mp.play()
def video_audio_merge_single(self, a): # 删除上一次合成文件 File.del_file_subfix("../data/", ['mp4', 'mp3']) # 等待下载完成 while self.video_down_state == False or self.audio_down_state == False: print("下载中...") time.sleep(2) if self.exit_flag: return self.label_audio.pack_forget() # 合成视频 print('视频合成开始:', video_name) self.label_video.config(text="合成中...") time.sleep(1) ffm = 'ffmpeg ' system = Util.get_platform() if system == 'Windows': # ffm = fr"{self.project_root}\src\3rdParty\ffmpeg_win\bin\ffmpeg.exe " ffm = fr"{self.conf.read_val('LIB', 'lib_ffmpeg')} " # command = ffm + '-i "{}.mp4" -i "{}.mp3" -vcodec copy -acodec copy "../data/tmp.mp4" -vf scale=320:240 \ # "../data/{}.mp4"'.format( # 'tmp', 'tmp', video_name) command = ffm + '-i "{}.mp4" -i "{}.mp3" -vcodec copy -acodec copy "../data/{}.mp4"'.format( 'tmp', 'tmp', video_name) # subprocess.Popen(command, shell=True) child = subprocess.Popen(command, stderr=subprocess.PIPE, universal_newlines=True, shell=True) # child.poll()判断子进程是否结束 while child.poll() is None: line = child.stderr.readline().strip() if self.exit_flag == True: break if line: # 在此可以获取到ffmpeg每一次的信息输出 print(line) # ffmpeg进程结束,关闭流 child.stderr.close() print("视频合成结束:", video_name) self.label_video.config(text="合成结束") time.sleep(1) os.remove(f'tmp.mp3') os.remove(f'tmp.mp4') # 隐藏控件 self.label_video.pack_forget() # 显示视频帧 self._canvas.pack() self._canvas.focus_set() if self.exit_flag == False: # 开启播放线程 # self.player.play(f'../data/{video_name}.mp4') print('开始播放') self.m = self.vlcInstance.media_new(f'../data/{video_name}.mp4') self.mp = vlc.libvlc_media_player_new_from_media(self.m) vlc.libvlc_video_set_callbacks(self.mp, self._lockcb, None, self._display, None) self.mp.video_set_format("BGRA", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4) while True: if self.exit_flag: self.mp.stop() break self.mp.play() time.sleep(1) print('退出播放线程')
img = cv2.CreateImage((VIDEO_WIDTH, VIDEO_HEIGHT), IPL_DEPTH_8U, 4) img.imageData = videobuf cv2.ShowImage(vlc.libvlc_get_version(), img) cv2.WaitKey(10) cv2.ReleaseImage(img) def unlock(data, id, p_pixels): assert id == None if __name__ == '__main__': vlc_args = ["-I", "dummy", "--ignore-config"] arguments = [bytes(a, "utf-8") for a in vlc_args] #videobuf = (char*)malloc((VIDEO_WIDTH * VIDEO_HEIGHT) << 2); #memset(videobuf, 0, (VIDEO_WIDTH * VIDEO_HEIGHT) << 2); instance = vlc.libvlc_new(len(arguments), (ctypes.c_char_p * len(arguments))(*arguments)) media = vlc.libvlc_media_new_location(instance, "rtsp://192.168.200.20:8554/test") mediaPlayer = vlc.libvlc_media_player_new_from_media(media) vlc.libvlc_media_release(media) #libvlc_media_player_set_media(mediaPlayer, media) vlc.libvlc_video_set_callbacks(mediaPlayer, lock, unlock, display, None) vlc.libvlc_video_set_format(mediaPlayer, "RV32", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH << 2) vlc.libvlc_media_player_play(mediaPlayer)
def play(self, url, choose): global allow1, allow2, allow3, allow4, allow5, allow6, allow7, allow8, allow9, allow10, allow11, allow12, allow13, allow14, allow15, allow16, allow17, allow18, allow19, allow20 vlcInstance = vlc.Instance() m = vlcInstance.media_new(url) self.mp = vlc.libvlc_media_player_new_from_media(m) if choose == 1: self.mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(self.mp, _lockcb1, None, _display1, None) allow1 = 1 if choose == 2: self.mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(self.mp, _lockcb2, None, _display2, None) allow2 = 1 if choose == 3: self.mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(self.mp, _lockcb3, None, _display3, None) allow3 = 1 if choose == 4: self.mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(self.mp, _lockcb4, None, _display4, None) allow4 = 1 if choose == 5: self.mp = vlc.libvlc_media_player_new_from_media(m) vlc.libvlc_video_set_callbacks(self.mp, _lockcb5, None, _display5, None) allow5 = 1 if choose == 6: vlc.libvlc_video_set_callbacks(self.mp, _lockcb6, None, _display6, None) allow6 = 1 if choose == 7: vlc.libvlc_video_set_callbacks(self.mp, _lockcb7, None, _display7, None) allow7 = 1 if choose == 8: vlc.libvlc_video_set_callbacks(self.mp, _lockcb8, None, _display8, None) allow8 = 1 if choose == 9: vlc.libvlc_video_set_callbacks(self.mp, _lockcb9, None, _display9, None) allow9 = 1 if choose == 10: vlc.libvlc_video_set_callbacks(self.mp, _lockcb10, None, _display10, None) allow10 = 1 if choose == 11: vlc.libvlc_video_set_callbacks(self.mp, _lockcb11, None, _display11, None) allow11 = 1 if choose == 12: vlc.libvlc_video_set_callbacks(self.mp, _lockcb12, None, _display12, None) allow12 = 1 if choose == 13: vlc.libvlc_video_set_callbacks(self.mp, _lockcb13, None, _display13, None) allow13 = 1 if choose == 14: vlc.libvlc_video_set_callbacks(self.mp, _lockcb14, None, _display14, None) allow14 = 1 if choose == 15: vlc.libvlc_video_set_callbacks(self.mp, _lockcb15, None, _display15, None) allow15 = 1 if choose == 16: vlc.libvlc_video_set_callbacks(self.mp, _lockcb16, None, _display16, None) allow16 = 1 if choose == 17: vlc.libvlc_video_set_callbacks(self.mp, _lockcb17, None, _display17, None) allow17 = 1 if choose == 18: vlc.libvlc_video_set_callbacks(self.mp, _lockcb18, None, _display18, None) allow18 = 1 if choose == 19: vlc.libvlc_video_set_callbacks(self.mp, _lockcb19, None, _display19, None) allow19 = 1 if choose == 20: vlc.libvlc_video_set_callbacks(self.mp, _lockcb20, None, _display20, None) allow20 = 1 self.mp.video_set_format("BGRA", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4) self.mp.play()