rxmnnxfpvg
rxmnnxfpvg

Reputation: 30983

video recording logic error - fps

I created a camera/videowriter with open CV, and everything records fine. The problem is when I play back the video, I'm getting a super-speed playback even though the video file only plays 30.0 fps. Can someone help me spot the logic error where I am not recording enough frames? I do delay by some milliseconds based on the fps.

I know I am being stupid with math, just can't find it.

    self.FPS = 30.0
    self.VIDEO_FILENAME = 'test.mp4'
    self.CODEC = highgui.CV_FOURCC('D','I','V','X')  #mpeg-4 codec                                                                
    self.VIDEO_RESOLUTION = (640,480)
    self.camera = highgui.cvCreateCameraCapture(0)
    self.writer = highgui.cvCreateVideoWriter(self.VIDEO_FILENAME, self.CODEC,
                                              self.FPS, self.VIDEO_RESOLUTION, 1)        

    while True:
        im = highgui.cvQueryFrame(self.camera)
        im = opencv.cvGetMat(im)
        highgui.cvWriteFrame(self.writer, im)
        pygame.time.delay(int(1000 * 1.0/self.FPS)) ## of milliseconds

Upvotes: 0

Views: 236

Answers (1)

Lennart Regebro
Lennart Regebro

Reputation: 172179

Nope, no clue, sounds like the delay function isn't delaying, strangeley enough. But, here are some additional comments:

  1. Don't use floats just to get division working properly. Use the future import instead:

    >>> from __future__ import division
    >>> 1000/30
    33.3333333333336
    

    Now you can do int(1000/30) instead of int(1000*1.0/30). Much better.

  2. Also you are using pygames delay function, which basically delays by chewing up processor time. Bad idea. Use wait() instead.

  3. 1000/30 is 33.33333, while your code will delay with 33, so it will run 1% too fast, unless displaying the image takes exactly 1% of the time. :) If displaying the image takes longer than 0.333333333 milliseconds, you should actually run slow. :-) You should probably instead take a look at the time after displaying the page, and waiting the remaining time to when the next frame should appear. pygame.time.Clock might help in this.

Upvotes: 1

Related Questions