目錄
1--基于Qt Designer設(shè)計(jì)ui文件
2--代碼
3--結(jié)果
4--補(bǔ)充
5--加載、播放、轉(zhuǎn)換和保存視頻的實(shí)例
1--基于Qt Designer設(shè)計(jì)ui文件
2--代碼
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5 import uic
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.ui = uic.loadUi("./video.ui") # 加載由Qt Designer設(shè)計(jì)的ui文件
# 加載自定義ui屬性
self.openGLWidget = self.ui.openGLWidget
self.video_btn1 = self.ui.pushButton
self.video_btn2 = self.ui.pushButton_2
# 創(chuàng)建一個(gè)布局將 QVideoWidget 內(nèi)嵌到 自定義ui的Widget中
layout = QHBoxLayout()
self.vw = QVideoWidget()
layout.addWidget(self.vw)
self.openGLWidget.setLayout(layout)
# img_btn1 綁定槽函數(shù) loadVideo()
self.video_btn1.clicked.connect(self.loadVideo)
# img_btn2 綁定槽函數(shù) playVideo()
self.video_btn2.clicked.connect(self.playVideo)
def loadVideo(self):
self.player = QMediaPlayer()
self.player.setVideoOutput(self.vw) # 視頻播放的widget
self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0])) # 選取視頻文件
def playVideo(self):
self.player.play() # 播放視頻
self.vw.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.ui.show()
sys.exit(app.exec_())
3--結(jié)果
4--補(bǔ)充
① 上述代碼只實(shí)現(xiàn)了加載視頻和播放視頻的功能,缺少暫停、進(jìn)度移動(dòng)等常見功能。
② 上述代碼將 QVideoWidget 通過一個(gè)layout布局的形式內(nèi)嵌到自定義的 QOpenGLWidget 中,但播放測試視頻的時(shí)候存在視頻無法覆蓋 Widget 的問題。
5--加載、播放、轉(zhuǎn)換和保存視頻的實(shí)例
① 基于Qt Designer 設(shè)計(jì) ui 文件
② 代碼
注:代碼具有保存視頻的功能,轉(zhuǎn)換視頻則采取了最簡單的灰度化處理操作作為功能展示。
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5 import uic
import sys
import cv2
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.ui = uic.loadUi("./video.ui") # 加載由Qt Designer設(shè)計(jì)的ui文件
# 加載自定義ui屬性
self.openGLWidget1 = self.ui.openGLWidget
self.QGraphView = self.ui.graphicsView
self.video_btn1 = self.ui.pushButton
self.video_btn2 = self.ui.pushButton_2
self.gray_btn3 = self.ui.pushButton_3
self.gray_btn4 = self.ui.pushButton_4
# 創(chuàng)建一個(gè)布局將 QVideoWidget 內(nèi)嵌到 自定義ui的Widget1中
layout1 = QHBoxLayout()
self.vw1 = QVideoWidget()
layout1.addWidget(self.vw1)
self.openGLWidget1.setLayout(layout1)
# video_btn1 綁定槽函數(shù) loadVideo()
self.video_btn1.clicked.connect(self.loadVideo)
# video_btn2 綁定槽函數(shù) playVideo()
self.video_btn2.clicked.connect(self.playVideo)
# gray_btn3 綁定槽函數(shù) convert_gray()
self.gray_btn3.clicked.connect(self.convert_gray)
# gray_btn4 綁定槽函數(shù) save_gray()
self.gray_btn4.clicked.connect(self.save_gray)
def loadVideo(self):
self.player = QMediaPlayer()
self.player.setVideoOutput(self.vw1) # 視頻播放的widget
self.video_file = QFileDialog.getOpenFileUrl()[0]
self.video_path = self.video_file.toString()[8:]
print(self.video_path)
self.player.setMedia(QMediaContent(self.video_file))# 選取視頻文件
def playVideo(self):
self.player.play() # 播放視頻
self.vw1.show()
def convert_gray(self):
cap = cv2.VideoCapture(self.video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度圖轉(zhuǎn)換
frame = QImage(gray.data, gray.shape[1], gray.shape[0], gray.strides[0], QImage.Format_Indexed8)
pix = QPixmap.fromImage(frame)
item = QGraphicsPixmapItem(pix) # 創(chuàng)建像素圖元
scene = QGraphicsScene() # 創(chuàng)建場景
scene.addItem(item)
self.QGraphView.setScene(scene) # 將場景添加至視圖
self.QGraphView.fitInView(item) # 自適應(yīng)大小
cv2.waitKey(int((1/fps)*1000))
cap.release()
def save_gray(self):
self.output_path = self.video_path.rsplit("/", 1)[0] + '/gray_' + self.video_path.rsplit("/", 1)[1]
cap = cv2.VideoCapture(self.video_path)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
writer = cv2.VideoWriter(filename = self.output_path, fourcc = fourcc, fps = fps,
frameSize = (int(w), int(h)), isColor = 0)
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 灰度圖轉(zhuǎn)換
writer.write(gray) # 保存灰度圖
cap.release()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyWindow()
w.ui.show()
sys.exit(app.exec_())
?③ 結(jié)果展示:
文章來源:http://www.zghlxwxcb.cn/news/detail-608618.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-608618.html
到了這里,關(guān)于PyQt5學(xué)習(xí)筆記--基于Qt Designer加載、播放和保存視頻的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!