what is the codec for mp4 videos in python OpenCV
Asked Answered
T

6

41
fourcc = cv2.cv.CV_FOURCC(*'XVID')

The above line is used for avi video. In the same fashion, which codec do we use for mp4 videos in Ubuntu?

Tracery answered 7/5, 2015 at 13:49 Comment(1)
Do you consider any of this answers the correct one? Can you mark it ?Bevins
B
47

You can also use mp4v

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

where the videowriter should look like this:

out = cv2.VideoWriter('output.mp4',fourcc, 15, size)

But there are more codecs available for mp4. You can see the list of them by setting fourcc = -1, it will show a list like this:

OpenCV: FFMPEG: format mp4 / MP4 (MPEG-4 Part 14)
fourcc tag 0x7634706d/'mp4v' codec_id 000C
fourcc tag 0x31637661/'avc1' codec_id 001B
fourcc tag 0x33637661/'avc3' codec_id 001B
fourcc tag 0x31766568/'hev1' codec_id 00AD
fourcc tag 0x31637668/'hvc1' codec_id 00AD
fourcc tag 0x7634706d/'mp4v' codec_id 0002
fourcc tag 0x7634706d/'mp4v' codec_id 0001
fourcc tag 0x7634706d/'mp4v' codec_id 0007
fourcc tag 0x7634706d/'mp4v' codec_id 003D
....

All of them supports mp4 but h264 is supported by Web browsers if you want to serve the video into the web.

Bevins answered 9/4, 2019 at 15:31 Comment(2)
How do you generate that list? Where do you set fourcc = -1?Deer
literally pass fourcc = -1 to the constructor of VideoWriter and then this output will appearSpondylitis
W
21

The codec is H.264.

One of these should work for you:

fourcc = cv2.VideoWriter_fourcc(*'h264')
#or 
#fourcc = cv2.VideoWriter_fourcc(*'x264')
#or
#fourcc = cv2.VideoWriter_fourcc(*'mp4v')

However, I should warn you that you'll probably need to have ffmpeg and the x264 libraries installed so since you are in Ubuntu, try doing this command in the terminal:

sudo apt-get install ffmpeg x264 libx264-dev

Also, check out this link from OpenCV tutorials for more details as to the kinds of FourCC codes available for your platform.

In the above link, it says X264 is the FourCC code you should use, but switch between them until you get it to work.

Wandie answered 7/5, 2015 at 16:14 Comment(4)
Out of all solutions installing the codec seperate from open cv 4 with sudo apt-get install ffmpeg x264 libx264-dev did the job to get it working on my Raspberry Pi 3. Thanks!Hardship
ffmpeg rejects "h264" for mp4 format containers. it says FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)' but it accepts "avc1", as listed in the other answer. both designate the same video compression, H.264/AVC, but the MP4 format only accepts specific tags. it's a standard.Spondylitis
Would H.264 work for the browser?Sumner
@MehdiCharife yesWandie
M
6
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')

Always seems to work.

Manton answered 19/11, 2019 at 2:21 Comment(0)
B
1

This is an old question. But, if anyone is facing an issue lately using the codec who can't get a saved video. They can use 0X00000021 as the codec value for OpenCV 3 and later.

Boyer answered 21/12, 2017 at 7:56 Comment(0)
T
1

I had to reinstall ffmpeg and compile opencv from source in order to get this working. I documented the steps in this blog post.

Titration answered 25/6, 2020 at 16:23 Comment(0)
T
0

i just used fourcc as -1 and problem solved. my editor is vscode and os is windows.

Torruella answered 20/2 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.