import numpy as np import cv2 as cv cap = cv.VideoCapture(0) Define the codec and create…
- User
import numpy as np import cv2 as cv
cap = cv.VideoCapture(0)
Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID') out = cv.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened(): ret, frame = cap.read() if not ret: print("Can't receive frame (stream end?). Exiting ...") break frame = cv.flip(frame, 0)
write the flipped frame
out.write(frame)
cv.imshow('frame', frame) if cv.waitKey(1) == ord('q'): break
Release everything if job is finished
cap.release() out.release() cv.destroyAllWindows()
Дополни код для записи видео в файл каждую минуту
- Assistant
To modify the code so that it records a new video file every minute, you can incorporate a timestamp or a counter to manage the video files. Here's a revised version of your code that performs this task:
import numpy as np import cv2 as cv import time cap = cv.VideoCapture(0) # Function to create a new video writer object def create_video_writer(filename): fourcc = cv.VideoWriter_fourcc(*'XVID') return cv.VideoWriter(filename, fourcc, 20.0, (640, 480)) # Initialize the first video writer current_time = time.time() filename = time.strftime("output_%Y%m%d_%H%M%S.avi", time.localtime(current_time)) out = create_video_writer(filename) start_time = current_time while cap.isOpened(): ret, frame = cap.read() if not ret: print("Can't receive frame (stream end?). Exiting ...") break frame = cv.flip(frame, 0) # Check if a minute has passed current_time = time.time() if current_time - start_time >= 60: out.release() # Release the current video writer # Create a new filename and video writer for the next file filename = time.strftime("output_%Y%m%d_%H%M%S.avi", time.localtime(current_time)) out = create_video_writer(filename) start_time = current_time # Write the flipped frame out.write(frame) cv.imshow('frame', frame) if cv.waitKey(1) == ord('q'): break # Release everything if the job is finished cap.release() out.release() cv.destroyAllWindows() - User
then add a func to convert avi videot to mp4
- Assistant
- User
FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'
- Assistant
The error message you are seeing indicates that the
ffmpegexecutable is not available on your system's PATH. Theffmpeg-pythonlibrary is a Python wrapper around theffmpegcommand-line tool, so it needs access to theffmpegexecutable to function properly.Here’s how you can resolve this issue by installing on your system: