I am writing a Python script that records video from a usb webcam and stores the resulting file locally on a hard drive. Currently a 60 second file is 27mb. Format is motion jpeg (MJPG) because the target being observed is in moving water. Moving water confuses video compression algorithms. I'd like to reduce the size of the overall file by lowering the quality of all of the individual JPEG images in the file. Can this be done in OpenCV? Below is what I have so far. Thanks in advance.
#!/usr/bin/env python3
import cv2
import numpy as np
import time
import imutils
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print("Unable to read camera feed")
capture_duration = 60
sleep_duration = 0
frame_width = int('320')
frame_height = int('240')
frame_per_sec = int('1')
out = cv2.VideoWriter('C:\\videoPy\\LZ\\outputvideo.avi',cv2.VideoWriter_fourcc('m','j','p','g'),frame_per_sec, (frame_width,frame_height))
start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
ret, frame = cap.read()
if ret==True:
frame = imutils.resize(frame, width=320)
out.write(frame)
time.sleep(sleep_duration)
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()