先上效果图:
这个就是用python-pyqt5-opencv做出来的简易视频播放器,主要实现本地视频文件播放、本地摄像头播放和远程摄像头播放三个功能。
核心代码:
def showcamera(self, url): try: if url == none: self.cap = cv2.videocapture(0) else: self.cap = cv2.videocapture(url) print('摄像头是否开启: {}'.format(self.cap.isopened())) if self.cap.isopened: self.cap.set(cv2.cap_prop_frame_width, 640) self.cap.set(cv2.cap_prop_frame_height, 480) self.cap.set(cv2.cap_prop_fps, 25) print(self.cap.get(3)) print(self.cap.get(4)) print(self.cap.get(5)) print('开始读取摄像头数据......') while(true): ret, color_frame = self.cap.read() if ret == false: return if url == none: color_frame = cv2.flip(color_frame, 1) cv2.waitkey(1) im = cv2.cvtcolor(color_frame, cv2.color_rgb2bgr) a = qimage(im.data, im.shape[1], im.shape[0], qimage.format_rgb888) self.setpic(a) self.cap.release() else: print('camera open failed') except exception as e: print(str(e))
三类播放使用的都是同一个showcamera()函数,唯一的区别就是函数中的url参数不同。
文件播放:url=文件名
本地相机:url=0
网络串流:url=‘rtsp://……’
除了这个核心代码,打开文件使用的是qfiledialog,打开网络串流使用的是自定义的qinputdialog,两个代码如下:
def openfile(self): filename, filetype = qfiledialog.getopenfilename(self, '选择文件') print(filename, filetype) self.showcamera(filename)
def remote(self): input_dialog = qtwidgets.qinputdialog(self) input_dialog.setinputmode(qinputdialog.textinput) input_dialog.setwindowtitle('打开网络串流') input_dialog.setlabeltext('请输入网络串流地址rtsp://') input_dialog.setfixedsize(500, 100) input_dialog.show() if input_dialog.exec_() == input_dialog.accepted: text = input_dialog.textvalue() if text != '': print(text) self.showcamera(text) else: print('地址错误或空')
最后,是用qlabel加载图片的代码:
def setpic(self, image): self.label.setpixmap(qpixmap.fromimage(image))
剩下的就是ui界面的定义了
到此这篇关于基于python制作简易视频播放器的文章就介绍到这了,更多相关python视频播放器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论