1

I am using cv2.VideoWriter() to record from a single camera and writing the video to a .mp4 file with a HEVC codec. I had this script previously working on a (Windows) laptop with the following code:

video = cv2.VideoCapture(0)

frame_width = int(video.get(3))
frame_height = int(video.get(4))

# create output file

out1 = cv2.VideoWriter(videoFileName, cv2.VideoWriter_fourcc(str('H'), str('E'), str('V'), str('C')), 30.0,
                      (frame_width, frame_height))

I am setting up the script on a new laptop and the above code no longer records and throws the following error:

OpenCV: FFMPEG: tag 0x43564548/'HEVC' is not supported with codec id 173 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x31766568/'hev1'
[hevc_mf @ 0000028eed319d00] COM must not be in STA mode
[ERROR:0] global /build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2794) open Could not open codec hevc_mf, error: Unspecified error
[ERROR:0] global /build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2811) open VIDEOIO/FFMPEG: Failed to initialize VideoWriter

I have read through many forums who suggest that this is not possible with cv2 because it does not support the HEVC codec. I have also read the .mp4 and HEVC are possibly not compatible. However, it was previously functioning on the other laptop. Therefore, I know it works in some manner. Note: I did not set it up initially on the other laptop.

I have also tried

# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# fourcc = cv2.VideoWriter_fourcc(*'h264')
# out1 = cv2.VideoWriter(videoFileName, fourcc, 30.0, (frame_width, frame_height))

with mp4v working but not the codec I initially had and h264 throwing an error similar to above.

Has anyone encountered a solution to this problem?

Additional info: Windows 10, Python 3.8.10 through Pycharm, cv2 installed via opencv-python and imported with ´from cv2 import cv2`

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
anelson
  • 55
  • 7
  • According to the following [post](https://stackoverflow.com/questions/59023363/encoding-hevc-video-using-opencv-and-ffmpeg-backend) the "nonfree" version of OpenCV supports HEVC encoding. In my [post](https://stackoverflow.com/questions/61260182/how-to-output-x265-compressed-video-with-cv2-videowriter) I am showing how to encode HEVC using FFmpeg command line tool (it is not complicated). You may also use a python package that uses FFmpeg like [scikit-video](http://www.scikit-video.org/stable/modules/generated/skvideo.io.FFmpegWriter.html). – Rotem Nov 17 '21 at 20:49
  • According to this [page](https://github.com/DigiKlausur/docker-stacks/pull/9), opencv-python-contrib-nonfree==4.1.1.1 has been removed from PyPI. You may build OpenCV from sources if you must... – Rotem Nov 17 '21 at 21:25
  • this is complete nonsense: `cv2.VideoWriter_fourcc(str('H'), str('E'), str('V'), str('C'))` -- write `cv2.VideoWriter_fourcc(*"hvc1")` instead – Christoph Rackwitz Jul 13 '22 at 14:55

0 Answers0