我正在使用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();
}

所有提取的帧也是口吃的,例如:

但是当我使用谷歌libwebp工具提取帧时,没有任何问题。
// 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表示该错误已在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.添加一些代码
// ${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
  • 替换qwebp.dll
  • mv ${QT_DIR}/plugins/imageformats/qwebpd.dll ${QT_DIR}/plugins/imageformats/qwebpd.dll.bakcp ${REBUILD_WEBP_PLUGINS} ${QT_DIR}/plugins/imageformats/qwebpd.dll

    07-28 01:33