21

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.

4 Answers 4

16

I know the question is old but for everyone that is looking for a compatible codec + container for web browser : VP8 or VP80 is a compatible encoder

cv2.VideoWriter_fourcc('V','P','8','0')

I used it with .webM as a container.

Native WebM support by Mozilla Firefox,[7][8] Opera,[9][10] and Google Chrome[11] was announced at the 2010 Google I/O conference

https://en.wikipedia.org/wiki/WebM

it worked like a charm and with pretty good performance even though for some reason I got this error when creating videoWriter objects :

OpenCV: FFMPEG: tag 0x30385056/'VP80' is not supported with codec id 139 and format 'webm / WebM'
3
  • 1
    So strange to get that error, but it to work anyways. Thanks for the answer :)
    – Hakaishin
    Jul 6, 2020 at 8:48
  • Unfortunately vp80 is extremely slow in creating videos. 2k frames took 2 mins to build with less than 1 second for default xvid codec
    – vozman
    Mar 5, 2021 at 13:04
  • 1
    yes indeed an important point to considerate ! it requires more processing power but comes with its advantages as well such as a generally smaller file size and that it's royalty free (open source). i'd say the choice isn't trivial and both can be good depending on the situation.
    – JGauthier
    Mar 12, 2021 at 19:39
15

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.

5
  • 10
    Your solution is getting me this message: OpenCV: FFMPEG: tag 0x34363248/'H264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1' Could not find encoder for codec id 27: Encoder not found I tried to install all the dependencies but it's still working like this. Did you have this issue too? or this is only in linux? Apr 9, 2019 at 14:55
  • if I set fourcc = -1 I get all the codecs for mp4 but none of them are h264 neither x264. Even after inst.alling the libraries. It's some issue due to misconfiguration of the packages? . It's there another codec that is supported by web browsers that is not H264? Apr 9, 2019 at 15:51
  • Did you install the package x264? What distro are using?
    – Isma
    Apr 10, 2019 at 13:20
  • I found out that if I install opecv as pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. Apr 10, 2019 at 13:27
  • 2
    Just find where opencv installs ffmpeg and replace it or delete it and make sure ffmpeg is accessible in your path.
    – Isma
    Apr 10, 2019 at 13:50
2

Get .webm suffix audio file.

fourcc = cv2.VideoWriter_fourcc(*'vp80')
video_writer = cv2.VideoWriter('file.webm', fourcc, 20, (640, 480))

In html:

<body>
<video width="320" height="240" controls>
    <source src="file.webm" type="video/webm">
</video>
</body>

It works on centos7 and Windows10.

0
0

I print the all available mp4 codecs by fourcc=-1.

After that I check codecs which are useful for me. I see there avc1. So I write the code like:

fourcc = cv2.VideoWriter_fourcc(*'avc1')

When print the codes, you also see they are lowercase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.