Play video with shader effects fps always change

Hi I am trying to play video with shader my problem is that the FPS always change from 46 FPS to 1 FPS

the log of my FPS

FPS: 46
FPS: 8
FPS: 6
FPS: 1
FPS: 4
FPS: 1
FPS: 8
FPS: 2
FPS: 3
FPS: 5
FPS: 1
FPS: 7
FPS: 24
FPS: 23
FPS: 6
FPS: 13
FPS: 4
FPS: 3
FPS: 5

my codes looks like this.

#include “ofApp.h”

ofShader shader; //Shader
ofFbo fbo; //Buffer for intermediate drawing
ofImage image;
ofVideoPlayer video; //Declare video player object

float prevtime = ofGetElapsedTimef();

double clockToMilliseconds(clock_t ticks){
// units/(units/time) => time (seconds) * 1000 = milliseconds
return (ticks/(double)CLOCKS_PER_SEC)*1000.0;
}

clock_t deltaTime = 0;
unsigned int frames_count = 0;
double frameRate = 30;
double averageFrameTimeMilliseconds = 33.333;

//--------------------------------------------------------------
void ofApp::setup() {
shader.load( “shaderVert.c”, “shaderFrag.c” );
fbo.allocate( ofGetWidth(), ofGetHeight() );
image.loadImage( “sunflower.png” );

video.loadMovie("300.mp4");
video.play();

}

//--------------------------------------------------------------
void ofApp::update(){
clock_t beginFrame = clock();

video.update();

 clock_t endFrame = clock();
 deltaTime += endFrame - beginFrame;
frames_count ++;

//if you really want FPS
if( clockToMilliseconds(deltaTime)>1000.0){ //every second
    frameRate = (double)frames_count*0.5 +  frameRate*0.5; //more stable
     std::cout << "FPS: "<<frames_count << std::endl;
    frames_count = 0;
    deltaTime -= CLOCKS_PER_SEC;
    averageFrameTimeMilliseconds  = 1000.0/(frameRate==0?0.001:frameRate);
}

}

//--------------------------------------------------------------
void ofApp::draw(){

//1. Drawing into fbo buffer
fbo.begin(); //Start drawing into buffer

//Draw something here just like it is drawn on the screen
ofBackgroundGradient( ofColor( 255 ), ofColor( 128 ) );
ofSetColor( 255, 255, 255 );
//image.draw( 351, 221 );
video.draw(0,0,1024,768);

fbo.end();			//End drawing into buffer

//2. Drawing to screen through the shader
shader.begin();		//Enable the shader

//Draw fbo image
ofSetColor( 255, 255, 255 );
fbo.draw( 0, 0 );
shader.end();		//Disable the shader

}

How to fixed this? I am expecting steady FPS value.

I’m not a specialist but it seems that you make some measure of cpu cycling (in the update function) and want to evaluate GPU activities. I think that it’s not the same.
What your shader is doing ?

Hi,
It makes no sense to use the fbo in the way you are doing. you could simply put video.draw(0,0) inside the shader’s begin and end and it will work.
If you still want to use the fbo, draw into it only when needed. Check if the video player has a new frame and only then draw into the fbo. This might be causing you this weird readings.

I use shader to add effects to my video.

Ok I change my code like this

void ofApp::draw(){
//1. Drawing into fbo buffer
fbo.begin(); //Start drawing into buffer

//Draw something here just like it is drawn on the screen
ofBackgroundGradient( ofColor( 255 ), ofColor( 128 ) );
ofSetColor( 255, 255, 255 );
//image.draw( 351, 221 );

// video.draw(0,0,1024,768);

fbo.end();//End drawing into buffer

//2. Drawing to screen through the shader
shader.begin();		//Enable the shader
video.draw(0,0,1024,768);
//Draw fbo image
ofSetColor( 255, 255, 255 );
fbo.draw( 0, 0 );
shader.end();		//Disable the shader

}

Unfortunately it doest not display the video…

My main issue here is the FPS always changes… the player doest not play smoothly or continuously. forget about the shader it is not my issue.

HI, sure, that will not show the video as the fbo is being drawn on top of the video.
the following should draw just the video passing through the shader.

void ofApp::draw(){
shader.begin();
video.draw(0,0,1024,768);
shader.end();
}

in update()
use video.isFrameNew() to check for new frames and then update your stuff.

Ok I remove how fbo.draw and it works…

In my update I use this but it play only 1 - 2 frame and it stop.

if(video.isFrameNew()){
video.update();
}

Is this correct?

nope, it is the other way round.

video.update();
if(video.isFrameNew()){
// whatever you want to do when you have a new video frame, like updating the fbo, in case you want to use it.
}

OK what I did is like this.

void ofApp::update(){
video.update();
}

Now I dont need to check the FPS the main issue here is the player doest not play smoothly I mean some times the player play the video smoothly and sometimes it play veryslow the sounds stops and play again. or the video frame display stops for a while and play again… I dont know how to fixed this.

I think it is better create new topic How to play video with steady speed