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?
In this Python OpenCV lesson we are going to learn How to Play Mp4 Videos in Python OpenCV, before this we have learned that how you can read avi videos in opencv, you can use an mp4 video or you can use camera, the same code will be implemented with camera or mp4 video.
A FOURCC code is a 32-bit unsigned integer that is created by concatenating four ASCII characters. For example, the FOURCC code for YUY2 video is 'YUY2'. For compressed video formats and non-RGB video formats (such as YUV), the biCompression member of the BITMAPINFOHEADER structure should be set to the FOURCC code.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With