Instantly share code, notes, and snippets.

Embed
What would you like to do?
Video Scrubbing With Cinder (Mac OSX / Xcode)
// Video Scrubbing With Cinder (Mac OSX)
// -------------------------------------
/*
Create a Cinder project in Xcode (adding the QuickTime Cinder Block).
Then use this code in your main App cpp.
When the app running, you should be able to scrub the video by dragging the mouse
horizontally across the app window.
*/
#include "cinder/app/AppNative.h"
#include "cinder/qtime/QuickTime.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class VideoScrubbingApp : public AppNative {
public:
void setup();
void mouseDown( MouseEvent event );
void mouseUp( MouseEvent event );
void mouseMove( MouseEvent event );
void calculateMousePosition();
void seek();
void update();
void draw();
qtime::MovieGl myVideo;
Vec2i mousePos;
int numFrames;
bool canScrubVideo;
double seekPos;
int seekFrame;
};
void VideoScrubbingApp::setup()
{
canScrubVideo = false;
//TinderBox will create an 'assets' folder in the root of your project, put 'myVideo.mov' there.
myVideo = qtime::MovieGl( loadAsset("myVideo.mov") );
//Execution halts until the 'myVideo.mov' is loaded...
numFrames = myVideo.getNumFrames();
console() << "VideoScrubbingApp::setup() numFrames: " << numFrames << endl;
myVideo.seekToStart();
}
void VideoScrubbingApp::mouseDown( MouseEvent event ){ canScrubVideo = true; }
void VideoScrubbingApp::mouseUp( MouseEvent event ){ canScrubVideo = false; }
void VideoScrubbingApp::mouseMove( MouseEvent event ){}
void VideoScrubbingApp::calculateMousePosition()
{
if(isFullScreen()){ mousePos = getMousePos(); }
mousePos = getMousePos() - getWindowPos();
if(mousePos.x < 0){ mousePos.x = 0; }
else if( mousePos.x > getWindowWidth() ){ mousePos.x = getWindowWidth(); }
if(mousePos.y < 0){ mousePos.y = 0; }
else if( mousePos.y > getWindowHeight() ){ mousePos.y = getWindowHeight(); }
}
void VideoScrubbingApp::seek()
{
seekPos = (double)mousePos.x / getWindowWidth();
seekFrame = (int)std::round(numFrames * seekPos);
if(myVideo != NULL)
{
myVideo.seekToFrame(seekFrame);
}
}
void VideoScrubbingApp::update()
{
if(!canScrubVideo) { return; }
calculateMousePosition();
//console() << "VideoScrubbingApp::mouseMove() mousePos.x: " << mousePos.x << ", mousePos.y: " << mousePos.y << endl;
seek();
}
void VideoScrubbingApp::draw()
{
gl::disableAlphaBlending();
// clear out the window with black
gl::clear( Color( 0, 0, 0 ) );
gl::Texture videoTexture = myVideo.getTexture();
if(videoTexture)
{
gl::draw( videoTexture);
videoTexture.disable();
}
gl::enableAlphaBlending();
//Alpha-channeled Overlays / GUI could go here!
}
CINDER_APP_NATIVE( VideoScrubbingApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment