Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoWriter's frame rate rounding error #9023

Closed
orzzzl opened this issue Jun 28, 2017 · 3 comments
Closed

VideoWriter's frame rate rounding error #9023

orzzzl opened this issue Jun 28, 2017 · 3 comments

Comments

@orzzzl
Copy link

orzzzl commented Jun 28, 2017

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

@mshabunin mshabunin added the bug label Jul 24, 2017
@WydD
Copy link
Contributor

WydD commented Sep 30, 2018

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.

@alalek
Copy link
Member

alalek commented Sep 30, 2018

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

@WydD
Copy link
Contributor

WydD commented Sep 30, 2018

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

AhiyaHiya added a commit to AhiyaHiya/opencv that referenced this issue Oct 4, 2018
* master: (48 commits)
  Merge pull request opencv#12649 from sturkmen72:patch-9
  ocl: OPENCV_OPENCL_BUILD_EXTRA_OPTIONS parameter
  Added support for multi-path configuration parameter (env)
  Fix documentation of cv::cuda::compare
  dnn(ocl4dnn): drop weights_buf
  Fix frame rate rounding in ffmpeg wrapper (opencv#9023)
  3rdparty/protobuf: fix compilation issue on s390
  document imread grayscale behaviour
  Merge pull request opencv#12667 from cv3d:fix/ts_report
  Utilize the currently running Python executable
  fix test failure of cudev   * follow the implementation of Luv2RGBfloat in imgproc/src/color_lab.cpp   * loosen threshold in cudaimgproc
  fix typo in FpsMeter.java
  Fix Python wrapper for GpuMat
  CUDA: drop OPENCV_TRAITS_ENABLE_DEPRECATED requirement
  Merge pull request opencv#12658 from chacha21:clr-mutex
  Update findContours parameter type
  Merge pull request opencv#12674 from dmatveev:gapi_upd270918
  Fix Xcode version parsing error (affects bitcode generation)
  Merge pull request opencv#12673 from alalek:fix_build_warnings
  JavaScript bindings for features2d module
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants