【问题标题】:How to avoid Video lag when writing using OpenCV2?使用 OpenCV2 编写时如何避免视频延迟?
【发布时间】:2020-11-25 13:34:27
【问题描述】:

当我尝试使用 OpenCV2 简单地读写视频时,它会在视频中引入 1.033 倍的延迟 - 例如,3:17 分钟的原始视频在输出视频中变为 3:24 分钟,19:00 分钟变为19:38 分钟。我在这里做错了吗?

FPS (29) 和帧数在输入和输出视频中保持不变。 (我正在尝试做面部识别,但我想先弄清楚滞后)

input_movie = cv2.VideoCapture(video_under_analysis)
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
width, height = (
        int(input_movie.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(input_movie.get(cv2.CAP_PROP_FRAME_HEIGHT))
    )
fps = int(input_movie.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
output_movie = cv2.VideoWriter()
output_file_name = "output.mp4"

# Define the codec and create VideoWriter object
output_movie.open(output_file_name, fourcc, fps, (width, height), True)
frame_number = 0
FRAME_LIMIT = length
while True:
    ret, frame = input_movie.read()
    frame_number += 1
    
    if not ret or frame_number > FRAME_LIMIT:
        break
    
    if frame is not None:
      output_movie.write(frame)

update_progress(1)
output_movie.release()
input_movie.release()

cv2.destroyAllWindows()

【问题讨论】:

  • FRAME_LIMIT 是什么?是length吗?
  • 是的,长度,会更新问题
  • 您如何验证视频长度?
  • 我正在检查帧数 - output_movie='output.mp4'; output_movie = cv2.VideoCapture(output_movie); output_movie.get(cv2.CAP_PROP_FRAME_COUNT) 并得到相同的数字
  • 我的意思是你如何验证视频的持续时间。

标签: opencv image-processing video-processing frame-rate opencv-python


【解决方案1】:

我认为问题可能出在这一行

fps = int(input_movie.get(cv2.CAP_PROP_FPS))

您正在将float 值转换为int。您输入视频的 fps 可能是一些 float,例如 29.9,它转换为 29。因此,持续滞后。

【讨论】:

  • 巨大的忽视!我几乎要跳帧以防止音频滞后。非常感谢您指出这一点!
猜你喜欢
  • 2021-06-07
  • 2012-02-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
相关资源
最近更新 更多