In this recipe, we will learn how to load a video from a file and display it on screen using Quicktime and OpenGL. We'll learn how to load a file as a resource or from a file selected by the user using a file open dialog.
You need to have QuickTime installed and also a video file in a format compatible with QuickTime.
To load the video as a resource it is necessary to copy it to the resources
folder in your project. To learn more on resources, please read the recipes Using resources on Windows and Using resources on OSX and iOS from Chapter 1, Getting Started.
We will use Cinder's QuickTime wrappers to load and display vido.
#include "cinder/qtime/QuickTime.h" #include "cinder/gl/gl.h" #include "cinder/gl/Texture.h"
ci::qtime::MovieGl
member in you application's class declaration. This example will only need the setup
, update
, and draw
methods, so make sure at least these are declared:using namespace ci; using namespace ci::app; class MyApp : public AppBasic { public: void setup(); void update(); void draw(); qtime::MovieGl mMovie; gl::Texture mMovieTexture; };
ci::app::loadResource
method with the file name as parameter
and pass the resulting ci::app::DataSourceRef
when constructing the movie object. It is also good practice to place the loading resource inside a trycatch
segment in order to catch any resource loading errors. Place the following code inside your setup
method:try{ mMovie = qtime::MovieGl( loadResource( "movie.mov" ) ); } catch( Exception e){ console() <<e.what()<<std::endl; }
mMovie
object. Your setup
would instead have the following code:try{ fs::path path = getOpenFilePath(); mMovie = qtime::MovieGl( path ); } catch( Exception e){ console() <<e.what()<<std::endl; }
play
method on the movie object. You can test the successful instantiation of mMovie
by placing it inside an if
statement just like an ordinary pointer:If( mMovie ){ mMovie.play(); }
update
method we copy the texture of the current movie frame into our mMovieTexture
to draw it later:void MyApp::update(){ if( mMovie ){ mMovieTexture = mMovie.getTexture(); }
gl::draw
. We need to check if the texture is valid because mMovie
may take a while to load. We'll also create ci::Rectf
with the texture size and center it on screen to keep the drawn video centered without stretching:gl::clear( Color( 0, 0, 0 ) ); if( mMovieTexture ){ Rect frect = Rectf( mMovieTexture.getBounds() ).getCenteredFit( getWindowBounds(), true ); gl::draw( mMovieTexture, rect ); }
The ci::qtime::MovieGl
class allows playback and control of movies by wrapping around the QuickTime framework. Movie frames are copied into OpenGl textures for easy drawing. To access the texture of the current frame of the movie use the method ci::qtime::MovieGl::getTexture()
which returns a ci::gl::Texture
object. Textures used by ci::qtime::MovieGl
are always bound to the GL_TEXTURE_RECTANGLE_ARB
target.
If you wish to do iterations over the pixels of a movie consider using the class ci::qtime::MovieSurface
. This class allows playback of movies by wrapping around the QuickTime framework, but converts movie frames into ci::Surface
objects. To access the current frame's surface, use the method ci::qtime::MovieSurface::getSurface()
which returns a ci::Surface
object.
18.226.96.155