这是我最终找到的解决方案:
子类化 QAbstractVideoSurface:
class alphaVideoDrawer : public QAbstractVideoSurface
{
Q_OBJECT
public:
alphaVideoDrawer(QLabel *displayLbl);
private:
QLabel *displayLbl;
protected:
bool present(const QVideoFrame &frame);
QList<QVideoFrame::PixelFormat> supportedPixelFormats(
QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const
{
Q_UNUSED(handleType);
return QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_ARGB32;
}
};
表面将接收帧转换并将其转发到显示视频的 qlabel。
alphaVideoDrawer::alphaVideoDrawer(QLabel *displayLbl):displayLbl(displayLbl)
{
}
extern QImage qt_imageFromVideoFrame(const QVideoFrame &f);
bool alphaVideoDrawer::present(const QVideoFrame &frame)
{
QImage image = qt_imageFromVideoFrame(frame);
displayLbl->setPixmap(QPixmap::fromImage(image));
return true;
}
然后我们将一个 QLabel 子类化,这将是我们的视频输出:
class alphaVideo : public QLabel
{
Q_OBJECT
public:
alphaVideo(QLabel *parent = nullptr);
private:
alphaVideoDrawer *videoDrawer;
QMediaPlayer *videoPlayer;
QMediaPlaylist *playlist;
};
它加载抽屉和播放器并开始播放/渲染视频:
alphaVideo::alphaVideo(QLabel *parent): QLabel(parent)
{
setStyleSheet("QLabel { background-color : transparent; }");
videoDrawer = new alphaVideoDrawer(this);
videoPlayer = new QMediaPlayer(this);
playlist = new QmediaPlaylist();
videoPlayer->setPlaylist(playlist);
videoPlayer->setVideoOutput(videoDrawer);
playlist->addMedia( Qurl::fromLocalFile(“your RGBA video file.mp4”) );
videoPlayer->play();
}