2. ofVideoPlayer
openFrameworks allows you to easily display
videos from file or grab a video stream from a
webcam. For playing videos from file you use
ofVideoPlayer to grab video from your webcam you
use ofVideoGrabber. The ofVideoPlayer class looks
follows the same API as ofSoundPlayer which
makes it really easy to work with video and sound.
3. Playing a video
To playback a video you create a ofVideoPlayer
member and call loadMovie to load a movie from
disk. Call play to start playing the movie. In
testApp::update you need to update the playhead for
the video player as well by calling update on your
ofVideoPlayer object.
testApp.h testApp.cpp
class testApp : public ofBaseApp{ void testApp::setup(){
public:
my_video.loadMovie("fingers.mov");
ofVideoPlayer my_video;
my_video.play();
} }
void testApp::draw(){
my_video.draw(10,10);
}
4. Playing a video
To playback a video you create a ofVideoPlayer
member and call loadMovie to load a movie from
disk. Call play to start playing the movie. In
testApp::update you need to update the playhead for
the video player as well by calling update on your
ofVideoPlayer object.
testApp.h testApp.cpp
class testApp : public ofBaseApp{ void testApp::setup(){
public:
my_video.loadMovie("fingers.mov");
ofVideoPlayer my_video;
my_video.play();
} }
void testApp::draw(){
my_video.draw(10,10);
}
5. Scrubbing through video
Using nextFrame and previousFrame you can scrub
through a paused movie.
testApp.h testApp.cpp
void testApp::setup(){
ofBackground(33);
class testApp : public ofBaseApp{
my_video.loadMovie("fingers.mov");
public:
my_video.play();
ofVideoPlayer my_video;
}
}
void testApp::update(){
my_video.update();
}
void testApp::draw(){
my_video.draw(0,0);
}
void testApp::keyPressed(int key){
if(key == 'p') {
my_video.setPaused(true);
}
else if(key == 'a') {
my_video.nextFrame();
}
else if(key == 'b') {
my_video.previousFrame();
}
}
6. Getting pixels
Check if the player has a new frame using
isFrameNew, then use getPixels to retrieve the new
pixels.
testApp.h testApp.cpp
void testApp::setup(){
class testApp : public ofBaseApp{
ofBackground(33);
public:
my_video.loadMovie("fingers.mov");
ofVideoPlayer my_video;
my_video.play();
ofImage my_video_frame;
}
// tell our image how many/what pixels it will store.
my_video_frame.allocate(
my_video.getWidth()
,my_video.getHeight()
,OF_IMAGE_COLOR
);
}
void testApp::update(){
my_video.update();
if(my_video.isFrameNew()) {
// copy the pixels from the new video frame.
my_video_frame.setFromPixels(
my_video.getPixels()
,my_video.getWidth()
,my_video.getHeight()
,OF_IMAGE_COLOR
);
}
}
7. Controlling playback
You can change the playhead position using
setPosition(float). A value of 0.0 means go to the
start, 1.0 go to the end. Change the speed using
setSpeed(float). A speed of 0.0 is the same as
pausing the video, values above 1.0 are faster then
the default playback speed. Of course you can use
firstFrame() to jump to the first frame. With
setPaused(bool) you can pause or continue playback.
When you want to jump to a specific frame use
setFrame(int). Check the total number of frames with
getTotalNumFrames()
11. ofVideoGrabber
The ofVideoGrabber grabs video from a webcam.
The class is similar to ofVideoPlayer. Main
difference is that you need to tell what webcam
(device) you want to use as source.
12. ofVideoGrabber
Get a list of available devices. Make sure you first
initialize the grabber using initGrabber.
testApp.h
class testApp : public ofBaseApp{
public:
ofVideoGrabber my_grabber;
}
testApp.cpp
void testApp::setup(){
my_grabber.initGrabber(320,240);
my_grabber.listDevices();
}
13. ofVideoGrabber
Draw video to screen!
testApp.cpp
void testApp::setup(){
my_grabber.initGrabber(320,240);
my_grabber.setDeviceID(1);
}
void testApp::update(){
my_grabber.update();
}
void testApp::draw(){
my_grabber.draw(0,0);
}
14. ofVideoGrabber
When you want to grab pixels from your webcam
you can use the function getPixels. Though you
need to check if we got a new frame using
isFrameNew because you don’t want to get the
same pixels we already have.
void testApp::update(){
my_grabber.update();
if(my_grabber.isFrameNew()) {
unsigned char* pix = my_grabber.getPixels();
}
}