Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Convert YouTube (Flash) video frames to QImage problems
Pt_develop last edited by
For a project I'm working on I need to convert a YouTube (Flash) video frame to a QImage. I'm using the method suggested by the Qt documentation for QWebPage but am experiencing problems and would appreciate some help. The method is straightforward and works great for web images but fails for videos under certain conditions (see below). I created a simple basic test that demonstrates the problem. The test code (sans debug statements) is:
@class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0);
void loadDone(bool ok);
@MainWindow::MainWindow(QWidget *parent) :
// QUrl url("http://www.w3schools.com/tags/...");
// QUrl url("http://..happy2.png");
// QUrl url = QUrl::fromLocalFile("C:/...green.html");
// QUrl url = QUrl::fromLocalFile("C:/...green1.html");
// QString ht = "<img >";
// QString ht = "YouTube embedded code";
void MainWindow::loadDone(bool ok)
}// end load done
QImage image(sz,QImage::Format_ARGB32); QPainter painter(&image); ui->webView->page()->mainFrame()->render(&painter); image.save("webRendered.png"); ui->label->setPixmap(QPixmap::fromImage(image));
}// end loadfin
The file paths have been abbreviated. Several test cases are hard coded and I add and delete comment lines as necessary to run the tests. The results puzzle me. The code correctly creates an image from from either a url or when the HTML is loaded using the webpage setHtml method but if the HTML contains a Flash video then things get puzzling.
The method fails completely if the reference to the flash video is generated from a local file or using the setHtml method (e.g., using the YouTube embedded HTML). Note: QWebView correctly displays the video in all cases. It is the render to the QImage that fails. However if the video is referenced directly over the web then the render sometimes works and sometimes doesn't.
Thinking that it may be a synchronization problem I added an 1 second delay using the loadDone slot but that doesn't make a difference. The various test cases I ran generated these conclusions (I actually ran more test cases than indicated in the above).
These results generate the following questions:
- Can anyone explain the above behavior?
- Does anyone have any suggestions as to how I can capture a Flash video frame in a QImage using Qt 4.8? I have searched the internet but have found no useful suggestions.
chiyuwang last edited by
I think the url for local file, you have to put file:/// as prefix, I guess.
Pt_develop last edited by
Making some progress. The problem appears to be a combination of a sync problem and render problem. A more efficient way to get a QPixmap is to use either the static function QPixmap::grabWidget(QWidget*) or the static function QPixmap::grabWindow(WId).
The grabWidget function has all the same problems as the QPage method outlined in the original post. The grabWindow function grabs screen pixels which isn't ideal but somewhat workable provided that the YouTube video is visible. Therein lies another problem.
In order to use the grabWindow method one has to wait until the YouTube player has displayed the video which takes some time. On my system it takes as long as 10 seconds provided that the internet connect is viable. Therefore one would like to have a signal when the display is visible.
The problem is that the YouTube player doesn't call the callback function when I'm using QWebView to display the YouTube video. At this point I'm stumped. I don't know if the reason the player doesn't call the callback function is because of a security problem or something stupid that I've done.
For reference here is the html I'm currently using (just the latest in many variations I've tried):
@<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<object id="objid" width="420" height="315">
<param id="paramid" name="movie" value="http://www.youtube.com/v/fwbH8dgASRc?enablejsapi=1&playapiid=player1&version=3&hl=en_US&rel=0"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed id="embid" src="http://www.youtube.com/v/fwbH8dgASRc?enablejsapi=1&playapiid=player2&version=3&hl=en_US&rel=0"
(modified from the suggested code on the YouTube site to include playerid and enablejsapi).
So the original post has morphed into the following two questions:
Is it possible to somehow grab the video display without making the QWebView object visible?