我正在使用媒体播放器,我想将预览图像显示为文件图标。如何使用 Qt 或使用任何 Qt 工具(如 QQuick 图像提供程序)获取视频的缩略图?
一种方法是在QMediaPlayer 视频输出中使用QAbstractVideoSurface,但这非常困难。示例在这里:PyQt5 Access Frames with QmediaPlayer 只需在以下函数中添加额外的行:
def process_frame(self, image):
# add this line:
image = image.scaled(QSize(200,200), Qt.KeepAspectRatio, Qt.SmoothTransformation)
# Save image here
image.save('c:/temp/{}.jpg'.format(str(uuid.uuid4())))
最简单的方法是使用 OpenCV 库(PyQT6 示例):
import cv2
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QLabel
path = "/your_video_path.mp4"
max_size = (200,200) # set your thumbnail size
video = cv2.VideoCapture(path)
success, img = video.read()
if success:
img = cv2.resize(img,max_size,interpolation=cv2.INTER_AREA)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
qt_img = QImage(img.data, img.shape[1], img.shape[0], QImage.Format.Format_RGB888)
# save it:
qt_img.save(newpath, "JPG", 70)
# or convert it to QPixmap and show in QLabel:
pixmap = QPixmap.fromImage(qt_img)
label = QLabel()
label.setPixmap(pixmap)