Home>

Shoot a video with opencv + web camera and create a 5-minute video file

I have set the shooting time to 5 minutes, but the video file will be about 2 minutes.
I want to set it to create a 5-minute video file after shooting for 5 minutes.

Corresponding source code
import cv2
import time
import datetime
import shutil
import os
import numpy as np
cap = cv2.VideoCapture (0)
cap.set (cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set (cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set (cv2.CAP_PROP_BUFFERSIZE, 3)
cap.set (cv2.CAP_PROP_FPS, 30)
width = int (cap.get (cv2.CAP_PROP_FRAME_WIDTH))
height = int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT))
fps = int (cap.get (cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc ('H', '2', '6', '4')
bfdt = nowdt = datetime.datetime.now ()
filename ='cap' + nowdt.strftime ('% Y% m% d_% H% M') +'.mp4'
out = cv2.VideoWriter (filename, fourcc, fps, (width, height))
path ='/ home/pi /'
while (True):
    ret, frame = cap.read ()
    frame = cv2.flip (frame, 0)
    out.write (frame)
    cv2.imshow ('frame', frame)
    nowdt = datetime.datetime.now ()
    border_time = bfdt + datetime.timedelta (minutes = 5)
    if border_time<= nowdt:
        bfdt = nowdt
        shutil.move (path + filename,'./movie')
        filename ='cap' + nowdt.strftime ('% Y% m% d_% H% M') +'.mp4'
        out = cv2.VideoWriter (filename, fourcc, fps, (width, height))
    if cv2.waitKey (30)&0xFF == ord ('q'):
        break
cap.release ()
cv2.destroyAllWindows ()
Supplementary information (FW/tool version, etc.)

python 3.7.3