33

I'm trying to process frames from a video stream, and it as a new video.

This is what I'm doing :

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

I keep getting :

OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

I think I'm using the wrong fourcc value... Which one should I use ? I've been trying a lot of them.

I'm using Ubuntu 16.04, Python 2.7.11 and OpenCV 3.1.0

11 Answers 11

22

Define the codec and create VideoWriter object like this

fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
Sign up to request clarification or add additional context in comments.

4 Comments

It's best to post an answer with more than just code.
working c version for Ubuntu 16.04 ` outputVideo = cvCreateVideoWriter("/tmp/out.avi",CV_FOURCC('M', 'P', 'E', 'G'),25,imgSize,1);`
@JeffBridgman I just posted an answer below explaining the issue. Cheers
I am getting below error. - OpenCV: FFMPEG: tag 0x4745504d/'MPEG' is not supported with codec id 2 and format 'mpegts / MPEG-TS (MPEG-2 Transport Stream)'. What I am doing wrong --- fourcc = cv2.VideoWriter_fourcc(*"MPEG") writer = cv2.VideoWriter(outpath, fourcc, 30, (W, H), True)
7

For Windows users

I am using OpenCV 2 with Python 3.6, on Windows 10.

The 'XVID' codec, along with producing a .avi file, seems to be the most generic solution (if not the only one that works).

fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('test.avi', fourcc, 60, (320, 240))

In addition, only BGR can be directly written with such a VideoWriter declaration. Don't try to write gray frames: the output would be empty.

3 Comments

And you need to revert the sizes of images if you read them from cv2.imread : image = cv2.imread(path_to_image) frameSize = (image.shape[1],image.shape[0])
Regarding BGR writings, there is isColor parameter in VideoWriter constructor, setting it to False enables to write gray
@Right this did not work for me on Ubuntu 18, OpenCV 3.4.3
7

The problem that you are having is that you are trying to export the frames in XVID format but the name of your output file is ending with .mp4. You should change the export format to MP4V or the name of your output file to .avi.

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

alternative

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.avi',fourcc, fps, (1080,1080))

here you can find more information about the video codecs

1 Comment

('XVID') with '.avi' is working, but if you want to create '*.mp4' -> ('m', 'p', '4', 'v') it worked in my Wind10 laptop. Interesting ...
3

The size of the frame(width,height) you are giving, should match the size of the frame you want to save. so
fw = int(cap.get(3)) fh = int(cap.get(4)) print("w,h",fw,fh) out = cv2.VideoWriter('fb1.avi',cv2.VideoWriter_fourcc('X','V','I','D'), fps, (fw,fh))

Comments

3

If you want to save video by opencv in mp4 format, let

Follow this link:

you should replace:

fourcc = cv2.VideoWriter_fourcc(*'XVID')

by:

fourcc = cv2.VideoWriter_fourcc(*'FMP4')

I tried and succeeded.

1 Comment

I am using MacOS none of the answers worked for me but *'FMP4' did the trick, thanks!
2

I had the same problem. With me, it turned out that I had switched the height and width of the video, so the frame dimensions did not match the video specifications and as a result nothing was written. Make sure they match exactly.

Also, OpenCV seems to give that same warning if the file extension does not match the codec used. Specifically, it wants .avi for the XVID codec.

Comments

2

I wanted to save as a .mp4, and using *"mp4v" turned out to be the right code, on Linux at least.

Check the example below

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))

Comments

1

Install K-Lite Mega Codec Pack: https://filehippo.com/download_klite_mega_codec/
This error occurs because some codecs are not available by default in Windows media player. So by installing this software, the video works fine with same code.

Comments

0

If you are using linux try this

fourcc = 0x00000021
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

Comments

0

In my case, i use:

fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video = cv2.VideoWriter(video_name, fourcc, FPS, (width,height))

and this work :>

Comments

-2

On a mac

writer = cv2.VideoWriter('mysupervideo.mp4', cv2.VideoWriter.fourcc(*'mp4v'), 20, (width, height))

Works better

1 Comment

that looks equivalent to existing answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.