赞
踩
读取图片
读取视频
录制视频
- import os
- import sys
- from PyQt5.QtWidgets import *
- from PyQt5.QtGui import *
- from PyQt5.QtCore import *
- import cv2 as cv
- import numpy as np
- from init import picturepath
- from ffpyplayer.player import MediaPlayer
- # 路径有中文,cv,imread,读不出
- def imread(path):
- img = cv.imdecode(np.fromfile(path, dtype=np.uint8), -1)
- return img
-
-
- class a(QWidget):
- def __init__(self):
- super().__init__()
- self.initui()
-
- def initui(self):
- self.resize(1085, 729)
- self.setWindowTitle("视频与图片")
-
- self.play_video = QPushButton(self)
- self.play_video.setText("播放视频")
- self.play_video.clicked.connect(self.ok)
- self.play_video.setGeometry(30, 40, 93, 28)
-
- self.open_pic = QPushButton(self)
- self.open_pic.clicked.connect(self.pic)
- self.open_pic.setGeometry(130, 40, 93, 28)
-
- self.open_capture = QPushButton(self)
- self.open_capture.setGeometry(240, 40, 93, 28)
- self.open_capture.setText("录制视频")
- self.open_capture.clicked.connect(self.get_video)
-
-
- self.save = QLineEdit(self)
- self.save.setPlaceholderText('写视频的名字(1.mp4)')
- self.save.setGeometry(350, 40, 160, 28)
-
- self.label = QLabel(self)
- self.open_pic.setText("打开图片")
- # 录制视频
- def get_video(self):
- if self.save.text():
- video = cv.VideoCapture(0)
- fourcc = cv.VideoWriter_fourcc(*'mp4v')
- save = cv.VideoWriter(f'{self.save.text()}.mp4', fourcc, 24, (640, 480))
- while True:
- ok, frame = video.read()
- if not ok:
- break
- self.label.setGeometry(30,100,640,480)
- data = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
- img = QImage(frame,640,400, QImage.Format_RGB888)
- self.label.setPixmap(QPixmap(img).scaled(640,480))
- save.write(frame)
- if cv.waitKey(1) == 27:
- break
- video.release()
- save.release()
- cv.destroyAllWindows()
- else:
- QMessageBox.information(self, '消息', '视频的名字为空', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
- # 打开视频,有声音
- def ok(self):
- filename, ok = QFileDialog.getOpenFileName(self, '打开视频', os.getcwd(), '(*.mp4 *.avi)')
- if ok:
- video = cv.VideoCapture(filename)
- z = video.get(5)
- player=MediaPlayer(filename)
- while True:
- yes,data = video.read()
- frame, val=player.get_frame()
- if val=='eof':
- break
- if not yes:
- break
- data = cv.cvtColor(data, cv.COLOR_BGR2RGB)
- h,w,_=data.shape
- self.label.setGeometry(500,0, h, w)
- img = QImage(data.data,w,h, QImage.Format_RGB888)
- self.label.setPixmap(QPixmap(img).scaled(w,h))
- key = cv.waitKey(1000 // int(z))
- video.release()
- cv.destroyAllWindows()
- img = imread(picturepath+'/31.jpg')
- w, h, _= img.shape
- self.label.setGeometry(30, 100,int(h/2),int(w/2))
- self.label.setPixmap(QPixmap(picturepath+'/31.jpg').scaled(self.label.width(),self.label.width(),self.label.height()))
- else:
- pass
- # 打开图片
- def pic(self):
- filename, ok = QFileDialog.getOpenFileName(self, '打开图片', os.getcwd(), '(*.jpg *.png)')
- if ok:
- img = imread(filename)
- w, h, _= img.shape
- self.label.setGeometry(30, 100,int(h/2),int(w/2))
- self.label.setPixmap(QPixmap(filename).scaled(self.label.width(),self.label.height()))
- else:
- pass
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
- app.setStyle(QStyleFactory.create('Fusion'))
- a = a()
- a.show()
- sys.exit(app.exec_())
1,用了文件对话框,选择视频或者图片文件。
2,opencv读的是图片,没有声音,用了第三方库ffpyplayer,我运行的时候有bug
[ffpyplayer_abuffersink @ 000002446343da40] Removing channel layout 0x3, redundant with 2 channels
不知道什么情况,没有解决
主页面
打开图片
选择不同的图片,label会根据图片动态变化大小。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。