7

After installed opencv like on Mac OS 10.13.6:

conda install -c conda-forge ffmpeg
conda install -c conda-forge opencv

And using fourcc = cv2.VideoWriter_fourcc('h', '2', '6', '4') in videowriter

I get error:

OpenCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1'

How to fix it?

mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • As far as I know you need libx264 to encode h264, by default you can only decode that format. I'm not sure if installing libx264 would fix your issue though. – Dmitrii Z. Oct 22 '18 at 14:53
  • If OpenCV is using ffmpeg to write, then avc1 is the correct tag for h264 in mp4 – Gyan Oct 22 '18 at 17:27

4 Answers4

4

You should change:

fourcc = cv2.VideoWriter_fourcc('h', '2', '6', '4')

to:

fourcc = cv2.VideoWriter_fourcc(*'avc1')
Gonzalo Garcia
  • 6,192
  • 2
  • 29
  • 32
  • 19
    `sudo apt-get install libx264-dev` did not work for me – João Abrantes Jun 07 '20 at 19:33
  • since this was not clear to me i thought of putting it here for reference... `avc1` actually is `h264` (from wikipedia: "Advanced Video Coding (AVC), also referred to as H.264..." https://en.wikipedia.org/wiki/Advanced_Video_Coding) – raphael Dec 01 '22 at 10:57
4

The codecs are platform dependent, that could be the problem. Try using this combination:

  • extension of file = test.mkv
  • codec . = CV_FOURCC(*'X264)

Here is the reference link

Community
  • 1
  • 1
Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
  • 1
    This worked perfect. Thanks. @mrgloom accept this answer so that it can be helpful to others also. – Imran Jun 27 '21 at 09:45
2

First like some answers pointed out, we should use "AVC1" instead of "h264". Secondly, when we are using opencv-python, there are some licence issues : https://github.com/opencv/opencv-python/issues/207 , we may need to compile on our own.

lenin
  • 851
  • 4
  • 3
1

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. The following codecs work fine for me.

  • In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
  • In Windows: DIVX (More to be tested and added)
  • In OSX: MJPG (.mp4), DIVX (.avi), X264 (.mkv).

Source