4

I am working on a media player and I want to show a preview image as file icon. How can I get the thumbnail of video using Qt or using any Qt tools like QQuick image provider?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

One way is using QAbstractVideoSurface in QMediaPlayer video output but it's very difficult. Example is here: PyQt5 Access Frames with QmediaPlayer Just add additional line in following function:

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())))

Easiest way is using OpenCV library (PyQT6 example):

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)
Oliver
  • 11
  • 2