Python OpenCV 视频格式浏览器播放

标签 python python-2.7 opencv opencv3.0

我正在尝试从一系列图像创建视频并将其显示在浏览器中,但出于某种奇怪的原因,无论我使用哪种编解码器或文件格式,我都会收到以下错误:

No video with supported format and mime type found

这是我的代码:

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 中......它们确实有效。

我不想使用 FFMPEG,而更愿意让它与 OpenCV 一起工作。

如果你们中有人知道我应该使用什么格式的视频(我知道网络格式是 webm、ogg、mp4)或编解码器,请告诉我。

谢谢。

最佳答案

大多数浏览器不支持 MP4V 或 MPEG-4 第 2 部分,您可能想尝试使用 H.264(MPEG-4 第 10 部分)。

为此,更改:

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

fourcc = cv2.VideoWriter_fourcc(*'H264')

如果您使用的是 Python 3,请改用以下十六进制代码(使用四字节表示法时似乎存在错误):

fourcc = 0x00000021

运行脚本,您可能会收到以下错误消息:

Failed to load OpenH264 library: openh264-1.6.0-win32msvc.dll Please check environment and/or download library: https://github.com/cisco/openh264/releases

您需要按照消息中的说明进行操作,从 github 下载所需的库并将其放置在您的 PATH 可访问的位置。

使用 H.264 压缩,您还将获得更适合 Web 的更小文件。

关于Python OpenCV 视频格式浏览器播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530857/

相关文章:

python - 如果所有行都包含至少一个负元素,则更改矩阵中元素的符号

python - 如何为 Python 3.4 版本安装 PyQt5?

python - 从从文本文件中提取 IP 的正则表达式中排除 IP

python - 如何比较python中的两个列表,对象实例

c++ - OpenCV 的 findHomography 产生无意义的结果

python - 当其他应用程序运行时,线程运行速度不够快

python - Linux 下的 Selenium Chrome 驱动

QT + OpenCV 未定义对 cv::stereoBM::create(int,int) 的引用

python - 错误 : (-215) nimages > 0 in function calibrateCamera using Python and OpenCV

python pandas 从 0/1 数据帧到项目集列表