0
votes

What is the video codec for mp4 in OpenCV 4.1.1.26?

I'm trying to write videos from two cameras, writing AVI video is working fine but writing the mp4 video is not working.

  • Python 3.7
  • OpenCV 4.1.1.26
  • Windows 10

Below is sample code for one camera

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output_1.mp4', fourcc, 15, (640, 480))

I used different codecs (MPEG, H264, mp4v, ('m','p','4','v'), FFmpeg etc.) as in OpenCV 4 Documentations , FOURCC Codecs Documentations and suggested by different experts and helpers on different platforms but any solution is not working in my code, mp4 video is saved but can't be played in any media player even in OpenCV VideoCapture due to problem in codec. On other hand when I save avi video, it works fine. I checked these links to get help.

what is the codec for mp4 videos in python OpenCV

https://medium.com/@iKhushPatel/convert-video-to-images-images-to-video-using-opencv-python-db27a128a481

How to write mp4 video file with H264 codec?

Looking for valuable suggestions.

1
if camera gives frame bigger then (640, 480) then you have to change its size before you save it in file. If you don't chage size then it may create empty file and then you can't play itfuras
Could you explain it in detail?Muhammad Ahsan
if camera gives frame bigger than (640,480) then you may have to do frame = cv2.resize(frame, (640, 480)) before you save it.furas
don't you get error message when you run code? When I try DIVX with extension .mp4 then it shows me message that FFMPEG can't create it - but if I use extension .avi then it has no problem to create file. But I use Linux.furas

1 Answers

0
votes

I also had a similar problem. What I found was that the OpenCV-python usually gets some unexpected errors with the mp4 files (fails to read frames from the mp4 file, etc).

To solve this problem, what I did was re-write codes by using the skvideo(scikit-video). The scikit-video uses the command line FFmpeg tool, so you need to install FFmpeg before using the scikit-video.