QMovie webp帧断断续续

QMovie webp frame stutters

我正在使用 QMovie 在 QLabal 上播放 webp 动画,代码如下:

Foo::Foo() {
    movie_ = new QMovie("/path/to/my.webp", "", this);
    ui->label->setMovie(movie_);
}
void Foo::on_pushButton_clicked() {
    movie_->stop();
    movie_->start();
}

但是当我播放动画时,帧断断续续,例如:

我尝试使用 QImage::save 提取 webp 帧,代码如下:

QImageReader *reader = new QImageReader("/path/to/my.webp");
reader->setDecideFormatFromContent(true);
for (int i = 0; i < reader->imageCount(); ++i) {
    QImage image;
    reader->read(&image);
    image.save(QString("frame_%1.png").arg(i));
    reader->jumpToNextImage();
}

所有提取的帧也都是卡顿的,例如:

但是当我使用google libwebp工具提取帧时,没有任何problem.like:

// extract frame 9, not problem
webpmux.exe -get frame 9  -o "frame_9.webp"
// paly webp animation, there is no any problem
vwebp.exe /path/to/my.webp

是Qt的问题吗? Qt 5.9.4(我正在使用)和Qt最新版本(我正在尝试)都有这个问题。

这似乎是一个 Qt 错误,该错误已在 Qt 版本 5.9.1 上报告: WebP: animations don't respect alpha

Qt说这个bug在5.9.2版本解决了,但是Qt 5.11问题依然存在

官方有solution:

diff --git a/src/plugins/imageformats/webp/qwebphandler.cpp b/src/plugins/imageformats/webp/qwebphandler.cpp
index 5a0ae4a..ce90158 100644
--- a/src/plugins/imageformats/webp/qwebphandler.cpp
+++ b/src/plugins/imageformats/webp/qwebphandler.cpp
@@ -122,6 +122,8 @@ bool QWebpHandler::ensureScanned() const
                 that->m_bgColor = QColor::fromRgba(QRgb(WebPDemuxGetI(m_demuxer, WEBP_FF_BACKGROUND_COLOR)));

                 that->m_composited = new QImage(that->m_features.width, that->m_features.height, QImage::Format_ARGB32);
+                if (that->m_features.has_alpha)
+                    that->m_composited->fill(Qt::transparent);

                 // We do not reset device position since we have read in all data
                 m_scanState = ScanSuccess;
@@ -193,6 +195,8 @@ bool QWebpHandler::read(QImage *image)
     } else {
         // Animation
         QPainter painter(m_composited);
+        if (m_features.has_alpha && m_iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND)
+            m_composited->fill(Qt::transparent);
         painter.drawImage(currentImageRect(), frame);

         *image = *m_composited;

这是我的解决方案:

1.add 一些代码

// ${QT_SRC}/qtimageformats/src/plugins/imageformats/webp/qwebphandler.cpp
// function QWebpHandler::read
if (m_features.has_alpha && (m_iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND || 
            m_iter.blend_method == WEBP_MUX_NO_BLEND)) {
      m_composited->fill(Qt::transparent);
}

2。重建 qt webp 插件

nmake module-qtimageformats

  1. 替换qwebp.dll

mv ${QT_DIR}/plugins/imageformats/qwebpd.dll ${QT_DIR}/plugins/imageformats/qwebpd.dll.bak

cp ${REBUILD_WEBP_PLUGINS} ${QT_DIR}/plugins/imageformats/qwebpd.dll