打开使用 opencv-python 保存的文件时出现问题

Problem in opening a file which was saved using opencv-python

我正在使用 OpenCV-python 使用图像创建视频文件。

当我 运行 代码时,没有显示任何错误,代码在 11 或 14 秒后执行。

但是当我尝试打开代码保存的视频文件时。打开文件时出错。

在 VLC 中它只显示一个空白屏幕,而在 windows 媒体播放器中它显示这个:-

并且视频的大小始终为 8.00 Kb。 这是代码:-

import cv2
import os
from os.path import isfile, join

def imgToVid(imgPath, vidSavePath, fps):
    '''This function will convert images to video'''
    frames = []
    files = [f for f in os.listdir(imgPath) if isfile(join(imgPath, f)) ]
    for i in range(len(files)):
        fileName = imgPath + files[i]
        '''reading images'''
        img = cv2.imread(fileName)

        # height, width, layers = img.shape
        # size = (width, height)

    out = cv2.VideoWriter(vidSavePath,  cv2.VideoWriter_fourcc(*'XVID'), fps, (1280, 720))  
    for j in range(len(frames)):
        out.write(frames[j])

    out.release()

imgPath = 'C:/Users/yash/Desktop/videocap/'
vidSavePath = 'C:/Users/yash/Desktop/testvideo.mp4'
fps = 14
imgToVid(imgPath, vidSavePath, fps)

frames

中追加 img
frames = []
files = [f for f in os.listdir(imgPath) if isfile(join(imgPath, f)) ]
for i in range(len(files)):
    fileName = imgPath + files[i]
    '''reading images'''
    img = cv2.imread(fileName)
    frames.append(img)