0

我在我的项目中使用了 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();
4

3 回答 3

0

您必须在 start() 之后调用;

也许你可以

FrameGrabber grabber = new OpenCVFrameGrabber("sample.avi");
grabber.start();
double fps=grabber.getFrameRate();
于 2013-09-09T07:06:16.293 回答
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();
    }
}
于 2013-09-16T05:11:50.123 回答
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
          .
          .              
          .
于 2014-12-19T07:00:36.933 回答