Playing a video file

The openFrameworks's ofVideoPlayer class is intended for playing and controlling video. The basic usage of the ofVideoPlayer video object is the following:

  1. Loading video file, specifying its name:
    video.loadMovie( "video.mov" ); 
  2. Starting video to play:
    video.play();
  3. Decoding the needed frame to show and playing the corresponding sound chunk (best to call it in testApp::update()):
    video.update();
  4. Drawing the current video frame:
    video.draw( x, y );

    or

    video.draw( x, y, w, h );

While drawing a video frame, you can think of the current frame of video as if it were an ordinal ofImage object. So, you can use the video.width, video.height values and set anchor using video.setAnchorPercent( percentX, percentY ), video.setAnchorPoint( x, y ), and video.resetAnchor().

The following example shows the basic usage of ofVideoPlayer. It is based on an emptyExample project in openFrameworks. Before running it, copy the handsTrees.mov file into the bin/data folder of your project.

Note

This is example 05-Video/01-VideoPlayback.

In the testApp.h file, inside the testApp class declaration, add the following line with video player object video declaration:

ofVideoPlayer video; //Declare the video player object

In the testApp.cpp file, fill the bodies of the setup(), update(), and draw() functions in the following way:

void testApp::setup(){
  video.loadMovie( "handsTrees.mov" );    //Load the video file
  video.play();     //Start the video to play
}

void testApp::update(){
  video.update();    //Decode the new frame if needed
}

void testApp::draw(){
  ofBackground( 255, 255, 255 );    //Set white background
  
  ofSetColor( 255, 255, 255 );
  video.draw( 0, 0 );              //Draw the current video frame
}

When you run the code, you will see the movie playing on the screen:

Playing a video file

Please note, that by default, ofVideoPlayer plays video with its speed based on the time data, independently of your application rendering rate. For example, if you set the application framerate to 60 by calling ofSetFrameRate( 60 ), but the video has the rate 30 fps, then video.update() will switch frames at rate 30, not 60 fps. So it is useful to know, whether the frame is new or not. Such information can be obtained using the Boolean function video.isFrameNew(), which returns true if a new frame was loaded during the last video.update() calling. See the example of using the video.isFrameNew() function in the The replacing colors example section and other examples given in the following sections.

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

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