Unable to open or export written video

asked 2020-11-24 10:44:25 -0600

System information OpenCV => 4.4.0.46 Operating System / Platform => MacOS Catalina 10.15.4 Compiler => Jupyter Notebook via Anaconda 4.8.3 I'm doing a computer vision task, in which my current step is to create a video based on previously created images. To do so, I'm using OpenCV like this:

fps = 25
files = natsorted([f for f in glob.glob(os.getcwd()+'/tf_files/test/*.jpg')])[:125]
size = (1920,1080)

fourcc = int(cv2.VideoWriter_fourcc(*'mp4v'))
out = cv2.VideoWriter('test.mp4',fourcc,fps=fps,frameSize=size)

for file in files:
    img = cv2.imread(file)
    out.write(img)

out.release()
cv2.destroyAllWindows()

The files list returns the paths of the images:

files[0]
'/Users/XXX/XXX/projects/phase_seg/tensorflow-for-poets-2/tf_files/test/0.jpg'

with the size of each file being:

cv2.imread(files[0]).shape
>>> (1080, 1920, 3)

The code does not give any errors and does actually create a video called 'test.mp4'. Moreover, if the amount of images is changed, the video does change in size as well. I can also open the images with opencv without any problem. However, the video cannot be played, and when exported to another program, it is of length 0. I've tried many different variations of fourcc, but *'mp4' is the only one that actually outputs a .mp4 video. What can be a possible explanation for this?

edit retag flag offensive close merge delete