【问题标题】:python opencv videoWriter fps roundingpython opencv videoWriter fps 四舍五入
【发布时间】:2018-09-14 04:41:08
【问题描述】:

我正在尝试测量输入视频文件中的一些事件:“test.mp4”。这是通过分几个步骤处理视频来完成的,其中每个步骤对视频数据执行一些操作,并将中间结果写入一个新的视频文件。

输入视频的fps为:29.42346629489295 fps

下面我写了一个脚本来测试这个问题。当我使用这个脚本编写一个新文件时,输出文件中的 fps 被四舍五入到 29.0 fps,这就是问题所在。

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()

我尝试硬编码不同的 fps 值。似乎低于 29.5 fps 的所有内容都四舍五入到小数点零 (29 fps),而高于小数点的所有内容都四舍五入到小数点后一位 (29.x fps)

所以我的问题是:

是否有可能获得任何 mp4 格式的 fps?

输出文件中实际使用的是哪个 fps?

如何在输出文件中获得正确的 fps?

其他信息

我尝试了从 28 fps 到 31 fps 的许多不同值,并绘制了实际输出文件帧率与预期值的关系。这显示了某种分形行为,也许这个提示会激发这里的一些数学向导:)

【问题讨论】:

  • 我也看到了这个问题。将尝试更新 OpenCV 版本
  • 更新:没有修复

标签: python mp4 frame-rate cv2


【解决方案1】:

OpenCV 使用一些工具包来编写。在我的例子中,在 iOS 上,OpenCV 使用原生的 AVFoundation 库。似乎 AVFoundation(或 OpenCV api)无法很好地处理具有许多有效数字的 fps 值,例如 29.7787878779,并且在 OpenCV 的 api 或 AVFoundation 中某些内容被错误地舍入。

为了解决这个问题,我在调用 VideoWriter::open 之前对一些有效数字进行了四舍五入

normalizedFPS = round(1000.0 * normalizedFPS) / 1000.0;

希望它也对你有用!

我已经看到 30,000 被用作时间尺度建议,所以也许可以测试一下 1000.0 与 30,000.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2012-08-04
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多