I am trying to measure some event in an input video file: "test.mp4". This is done by processing the video in several steps, where each step performs some operations on the video data and writes the intermediate results to a new video file.
The fps of the input video is: 29.42346629489295 fps
Below I have written a script to test the problem. When I write a new file using this script the fps gets rounded in the outputfile to 29.0 fps, and this is the problem.
import cv2
import sys
inputfilepath = "test.mp4"
outputfilepath = "testFps.mp4"
video = cv2.VideoCapture(inputfilepath)
fps = video.get(cv2.CAP_PROP_FPS)
framecount = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("original fps: ", fps)
# fps = 29.4
print("writing fps: ", fps)
writer = cv2.VideoWriter(outputfilepath, 0x00000020, fps, (width, height))
for i in range(framecount):
success, image = video.read()
if not success:
break
writer.write(image)
writer.release()
video.release()
video = cv2.VideoCapture(outputfilepath)
fps = video.get(cv2.CAP_PROP_FPS)
# the next line will not print the same fps as I passed to cv2.VideoWriter(...) above.
print("output fps: ", fps)
video.release()
I have tried to hardcode different values for fps. It seems like everything below 29.5 fps is rounded to zero decimals (29 fps), and everything above gets round to one decimal (29.x fps)
So my questions are:
Is it possible to get any fps with the mp4 format?
Which fps is actually used in the output file?
How can I get the correct fps in the output file?
Additional info
I tried many different values ranging from 28 fps to 31 fps, and plotted the actual output file framerate vs the expected. This displays some kind of fractral behavior, maybe this hint will inspire some math wizard in here :)
User contributions licensed under CC BY-SA 3.0