0

I want to save a video after converting to gray scale. I don't know where exactly put the line out.write(gray_video). I use jupyter notebook with Python 3, and the Opencv library.

the code is:

import cv2
import numpy as np

video = cv2.VideoCapture("video1.mp4")

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out_gray_scale.mp4', fourcc, 10.0, (640,  480),0)

while (True):
   (ret, frame) = video.read()

   if not ret:
       print("Video Completed")
       break

   # Convert the frames into Grayscaleo
   gray_video = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

   #writing
   gray_frame = cv2.flip(gray_video, 0)
   out.write(gray_video)  #it suppose to save the gray video

   #Show the binary frames
   if ret == True:
      cv2.imshow("video grayscale",gray_video)
    
      #out.write(gray_video)
      # Press q to exit the video  
      if cv2.waitKey(25) & 0xFF == ord('q'):
           break
   else:
       break
video.release()
cv2.destroyAllWindows()
5
  • 1
    What happened when you tried different places in the code to put the line? What's wrong with where you have put it now? Commented Jun 30, 2022 at 21:40
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Jun 30, 2022 at 23:23
  • the code don't save the video after convert it to gray scale, it only shows the video converted. I need to save the video after convert to public Commented Jul 1, 2022 at 3:24
  • is there a video file? how large is it (kilobytes, megabytes)? Commented Jul 1, 2022 at 12:26
  • the video has 15 megabites, it is a mp4 file. right now the code only saves a unique frame, not the total video Commented Jul 2, 2022 at 18:30

1 Answer 1

1

The working video writer module of OpenCV depends on three main things:

  • the available/supported/installed codecs on the OS
  • getting the right codec and file extension combinations
  • the input video resolution should be same as output video resolution otherwise resize the frames before writing

If any of this is wrong openCV will probably write a very small video file which will not open in video player. The following code should work fine:

import cv2
import numpy as np

video = cv2.VideoCapture("inp.mp4")
video_width  = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))   # float `width`
video_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))  # float `height`
video_fps = int(video.get(cv2.CAP_PROP_FPS))

print(video_width, video_height, video_fps)
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
out = cv2.VideoWriter('out_gray_scale1.avi', fourcc, video_fps, (video_width,  video_height),0)


while (True):
   ret, frame = video.read()

   if not ret:
       print("Video Completed")
       break

   # Convert the frames into Grayscale
   gray_video = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

   #Show the binary frames
   if ret == True:
      cv2.imshow("video grayscale",gray_video)

      #Writing video
      out.write(gray_video)

      # Press q to exit the video  
      if cv2.waitKey(25) & 0xFF == ord('q'):
           break
   else:
       break
video.release()
out.release()
cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.