python opencv打开视频,Python OpenCV视频格式在浏览器中播放

I'm trying to create a video from a sequence of images and display it in a browser but from some weird reason no matter what codec or file format I use I get the following error:

No video with supported format and mime type found

Here is my code:

ready_images = []

import cv2

for img in videos['Images']:

image = cv2.imread(img.fileName)

ready_images.append(image)

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

video_name = videos['Images'][0].gifLocationPath + "//" + videos['Name']

frame = cv2.imread(videos['Images'][0].fileName)

height, width, layers = frame.shape

video_name = video_name[:-4]+".mp4"

video = cv2.VideoWriter(video_name, fourcc, 20.0, (width, height))

for image in ready_images:

video.write(image)

cv2.destroyAllWindows()

video.release()

The funny thing is that in Firefox or Chrome the videos are not working but in Edge... they actually work.

I don't want to use FFMPEG and would prefer to make it work with OpenCV.

If any of you guys know what format of video (I know the web formats are webm, ogg, mp4) or codec I should use for this, please, just let me know.

Thanks.

解决方案

MP4V or MPEG-4 part 2 is not supported by most browsers, you may want to try H.264 (MPEG-4 part 10) instead.

To do that, change:

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

to

fourcc = cv2.VideoWriter_fourcc(*'H264')

If you are using Python 3, use the following hexadecimal code instead (there seems to be a bug when using the four bytes notation):

fourcc = 0x00000021

Run the script and you will likely get the following error message:

Failed to load OpenH264 library: openh264-1.6.0-win32msvc.dll

Please check environment and/or download library: https://github.com/cisco/openh264/releases

You need to do as the message says and download the required library from github and place it somewhere accessible by your PATH.

Using H.264 compression you will also get a smaller file which is better for Web.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值