javacv:grabber.getFrameRate()返回0

时间:2012-10-13 13:23:07

标签: javacv

我使用javacv来处理avi。 视频显示比平常更快,我想获得fps来设置视频的速度。但是grabber.getFrameRate()返回0,与grabber.getLengthInFrames()和grabber.getSampleRate()一样,任何人都可以告诉我为什么? 下面的代码段:

FrameGrabber grabber = new OpenCVFrameGrabber("sample.avi");
double fps=grabber.getFrameRate();
System.out.println(fps);
//int n=grabber.getLengthInFrames();
//int f=grabber.getSampleRate();
CvMemStorage storage = CvMemStorage.create();
grabber.start();
grabbedImage = grabber.grab();
while (frame.isVisible() && (grabbedImage = grabber.grab()) != null)
{
        BufferedImage bfimg = grabbedImage.getBufferedImage();        
        frame.showImage(bfimg);
        frame.waitKey((int)(1000/fps));            
        cvClearMemStorage(storage);
}
grabber.stop();

3 个答案:

答案 0 :(得分:0)

你必须在start();

之后调用

也许你可以

FrameGrabber grabber = new OpenCVFrameGrabber("sample.avi");
grabber.start();
double fps=grabber.getFrameRate();

答案 1 :(得分:0)

我使用此代码录制mp4视频:

public static void main(String[] args) {
    IplImage image;
    CanvasFrame canvas = new CanvasFrame("Web Cam");
    try {
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();
        IplImage grabbedImage = grabber.grab();

        canvas.setCanvasSize(grabbedImage.width(), grabbedImage.height());

        System.out.println("framerate = " + grabber.getFrameRate());
        grabber.setFrameRate(grabber.getFrameRate());
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("c:/demo.mp4", 320, 240);

        recorder.setVideoCodec(13);
        recorder.setFormat("mp4");
        recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
        recorder.setFrameRate(10);
        recorder.setVideoBitrate(5*1024);  


        recorder.start();
        System.out.println("framerate = " + grabber.getFrameRate());

        while (canvas.isVisible() && (grabbedImage = grabber.grab()) != null) {
            canvas.showImage(grabbedImage);
            recorder.record(grabbedImage);
        }


        recorder.stop();
        grabber.stop();
        canvas.dispose();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

我在网络摄像头示例中试过这个并且它有效:

double time1,time2;

// your  loop

 while(....){

     time1=System.currentTimeMillis();   // add this first in the loop 

         //code here
          .
          .
          .
          .

     time2=System.currentTimeMillis();   //   add this at the end of the loop

System.out.println("framerate = " + 1/(((time2-time)/1000)%60))

} //end of loop

如果网络摄像头示例中包含网络摄像头代码: http://opencvlover.blogspot.com/2012/05/accessing-webcam-using-javacv.html

它看起来像这样:

IplImage img
while (...) {

     time1=System.currentTimeMillis();   // add this first in the loop 

   //inser grabed video fram to IplImage img
   img = grabber.grab();

          .
          .
          .
          .
     //Show video frame in canvas
     canvas.showImage(img);

 time2=System.currentTimeMillis();   //   add this at the end of the loop

System.out.println("framerate = " + 1/(((time2-time)/1000)%60))
} //end of loop
          .
          .              
          .