Lunethan
Lunethan

Reputation: 76

OpenCV VideoWriter writes an empty video

import cv2
import numpy as np
import time
import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')


img = np.zeros((640,480))

center_x = 0
center_y = 256

videoFile1 = 'D:/Python/6. Const_Speed/sample1.mp4'

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('SaveVideo2.avi', fourcc, 20.0, (640, 480))

while True:
    img = np.zeros((640, 480))
    img = cv2.circle(img, (center_x, center_y), 20, 120, -1)

    cv2.imshow('img', img)
    out.write(img)

    center_x += 3
    time.sleep(1/30)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

out.release()
cv2.destroyAllWindows()

I would like to record a video of a circle moving constant speed from left side of the screen to the right. The code above runs without an error, but the result video is empty with only black screen.

What I tried

  1. I found that the video doesn't write if the dimension is different. So I matched the dimension to be (640,480)
  2. I tried to change the codec from XVID to different codecs. At last, I found that 'XVID' codec works with .avi file, so I decided to keep it.

Upvotes: 0

Views: 3072

Answers (1)

Ahx
Ahx

Reputation: 7985

There are three-issues with your code.

Issue#1:


  • If you are going to create .avi, I suggest you to use MJPG.

    • fourcc = cv2.VideoWriter_fourcc(*'MJPG')

Issue#2:

  • You need to define VideoWriter class carefully

    • When you are defining size, it should be frame_width and frame_height

    • For instance, if you want to create a video with the size (640, 480), you need to initialize VideoWriter with (480, 640)

    • out = cv2.VideoWriter('SaveVideo2.avi', fourcc, 20.0, (480, 640))
      
    • Also, you are planning to create a gray-scale video, therefore you need to initialize isColor to False

    • out = cv2.VideoWriter('SaveVideo2.avi', fourcc, 20.0, (480, 640), isColor=False)
      

Issue#3:


  • If you are creating a black image, you need to define its type:

    • while True:
          img = np.zeros((640, 480), dtype=np.uint8)
      

If you fix the issues, result will be:

enter image description here

Code:


import cv2
import numpy as np
import time
import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8')


img = np.zeros((640, 480))

center_x = 0
center_y = 256

videoFile1 = 'video.mp4'

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('SaveVideo2.avi', fourcc, 20.0, (480, 640), isColor=False)

while True:
    img = np.zeros((640, 480), dtype=np.uint8)
    img = cv2.circle(img, (center_x, center_y), 20, 120, -1)

    cv2.imshow('img', img)
    out.write(img)

    center_x += 3
    time.sleep(1/30)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

out.release()
cv2.destroyAllWindows()

Upvotes: 4

Related Questions