3

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.

Georgi Valchev
  • 103
  • 2
  • 11
  • 1
    Could you write the frames as uncompressed images (like a tiff) then use ffmpeg to make a raw video file from those frames? eg is writing the video with opencv a requirement? – NateTheGrate May 10 '19 at 14:36
  • That's probably a good idea. If i save the frame as .bmp picture the data will be kept, right ? Using opencv it's not a requirement, I just haven't worked with FFMPEG. I am still new to Python and I am currently working on a video-proccessing project. I just found a lot of information about opencv and spent a lot of time searching for this library and when I did almost everything that I want, I found this problem with saving the video. If you have any other alternatives for the code or something that can help me, I will be thankful ! – Georgi Valchev May 11 '19 at 14:03
  • 1
    How did you install Python OpenCV module? It's probably compiled without LAGS support. – ivan_pozdeev May 14 '19 at 07:56
  • E.g. check out `print cv2.getBuildInformation()` output. – ivan_pozdeev May 14 '19 at 07:58
  • I installed OpenCV 4.0.0 via pip "pip install opencv-python". – Georgi Valchev May 14 '19 at 14:53
  • Actually, NateTheGrate helped me a lot to solve my problem! I saved the frames as .bmp images and then i used FFMPEG to make a raw video ! From the changed frames i made a second video and the size is exactly the same as the first video. NateTheGrate, please write it like an answer so i can rate it ! And one last question: why does VLC player produce black screen from these videos but they work on Winamp ? – Georgi Valchev May 14 '19 at 15:03

0 Answers0