Loading and displaying video

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.

Getting ready

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.

How to do it…

We will use Cinder's QuickTime wrappers to load and display vido.

  1. Include the headers containing the Quicktime and OpenGL functionality by adding the following at the beginning of the source file:
    #include "cinder/qtime/QuickTime.h"
    #include "cinder/gl/gl.h"
    #include "cinder/gl/Texture.h"
  2. Declare a 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;
    };
  3. To load the video as a resource use the 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;
        }
  4. You can also load the video by using a file open dialog and passing the file path as an argument when constructing the 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;
        }
  5. To play the video, call the 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();
    }
  6. In the 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();
    }
  7. To draw the movie we simply need to draw our texture on screen using the method 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 );
    }

How it works…

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.

There's more

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.96.155