如何将循环录制的视频保存为不同的文件



我使用opencv和python在循环中录制视频。我已经设法将循环中的所有视频录制并保存到一个输出文件中,但我的目标是将循环中录制的每个视频保存为不同的文件,并将日期和时间作为每个视频的标识符。我不怎么使用python,我相信有一个简单的方法可以做到,但我就是找不到它。任何帮助都非常感谢!

import cv2
import time as t1
import numpy as np
def save_video(outPath,fps,mirror=False):
cap = cv2.VideoCapture(0)
currentFrame = 0
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter(outPath, fourcc, 20.0, (int(width), int(height)))
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
if mirror == True:
frame = cv2.flip(frame, 1)
out.write(frame)
cv2.imshow('frame', frame)
else:
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
currentFrame +=1

cap.release()
out.release()
cv2.destroyAllWindows()
date_string = t1.strftime("%Y-%m-%d-%H:%M")
count=0
while(True):
if count < 6:
print('Recording started: recording video number ' + str(count))
save_video('/Documents/0222_video_recordings/video' + date_string + '.avi', 
30.0,mirror=True)
count += 1
import cv2, numpy as np
import time
from datetime import datetime
i = 1;
win_name = 'Recording'
start_time = time.time()
capture_duration = 1
# Connect camera for capture 
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)              
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) 
# cap.set(cv2.CAP_PROP_FPS,150)
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
frame_size = (frame_width,frame_height)
#output = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc(*'XVID'), 20, (frame_width , frame_height))
outVid = 'output_' + str(i) + '.avi'
output = cv2.VideoWriter( outVid, cv2.VideoWriter_fourcc(*'XVID'), 20, (frame_width , frame_height))
while cap.isOpened():
e1 = cv2.getTickCount()       
ret, frame = cap.read()

duration = time.time() - start_time
minutes = int(duration/60)
hours = int(minutes/60)
seconds = duration%60
seconds = round(seconds, 2)
now = datetime.now()
# write text to the frame
cv2.putText(frame, now.strftime("date : %Y/%m/%d time : %H:%M:%S") + '  '+ 'hours: ' + str(hours) + '  min: ' + str(minutes) + '  sec: ' + str(seconds), (20, frame_height - 20), 0, 1, (255, 255, 255), 1,
cv2.LINE_AA)

cv2.imshow(win_name, frame)
# print(round(seconds % 20))
if round(minutes) < capture_duration:
output.write(frame)
elif round(minutes) == capture_duration:
output.release()
i += 1
capture_duration += 1
outVid = 'output_' + str(i) + '.avi'
output = cv2.VideoWriter( outVid, cv2.VideoWriter_fourcc(*'XVID'), 20, (frame_width , frame_height))
key = cv2.waitKey(1)  & 0xFF
if key == 27:    # Esc, 종료
break          

else:
print("can't open camera.")
cap.release()                          
cv2.destroyAllWindows()
output.release()

在每次迭代中选择不同的文件名。

示例:

save_video('/Documents/0222_video_recordings/video_' + str(count) + '_' + date_string + '.avi', 

我可以帮你,但我的答案没有多大用处。。。。。。。你可以使用这个代码。。。。。。。

import cv2
codec = cv2.VideoWriter_fourcc("*XVID")
# out = cv2.VideoWriter("out.avi", codec, frame speed, frame size)
out = cv2.VideoWriter("out.avi", codec, 10.0, (640,480))
i = 0
cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if ret == True:
cv2.VideoWriter("out{}.avi".format(i),fourcc, 10.0, (640,480)).write(frame)
i = i + 1
cv2.imshow("frame",frame)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()

最新更新