(Python) What is the four characters FOURCC code for mp4 encoding on TX2

In order to encode the video in Python, a four characters string is used to define the coding method. I tried to set the code to ‘MP4V’, as suggested by online references

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
voObj = cv2.VideoWriter('output.mp4',fourcc, 15.0, (1280,360))

But it’s not working. I got an error message saying ,

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

Encoding avi video works by setting the FOURCC code to ‘XVID’. For example,

fourcc = cv2.VideoWriter_fourcc(*'XVID')
voObj = cv2.VideoWriter('output.avi',fourcc, 15.0, (1280,360))

which compressed the content in avi video as expected.

Any idea about the FOURCC for mp4?

Does “X264” work for you?

Thanks WayneWWW. I tried “X264” as suggested but still failed.

OpenCV: FFMPEG: tag 0x34363258/'X264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'

Any other thoughts?

PS. Perhaps I should ask, is there a better way to write out mp4 than what I did like below:

fourcc = cv2.VideoWriter_fourcc(*'X264')
voObj = cv2.VideoWriter('output.mp4',fourcc, 15.0, (1280,360))
while True:
    # . . . some settings . . .
    ret_val0, dispBuf = cap.read();
    voObj.write(dispBuf)
    # . . . more stuff

I Google searched the FOURCC thing, couldn’t find an official statement besides a general disclaimer in docs of OpenCV 3.0 saying that “FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found in fourcc.org. It is platform dependent.”
https://docs.opencv.org/3.0.0/dd/d43/tutorial_py_video_display.html
FI also went to OURCC.org, it has a list of ton of codecs to choose, I tried “X264”, “H264”, “MP4V”, “mp4v” . . .none of those worked on TX2.
http://www.fourcc.org/codecs.php

Some people hinted to give up on cv2.VideoWriter_fourcc(). Alternatively, plug in the ASCII number directly to cv2.VideoWriter(). So, here is what I tried:

voObj = cv2.VideoWriter('output.mp4', 0x00000021, 15.0, (1280,360))
while True:
    . . .
    voObj.write(dispBuf)

And, it works!

1 Like

This accepted “solution” is circular. You claim in comment #3 that that your write fails, with OpenCV telling you “OpenCV: FFMPEG: fallback to use tag 0x00000021/’!???’”. Then in your comment #4 you simply hard coded in the tag 0x00000021/’!???’ (which is what OpenCV was doing for you prior) and you claim the solution now works.

Do you know what ‘!???’ is as a fourcc codec?

@GWAVE, I plugged in the code “0x00000021” directly to my Python code (instead of generating the fourcc using cv2.VideoWriter_fourcc function, see the code in comment #4). It seems working. I haven’t experimented it on the latest version of Jetson.

fourcc = cv2.VideoWriter_fourcc(*“mp4v”)

1 Like

Thank you soo much for this @tmx3, you save my life <3
Works nice, no more errors on my application.

Please try ‘avc1’ in lowercase. It works for me.