ofxHapPlayer
ofxHapPlayer copied to clipboard
It's possible to extract an image frame "offline" to generate thumbnails?
Hello, I would like to generate 10 thumbnails from the loaded video, but without having to play the video (with a semi manual workflow...)
I tried to set the player position into a for loop but it seems, that video position is not updated if not "really playing". All the output thumbs are the same image frame. (Then current being drawn frame)
That's my approach:
void doGenerateThumbnails()
{
ofImageType type = OF_IMAGE_COLOR;
int n = 10;//thumbnails amount
for (int i = 0; i < n; i++)
{
float p = i * (1.0f / n);
player.stop();//trying to force refresh
player.setPosition(p);
//trying tricks
//ofGetAppPtr()->update();
//ofGetAppPtr()->draw();
player.play();//trying to force refresh
player.update();//trying to force refresh
ofTexture* t = player.getTexture();
int w = t->getWidth();
int h = t->getHeight();
ofFbo fbo;
fbo.allocate(w, h);
fbo.begin();
ofClear(0);
player.draw(0, 0);
fbo.end();
ofPixels pix;
pix.allocate(w, h, type);
fbo.readToPixels(pix);
ofImage img;
img.allocate(w, h, type);
img.setFromPixels(pix);
img.update();
img.saveImage(ofToString(i) + ".jpg");
}
}
Any idea or workaround?
I found a workaround using an FFMPEG script: https://ottverse.com/thumbnails-screenshots-using-ffmpeg/
then we can call a system command doing something like that:
string s = "ffmpeg -i myHapVideo.mov -vf \"select='not(mod(n,300))',setpts='N/(30*TB)\'" -f image2 thumb%02d.jpg"
cout << ofSystem(s) << endl;
take care with the use of slashing by \"
instead of "
:
..
s += "))',setpts='N/(30*TB)'\" ";
..