Opencv: VideoWriter's frame rate rounding error

Created on 28 Jun 2017  路  3Comments  路  Source: opencv/opencv

System information (version)
  • OpenCV => 3.2.0
  • Operating System / Platform => Ubuntu
  • Compiler => GCC 5.4.0 20160609
Detailed description

For the frame rate of the videoWriter, it's supposed to accept a double/float, but it only works when the digit right after the decimal point is larger or equal than 5, otherwise it will be rounded to an integer(For example it works for 25.5 but it will round to 25 when you pass in 25.4).

Here is two examples which I basically just record a 50 frames videos from the camera and output the frame rate of the recorded videos to demonstrate the issue:

import cv2


fourcc = cv2.VideoWriter_fourcc(*'H264')


vw = cv2.VideoWriter()
vw.open('test2.avi', fourcc, 25.6, (640, 480))

camera = cv2.VideoCapture(0)

for f in range(50):
    _, frame = camera.read()
    vw.write(frame)

vw.release()
c = cv2.VideoCapture('test2.avi')
print(c.get(cv2.CAP_PROP_FPS))

Output: 25.6

import cv2


fourcc = cv2.VideoWriter_fourcc(*'H264')


vw = cv2.VideoWriter()
vw.open('test2.avi', fourcc, 25.4, (640, 480))

camera = cv2.VideoCapture(0)

for f in range(50):
    _, frame = camera.read()
    vw.write(frame)

vw.release()
c = cv2.VideoCapture('test2.avi')
print(c.get(cv2.CAP_PROP_FPS))

Output: 25.0

bug videoio

Most helpful comment

Hello everyone,

I think that I've found the issue around this. On the ffmpeg wrapper there is a small typo in the frame rate rounding.

You can find two instances of this in: https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_ffmpeg_impl.hpp#L1544 and https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_ffmpeg_impl.hpp#L2377

Before

while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){

After

while (fabs(((double)frame_rate/frame_rate_base) - fps) > 0.001){

Using fps=59.15, before the change you obtain frame_rate=59 and frame_rate_base=1, after you can get frame_rate=5915 and frame_rate_base=100.

I have trouble testing this on my computer (windows based) because the wrapper is pre-built as a 3rd party component and the build instructions are not that clear on that part. That's why I'm not creating a PR already. Tell me if I can help.

All 3 comments

Hello everyone,

I think that I've found the issue around this. On the ffmpeg wrapper there is a small typo in the frame rate rounding.

You can find two instances of this in: https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_ffmpeg_impl.hpp#L1544 and https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_ffmpeg_impl.hpp#L2377

Before

while (fabs((double)frame_rate/frame_rate_base) - fps > 0.001){

After

while (fabs(((double)frame_rate/frame_rate_base) - fps) > 0.001){

Using fps=59.15, before the change you obtain frame_rate=59 and frame_rate_base=1, after you can get frame_rate=5915 and frame_rate_base=100.

I have trouble testing this on my computer (windows based) because the wrapper is pre-built as a 3rd party component and the build instructions are not that clear on that part. That's why I'm not creating a PR already. Tell me if I can help.

@WydD Nice catch! Feel free to open PR onto 3.4 branch. It will be tested on OpenCV CI.

@alalek Thanks for the quick reply. The PR has been created.

Was this page helpful?
0 / 5 - 0 ratings