opencv - 使用 OpenCV2 编写时如何避免视频延迟?
问题描述
当我尝试使用 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()
解决方案
我认为问题可能出在这一行
fps = int(input_movie.get(cv2.CAP_PROP_FPS))
您正在将float
值转换为int
. 您输入视频的 fps 可能float
有点像 29.9 转换为 29。因此,恒定滞后。
推荐阅读
- javascript - moment.localeData().longDateFormat("L") 未根据区域设置返回正确的日期格式
- wordpress - 电影院网站 - Wordpress - 加入 Fandango 放映时间
- javascript - 无法在 JavaScript/Angular 中使用字符串 Polation 解析函数
- android - 当firebase中不存在变量时防止返回null
- c++ - 在 C++ 中,为什么我们必须在复制构造函数中传递用户定义的数据类型作为引用而不是在函数中,反之亦然?
- r - 在 R 中编写一个使用向量运算的函数,以便将其应用于向量
- spring-batch - Spring Batch - 如何将标题信息保存到 FlaFileItemReader 中的单个行对象中
- python - 如何再添加一个频道 ID,以便机器人对更多频道做出反应?
- python - 在 Tkinter 的文件夹中动态初始化 Frames
- java - 从 javafx 中的 Stage 类创建新对象时抛出异常