问题
I am writing an application which will allow the user to scrub through an open video. Developing on Windows 7/8 with Qt 5.3, I have been using QMediaPlayer
and QVideoWidget
following the qvideowidget
example project. The result has been pretty good, except that the QVideoWidget
seems only to update during idle time. Still, it's a good start and it's usable.
However, when I build on Mac OS 10.10 (again with Qt 5.3), scrubbing behaves as though there were only one frame per second in the video. As I drag the "position" slider, the video jumps from one frame to the frame one second later, then one second after that, even though I am calling QMediaPlayer::setPosition
several times with positions between those two frames.
The problem can be reproduced using the videowidget
example that ships with Qt 5.3 here: Qt\Examples\Qt-5.3\multimediawidgets\videowidget
. When the slider is dragged on a Windows machine, the QVideoWidget
moves between frames that are spaced fairly close together. When the slider is dragged on a Mac (at least on mine), the QVideoWidget
jumps between frames spaced about one second apart. No matter how long I wait for an "in between" frame to render, it won't happen unless I hit the "play" button.
I've tried calling QMediaPlayer::play()
and QMediaPlayer::pause()
one after the other to force an update, but this doesn't seem to work--QMediaPlayer
works asynchronously, so the update doesn't have time to take effect.
If I check the value of QMediaPlayer::position
, I find that it actually doesn't change between these jumps. It appears that when I call QMediaPlayer::setPosition
, it is actually rounding the position to one second increments on a Mac and finer increments on a Windows machine.
Ideally, I would like to jump to a particular position in the video and render that frame immediately on the QVideoWidget
. Is there any way to force QMediaPlayer
to set the position accurately and update the associated QVideoWidget
? Is there a better way to implement smooth scrubbing in a video?
Thanks for your help!
回答1:
In case anyone else has a similar problem...
My best guess is that the issue stems from limitations in the codec used by QMediaPlayer
, since this seems to be the main difference between the two platforms. Rather than deal with the codec issues directly, I looked around for other options.
MLT
(http://www.mltframework.org/) seemed promising, but it is a major pain to compile and the primary author seems to have settled on offering SDK support to commercial users only.
libVLC
(https://wiki.videolan.org/LibVLC/) looks a lot better. In particular, I’ve been using vlc-qt
(https://github.com/ntadej/vlc-qt). The latter has an interface that will look quite familiar to users of QMediaPlayer
and QVideoWidget
. It was an easy replacement in my own application, and the result was much smoother video scrubbing on both Windows and Mac.
Hope this helps someone else!
来源:https://stackoverflow.com/questions/27006902/force-qmediaplayer-to-update-position-accurately-for-video-scrubbing-application