欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python制作簡易視頻播放器

 更新時間:2024年11月18日 09:27:08   作者:天颶  
本文將用python-pyqt5-opencv做出來的簡易視頻播放器,主要實現(xiàn)本地視頻文件播放、本地攝像頭播放和遠程攝像頭播放三個功能,需要的可以參考下

先上效果圖:

 這個就是用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)文章

最新評論