The openFrameworks's ofVideoPlayer
class is intended for playing and controlling video. The basic usage of the ofVideoPlayer
video object is the following:
video.loadMovie( "video.mov" );
video.play();
testApp::update()
):video.update();
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.
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:
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.