7

I am using opencv-python==4.5.1.48 and python3.9 docker. I want to save a video in h264 format. Here is my function to save a video:

import cv2

def save_video(frames):
    fps = 30
    video_path = '/home/save_test.mp4'
    fourcc = cv2.VideoWriter_fourcc(*'h264')
    video_writer = cv2.VideoWriter(video_path, fourcc, fps, (112, 112))

    for frame in frames:
        video_writer.write(frame)

    video_writer.release()

When I use .mp4 format to save video, I get the following 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' Could not find encoder for codec id 27: Encoder not found

I search and read some solutions but none of them solve my issue.

Update:

I also install libx264-dev which was recommended in this post and did not work.

Mohammadreza Riahi
  • 502
  • 1
  • 6
  • 20
  • 2
    use `avc1` fourcc -- opencv probably uses its own ffmpeg, so what you install in your system won't matter – Christoph Rackwitz Dec 06 '21 at 15:09
  • 2
    Thanks. I change the `h264` to `avc1` and get this error: `Could not find encoder for codec id 27: Encoder not found` – Mohammadreza Riahi Dec 06 '21 at 15:25
  • since that didn't help, I guess you can't use H.264 https://github.com/opencv/opencv-python/issues/207 or you have to build OpenCV yourself, or find a package that disregards the GPL -- use a different codec. `MJPG` always works because it's built into OpenCV. or `mp4v`, that's the predecessor of H.264. – Christoph Rackwitz Dec 06 '21 at 15:40

3 Answers3

3

you can either build opencv by yourself(as mentioned above) or do a quicker solution: install anaconda3 and after creating a new environment run "conda install -c conda-forge opencv"

gilad eini
  • 360
  • 2
  • 6
2

Finally, I found the solution. I can solve my problem in the ubuntu:20.04 docker. The important thing you should notice is that you should install OpenCV via apt-get install python3-opencv not using pip.

Mohammadreza Riahi
  • 502
  • 1
  • 6
  • 20
  • 1
    Tried installing python3-opencv on ubuntu:20.04 but no luck. Could you please share your dockerfile? 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' [ERROR:0@1.556] global /io/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2767) open Could not find encoder for codec_id=27, error: Encoder not found [ERROR:0@1.556] global /io/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (2839) open VIDEOIO/FFMPEG: Failed to initialize VideoWriter – ロジャー Jan 28 '22 at 03:56
  • I also install these ubuntu packages: RUN apt-get update && apt-get install -y \ wget \ build-essential \ pkg-config \ cmake \ ninja-build \ libgl1-mesa-glx \ libleptonica-dev \ ffmpeg \ zlib1g-dev \ libncurses5-dev \ libgdbm-dev \ libnss3-dev \ libssl-dev \ libreadline-dev \ libffi-dev \ python3.8 \ python3-pip \ python3-opencv @y2kbug – Mohammadreza Riahi Jan 29 '22 at 06:43
  • I am still getting the same error message. May you take a look on my dockerfile please? https://pastebin.com/RvCK0YBR (btw why tagging username does not work) – ロジャー Jan 30 '22 at 06:23
  • @y2kbug Ok. I will check your DockerFile with my python sample code and inform the result to you. – Mohammadreza Riahi Jan 30 '22 at 06:26
  • @y2kbug I test it. When I do install OpenCV with pip it works. In your dockerfile you install OpenCV with pip. – Mohammadreza Riahi Jan 30 '22 at 09:23
  • Should it be `apt-get install python3-opencv`, `pip3 install opencv-python` or both? – ロジャー Jan 30 '22 at 10:32
  • @y2kbug You should install OpenCV only by this `apt-get install python3-opencv`. – Mohammadreza Riahi Jan 30 '22 at 11:04
  • 1
    This is the new dockerfile https://pastebin.com/Hzeey5qD and I am still getting the error https://pastebin.com/SU8xYQFt – ロジャー Jan 30 '22 at 11:15
  • I have tried building OpenCV https://pastebin.com/CizysgwJ During build it can detect FFMpeg https://pastebin.com/JmUZ5x1L FFMpeg should support H264 encoding. No idea why OpenCV still cannot encode with H264 – ロジャー Jan 30 '22 at 11:19
  • I do not mean that. You can download my Dockerfile from this link. https://drive.google.com/file/d/1zItn2Vu5rUPRJXhr9ETM8RRHwn0pdknO/view?usp=sharing – Mohammadreza Riahi Jan 30 '22 at 11:49
  • 1
    Same error using your dockerfile. Thanks anyway. – ロジャー Jan 30 '22 at 12:36
  • @y2kbug Can you share your code for saving video? – Mohammadreza Riahi Jan 30 '22 at 12:43
  • https://pastebin.com/SU8xYQFt – ロジャー Jan 30 '22 at 13:08
  • MP4V works for my needs. I am giving H264 / H265 / X264 / X265. Thanks. – ロジャー Jan 30 '22 at 14:45
2

Below is my solution on ubuntu20.04:

sudo apt install build-essential cmake git python3-dev python3-numpy \
libavcodec-dev libavformat-dev libswscale-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer1.0-dev libgtk-3-dev \
libpng-dev libjpeg-dev libopenexr-dev libtiff-dev libwebp-dev \
libopencv-dev x264 libx264-dev libssl-dev ffmpeg


python -m pip install --no-binary opencv-python opencv-python

refer

Tombon
  • 21
  • 3