基于Python制作簡易視頻播放器
先上效果圖:
這個就是用python-pyqt5-opencv做出來的簡易視頻播放器,主要實現(xiàn)本地視頻文件播放、本地攝像頭播放和遠程攝像頭播放三個功能。
核心代碼:
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('開始讀取攝像頭數(shù)據(jù)......') 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()函數(shù),唯一的區(qū)別就是函數(shù)中的url參數(shù)不同。
文件播放:url=文件名
本地相機:url=0
網(wǎng)絡(luò)串流:url=‘rtsp://……’
除了這個核心代碼,打開文件使用的是QFileDialog,打開網(wǎng)絡(luò)串流使用的是自定義的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('打開網(wǎng)絡(luò)串流') input_dialog.setLabelText('請輸入網(wǎng)絡(luò)串流地址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界面的定義了
到此這篇關(guān)于基于Python制作簡易視頻播放器的文章就介紹到這了,更多相關(guān)Python視頻播放器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python單線程下實現(xiàn)多個socket并發(fā)過程詳解
這篇文章主要介紹了python單線程下實現(xiàn)多個socket并發(fā)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07python3 selenium自動化測試 強大的CSS定位方法
今天小編就為大家分享一篇python3 selenium自動化測試 強大的CSS定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)在字典中將鍵映射到多個值上的方法
這篇文章主要介紹了Python在字典中將鍵映射到多個值上的方法,涉及Python針對字典的相關(guān)映射與初始化相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python中Tkinter復(fù)選框Checkbutton是否被選中判斷
這篇文章主要介紹了python中Tkinter復(fù)選框Checkbutton是否被選中判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01