I am trying to save 2 videos from my laptop camera with the same size and pixel values of the frames, using OpenCV with Python. I'm changing the RGB values of some pixels in frames in the second video. I want to save the videos without any difference between them, except the pixels that i change, but when i use cv2.VideoWriter there is a fourcc codec that compresses the videos and i am failing my project, because i want to retrieve the changed information in the pixels later. The videos go different size and different pixels values. For example with the following code i recorded a one second video and save it twice (the second video is with changed pixels).
import cv2
from numpy import *
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Get the default resolution.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc(*"XVID"), 25, (frame_width,frame_height))
out2 = cv2.VideoWriter('outpy2.avi',cv2.VideoWriter_fourcc(*"XVID"), 25, (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# changing the BGR values
out.write(frame)
frame[1,1,0] = 255
frame[1,1,1] = 255
frame[1,1,2] = 255
out2.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and video write objects
cap.release()
out.release()
out2.release()
# Closes all the frames
cv2.destroyAllWindows()
The size of the first video is 314 120 bytes and the second one is 314 452 bytes, also the values of the frame pixels in the videos are different.
So i tried to use " Lagarith Lossless Video Codec " to avoid the compression like: out=cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc(*"LAGS"), 25, (frame_width,frame_height))
, but i receive an error: Could not find encoder for codec id 146: Encoder not found.
After this I tried to download the LAGS codec from : https://lags.leetcode.net/codec.html with the first link, i installed the codec and i got the same error. I dont know where the problem is.
If i'm not wrong, my problem is because of the compression with the fourcc codec. Is the LAGS codec a solution and if it's not, please suggest a method to solve it.