问题描述
我正在尝试根据一系列图像创建视频并将其显示在浏览器中,但是出于某种奇怪的原因,无论我使用哪种编解码器或文件格式,都会出现以下错误:
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:
这是我的代码:
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()
有趣的是,在Firefox或Chrome浏览器中,视频无法正常运行,但在Edge中却可以正常运行.
The funny thing is that in Firefox or Chrome the videos are not working but in Edge... they actually work.
我不想使用FFMPEG,而是希望使其与OpenCV一起使用.
I don't want to use FFMPEG and would prefer to make it work with OpenCV.
如果你们中的任何一个知道我应该使用哪种视频格式(我知道Web格式是webm,ogg,mp4)或编解码器,请告诉我.
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.
谢谢.
推荐答案
大多数浏览器不支持MP4V或MPEG-4第2部分,您可能需要尝试使用H.264(MPEG-4第10部分).
MP4V or MPEG-4 part 2 is not supported by most browsers, you may want to try H.264 (MPEG-4 part 10) instead.
为此,请更改:
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
到
fourcc = cv2.VideoWriter_fourcc(*'H264')
如果您使用的是Python 3,请改用以下十六进制代码(使用四字节表示法时似乎有错误):
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:
您需要按照消息中的说明进行操作,并从github下载所需的库并将其放置在您的PATH可以访问的位置.
You need to do as the message says and download the required library from github and place it somewhere accessible by your PATH.
使用H.264压缩,您还将获得一个更小的文件,更适合Web.
Using H.264 compression you will also get a smaller file which is better for Web.
这篇关于Python OpenCV视频格式在浏览器中播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!