如何在第一个视频文件正确保存后获取视频文件
How to get the video files after the first video file to save properly
我正在尝试使用 openCV 从我的 IP 摄像机录制视频以供安全使用。我想将视频设置为在给定的时间跨度内录制(例如,从上午 10 点到晚上 10 点),然后在下一个时间睡到第二天。下面的代码尝试在较短的时间内模拟此过程,以便于测试。
import cv2
# import numpy as np
from datetime import datetime, timedelta
import time
# capture IP camera's live feed
cap = cv2.VideoCapture('rtsp://admin:@10.187.1.146:554/user=admin_password=tlJwpbo6_channel=1_stream=0')
ret, initialFrame = cap.read()
# Settings for the video recording.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 22
# Get the frame's size
fshape = initialFrame.shape
fheight = fshape[0] # int(fshape[0] * (75 / 100))
fwidth = fshape[1] # int(fshape[1] * (75 / 100))
frameSize = (fwidth,fheight)
if cap.isOpened:
print ("The IP Camera's feed is now streaming\n")
today = datetime.today().strftime('%Y-%b-%d')
try:
#Loop to view the camera's live feed
while datetime.today().strftime('%Y-%b-%d') == today:
vidOut = cv2.VideoWriter('Cam01_'+str(today)+ " " + str(datetime.today().strftime('%H:%M')) +'.avi',fourcc,fps,frameSize)
print ("Recording for " + today)
recStart = datetime.now()
recStop = recStart + timedelta(seconds= 60*3)
print ("Recording for duration of 10 mins \n\n Press Ctrl+C on the keyboard to quit \n\n")
while recStart <= datetime.now() and datetime.now() <= recStop:
# read frame
ret, frame = cap.read()
# Write frame to video file
vidOut.write(frame)
print ("Recording ended at " + datetime.now().strftime("%Y-%B-%d, %A %H:%M"))
print ("Next Recording at " + (datetime.now() + timedelta(seconds= 60*3)).strftime("%Y-%B-%d, %A %H:%M"))
vidOut.release() # End video write
cap.release() # Release IP Camera
# cv2.destroyAllWindows() # Close all Windows that were launched by cv2.
print ('Waiting 3 mins before next recording')
time.sleep(60*3)
continue
except KeyboardInterrupt:
print ("\n Process Interrupted by User \n")
print ("\n Recording ended at " + datetime.now().strftime("%Y-%B-%d, %A %H:%M"))
print ("Next Recording at " + (recStart + timedelta(seconds= 600)).strftime("%Y-%B-%d, %A %H:%M"))
vidOut.release() # End video write
cap.release() # Release IP Camera
else:
print("The video stream couldn't be reached")
我预计在第一个视频录制完成并发布后,之后的视频都会有内容。但是,只有 运行 代码之后的第一个视频文件中有帧。其余的只是空文件。
在 while 循环中调用 cap.release()
这会破坏对相机的引用,因此没有新的帧可以保存。在 while 循环开始时(重新)打开 videoCapture
,或者不关闭它。检查 cap.read()
是否成功也是一个好习惯。
我正在尝试使用 openCV 从我的 IP 摄像机录制视频以供安全使用。我想将视频设置为在给定的时间跨度内录制(例如,从上午 10 点到晚上 10 点),然后在下一个时间睡到第二天。下面的代码尝试在较短的时间内模拟此过程,以便于测试。
import cv2
# import numpy as np
from datetime import datetime, timedelta
import time
# capture IP camera's live feed
cap = cv2.VideoCapture('rtsp://admin:@10.187.1.146:554/user=admin_password=tlJwpbo6_channel=1_stream=0')
ret, initialFrame = cap.read()
# Settings for the video recording.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 22
# Get the frame's size
fshape = initialFrame.shape
fheight = fshape[0] # int(fshape[0] * (75 / 100))
fwidth = fshape[1] # int(fshape[1] * (75 / 100))
frameSize = (fwidth,fheight)
if cap.isOpened:
print ("The IP Camera's feed is now streaming\n")
today = datetime.today().strftime('%Y-%b-%d')
try:
#Loop to view the camera's live feed
while datetime.today().strftime('%Y-%b-%d') == today:
vidOut = cv2.VideoWriter('Cam01_'+str(today)+ " " + str(datetime.today().strftime('%H:%M')) +'.avi',fourcc,fps,frameSize)
print ("Recording for " + today)
recStart = datetime.now()
recStop = recStart + timedelta(seconds= 60*3)
print ("Recording for duration of 10 mins \n\n Press Ctrl+C on the keyboard to quit \n\n")
while recStart <= datetime.now() and datetime.now() <= recStop:
# read frame
ret, frame = cap.read()
# Write frame to video file
vidOut.write(frame)
print ("Recording ended at " + datetime.now().strftime("%Y-%B-%d, %A %H:%M"))
print ("Next Recording at " + (datetime.now() + timedelta(seconds= 60*3)).strftime("%Y-%B-%d, %A %H:%M"))
vidOut.release() # End video write
cap.release() # Release IP Camera
# cv2.destroyAllWindows() # Close all Windows that were launched by cv2.
print ('Waiting 3 mins before next recording')
time.sleep(60*3)
continue
except KeyboardInterrupt:
print ("\n Process Interrupted by User \n")
print ("\n Recording ended at " + datetime.now().strftime("%Y-%B-%d, %A %H:%M"))
print ("Next Recording at " + (recStart + timedelta(seconds= 600)).strftime("%Y-%B-%d, %A %H:%M"))
vidOut.release() # End video write
cap.release() # Release IP Camera
else:
print("The video stream couldn't be reached")
我预计在第一个视频录制完成并发布后,之后的视频都会有内容。但是,只有 运行 代码之后的第一个视频文件中有帧。其余的只是空文件。
在 while 循环中调用 cap.release()
这会破坏对相机的引用,因此没有新的帧可以保存。在 while 循环开始时(重新)打开 videoCapture
,或者不关闭它。检查 cap.read()
是否成功也是一个好习惯。