一、问题代码

几个月前有段代码能正常执行的,部分代码如下:

def buildVideoByCV():
    videoMake = cv2.VideoWriter()
    fourcc = cv2.VideoWriter_fourcc(*'MP4V') #
    fps = 12
    videoMake.open(r"g:\video\lightShowCV.MP4", fourcc, fps, (800,600))

    for t in range(65*fps):
        img = makeframe(t*1.0/fps)
        videoMake.write(img)
        if t%20==0:print(f'\r视频制作进度:{(t*100.0)/(66*fps):4.2f}%',end='')
    videoMake.release()

二、报错信息

今天执行时报错:

视频制作进度:95.96%OpenCV: FFMPEG: tag 0x5634504d/'MP4V' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'

三、解决办法

fourcc = cv2.VideoWriter_fourcc(*'MP4V')改成:fourcc = cv2.VideoWriter_fourcc(*'mp4v')即可,只需要将编码的大写MP4V改成小写即可。

四、小结

本文介绍了OpenCV VideoWriter报错: FFMPEG: tag ‘MP4V‘ is not supported 错误的解决办法,只需要将编码‘MP4V’换成‘mp4v’即可。