Ask Your Question
0 silently failing when trying to use h264 / x264

asked 2019-10-01 13:34:54 -0600

bevilacq12 gravatar image

updated 2019-10-02 12:40:03 -0600

I'm trying to record and encode a video using openCV's VideoWriter class. I can successfully record an .avi file using CV_FOURCC('M','J','P','G'), but no mp4 options work. just fails (the function returns False), and I don't know what I'm doing wrong.

I'm using Ubuntu 16.04 and opencv 3.4.1 and writing in C++. With 'X', '2', '6', '4', or 'M','P','4','V', or any of the other variants I've found online, fails. For x264, I've downloaded and built Cisco's open-source openH264, but I don't know how openCV knows to look for it, so I imagine that is a possible cause of my problems. I've tried putting the built executable in the same directory as my program, and I've tried adding the build directory of openH264 to my path, but neither has helped., CV_FOURCC('X','2','6','4'), myFramesPerSecond, myFrameSize)

edit retag flag offensive close merge delete


I don't know linux but about ffmpeg I think you can read this. i think (may be I'm wrong) openh264 cisco is only for windows

LBerger gravatar imageLBerger ( 2019-10-01 14:13:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-10-01 23:01:37 -0600

supra56 gravatar image

You should have ffmpeg and the x264 libraries installed.

sudo apt-get install ffmpeg x264 libx264-dev

Also for mp4.

fourcc = cv2.CV_FOURCC(*'mp4v') or fourcc = cv2.CV_FOURCC(*'mp4v2')


fourcc = cv2.CV_FOURCC(*'MP4V')

For X264.

out = cv2.VideoWriter_fourcc((*'X264')
edit flag offensive delete link more


When I attempt to use cv2.CV_FOURCC, I get make errors that cv2 is an undeclared identifier. I'm including opencv2/opencv.hpp - what else do I need? (edit: actually, I'm thinking this is python code and not C++?)

Also, is there a reason the x264 version is in a different, or would fourcc = cv2.CV_FOURCC(*'X264') work as well?


bevilacq12 gravatar imagebevilacq12 ( 2019-10-02 12:02:39 -0600 )edit

Drop cv2. Should be like this CV_FOURCC

supra56 gravatar imagesupra56 ( 2019-10-02 21:29:12 -0600 )edit

Question Tools

1 follower


Asked: 2019-10-01 13:33:33 -0600

Seen: 4,543 times

Last updated: Oct 02 '19