V
V
Vladosio2021-04-13 19:55:48
Python
Vladosio, 2021-04-13 19:55:48

How to get a new video recording every time and how to save it in the root folder?

Took the code to record from the webcam, etc. with video saved from here - https://opencv-python-tutroals.readthedocs.io/en/l... Modifying it a

bit. And now the questions:

1. How can I get a new video every time so that the previous one is saved, because. now it is overwritten every time in one file? I tried using % i following the example, but it didn't work.
2. How to save the video in the root folder with the .py file (that was run), for example, in the testvideo folder?

I try this option, but the error is that the testvideo folder cannot be found. If you do not use it like this, then the videos are saved in the C:\Users\%User% folder and you can specify the folder where to save it, but I need it in the project folder.

import numpy as np
import cv2
import os
import sys

cap = cv2.VideoCapture(0)

i = 0
fourcc = cv2.VideoWriter_fourcc(*'XVID')
global path
path = 'testvideo'
videoPaths = [os.path.join(path,f) for f in os.listdir(path)]
video = "output%.4d.avi" % i
out = cv2.VideoWriter(video, fourcc, 20.0, (640,480))
for videoPath in videoPaths:
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret==True:
            frame = cv2.flip(frame,1)
    
 
            out.write(frame)
            i += 1 
            cv2.imshow('frame',frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        else:
            break


cap.release()
out.release()
cv2.destroyAllWindows()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexa2007, 2021-04-13
@Vladosio

It is better to tie the name to the time, as in all cameras

import pathlib
folder = pathlib.Path().absolute()
video = fr'{folder}\VIDEO_{time.strftime("%m%d%Y_%H%M%S")}.avi'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question