Hi,
I’ve an issue with fast playback of video in the files this captures. I’ve tried a number of things and maybe the USB camera is stuck in YUV mode? Maybe the method of writing the file is wrong! Also, the files only play back in VLC… Any ideas? I tried a lot of things…
Thanks in advance!
import _thread as thread
import time
import cv2
import sys
import os
SAVE_FOLDER = '../'
FC = cv2.VideoWriter_fourcc(*'MJPG')
def run_camera(cam_name, cam_index, show_video=False):
while True:
cap = cv2.VideoCapture(0)
codec = 0x47504A4D
# cap.set(cv2.CAP_PROP_FORMAT, 10)
# cap.set(cv2.CAP_PROP_MODE, 1)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
cap.set(cv2.CAP_PROP_FPS, 20)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
w1 = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h1 = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(w1, h1)
f1 = cap.get(cv2.CAP_PROP_FPS)
# f1 = 20
if cap.isOpened():
print(f"{str(datetime.now())} Camera {cam_name} is connected!")
mon = datetime.now().month
day = datetime.now().day
hour = datetime.now().hour
video_write = cv2.VideoWriter(os.path.join(SAVE_FOLDER, f'cam_{cam_index}_{mon}_{day}_{hour}.avi'),
FC, f1, (w1, h1))
while True:
r1, frame1 = cap.read()
if not r1:
break
if hour != datetime.now().hour:
mon = datetime.now().month
day = datetime.now().day
hour = datetime.now().hour
video_write.release()
video_write = cv2.VideoWriter(os.path.join(SAVE_FOLDER, f'cam_{cam_index}_{mon}_{day}_{hour}.avi'),
FC, f1, (w1, h1))
video_write.write(frame1)
if show_video:
cv2.imshow(f'cam {cam_index}', frame1)
if cv2.waitKey(10) == ord('q'):
break
# else:
# time.sleep(0.02)
video_write.release()
else:
print(f"{str(datetime.now())} Camera {cam_name} isn't connected! Please check connection.")
cap.release()
time.sleep(5)
if __name__ == '__main__':
if len(sys.argv) == 2:
cam_num = int(sys.argv[1])
else:
cam_num = 1
# run_camera(cam_name=0, cam_index=1)
thread.start_new_thread(run_camera, (0, 1))
# thread.start_new_thread(run_camera, (2, 2))
while True:
time.sleep(5)