我使用的是ubuntu系统,遇到python opencv生成的视频,无法在浏览器上播放,之前用C++ 写的时候,直接将fourcc设置为h264编码的一种即可(apt 直接装的opencv)

CV_FOURCC('a', 'v', 'c', '1')

以为python opencv也可简单的设置,但是不行,google一下,发现python opencv 通过pip下载的不包含h264编码器,需要从源码编译才行,个人觉得太麻烦,直接舍弃,于是采用ffmepg转码,简单粗暴。

#主要转码代码为
def convertToMP4(inputPath):
    outputPath = inputPath[:-3] + 'mp4'
    cmd = 'ffmpeg -y -i {} -vcodec h264 {}'.format(input_file_path, output_file_path)
    subprocess.call(cmd, shell=True)
    return outputPath 

转换完成的视频,可流畅的在浏览器上播放