gpt4 book ai didi

Python OpenCV 将记录分割成多个文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:00 25 4
gpt4 key购买 nike

我想使用 OpenCV 和网络摄像头从网络摄像头连续录制视频 15 分钟,然后再次开始这个过程,这样我就有了 15 分钟的视频 block 。我已经写了一个脚本,但遇到了意外行为。录制一段时间后工作正常,然后程序只会创建大小为 5kb 的无法播放的文件。

有人知道为什么会这样吗?

这是代码:

import numpy as np
import cv2
import time


cap = cv2.VideoCapture(0)

#Record the current time
current_time = time.time()

#Specify the path and name of the video file as well as the encoding, fps and resolution
out = cv2.VideoWriter('/mnt/NAS326/cctv/' + str(time.strftime('%d %m %Y - %H %M %S' )) + '.avi', cv2.cv.CV_FOURCC('X','V','I','D'), 15, (640,480))




while(True):



# Capture frame-by-frame
ret, frame = cap.read()
out.write(frame)

#If the current time is greater than 'current_time' + seconds specified then release the video, record the time again and start a new recording
if time.time() >= current_time + 900:
out.release()
current_time = time.time()
out = cv2.VideoWriter('/mnt/NAS326/cctv/' + str(time.strftime('%d %m %Y - %H %M %S' )) + '.avi', cv2.cv.CV_FOURCC('X','V','I','D'), 15, (640,480))



out.release()

cap.release()




cv2.destroyAllWindows()

最佳答案

如前所述,您应该测试 cap.read() 是否成功,并且只有在有效时才写入框架。这可能导致输出文件出现问题。最好只在需要时提前 next_time 以避免轻微的时间延误。

import numpy as np
import cv2
import time


def get_output(out=None):
#Specify the path and name of the video file as well as the encoding, fps and resolution
if out:
out.release()
return cv2.VideoWriter('/mnt/NAS326/cctv/' + str(time.strftime('%d %m %Y - %H %M %S' )) + '.avi', cv2.cv.CV_FOURCC('X','V','I','D'), 15, (640,480))

cap = cv2.VideoCapture(0)
next_time = time.time() + 900
out = get_output()

while True:
if time.time() > next_time:
next_time += 900
out = get_output(out)

# Capture frame-by-frame
ret, frame = cap.read()

if ret:
out.write(frame)

cap.release()
cv2.destroyAllWindows()

关于Python OpenCV 将记录分割成多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45267727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com