python3 opencv没有保存可读视频

时间:2018-06-18 04:29:01

标签: javascript django python-3.x opencv html5-video

我只想让视频与网络浏览器兼容。从我获取图像路径的视频对象中,图像是png类型我只想将这些图像转换为视频并在浏览器中显示。

    path = settings.WEBUI_ROOT + '/events/'
    if not os.path.exists(path):
        os.makedirs(path)
    video = Video.objects.get(id=video_id)
    movements = Image.objects.filter(video=video)

    frame = cv2.imread(movements[0].file_path, 1)
    height, width, channel = frame.shape
    video_name = 'eventvideo.mp4'
    output_video = cv2.VideoWriter(path + video_name, cv2.VideoWriter_fourcc(*'MP4V'), 15, (height, width))

    for _, image in enumerate(movements):
        image = Image.objects.get(id=image.id)
        frame = cv2.imread(image.file_path)
        output_video.write(frame)
    output_video.release()

    result = {
        'success': True,
        'path': "events/" + event_video_name
    }
    return JsonResponse(result, safe=False)

使用Django和JS的工具

1 个答案:

答案 0 :(得分:1)

创建VideoWriter时,您应该使用(width, height)元组作为帧大小,而不是(height, width)