Opencv-python: H264 encoding failure

Created on 3 Jun 2018  ·  12Comments  ·  Source: opencv/opencv-python

Expected behaviour

I'm trying to open a VideoWriter using H264 encoding.

Actual behaviour

I'm getting an error when trying to write H264 encoded files/

OpenCV: FFMPEG: tag 0x34363248/'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

And I'm unable to write frames to file. I've tried using X264 but I get the same error.

Steps to reproduce

Code:

import cv2
fourcc = cv2.VideoWriter_fourcc(*'H264')
video_out = cv2.VideoWriter('test.mp4', int(fourcc), 25.0, (640, 480))

System setup:
Ubuntu 16.4,
FFMPEG 3.4.2 (upgraded from the default Ubuntu version due to this error, but it didn't solve it)
Opencv 3.4.1
Intel i7

Most helpful comment

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: https://github.com/skvark/opencv-python/issues/81

You'll have to compile OpenCV manually to get support for H264 encoding.

All 12 comments

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: https://github.com/skvark/opencv-python/issues/81

You'll have to compile OpenCV manually to get support for H264 encoding.

I am using an mp4 video and is it possible to get support for it , when I install open-cv by pip ?

As explained above: no (if you mean encoding to h264).

Sorry to bump this, but does anyone have any guidance on going about setting this up?
Whenever I try compiling the build with cmake, my locally installed ffmpeg implementation is not detected.

The opencv_thirdparty page says that the build should auto-detect ffmpeg if installed on UNIX systems...

Please ask at http://answers.opencv.org/questions/, you will get better answers there.

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: #81

You'll have to compile OpenCV manually to get support for H264 encoding.

How?

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: #81
You'll have to compile OpenCV manually to get support for H264 encoding.

How?

https://docs.opencv.org/master/df/d65/tutorial_table_of_content_introduction.html

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: #81

You'll have to compile OpenCV manually to get support for H264 encoding.

If install opencv python in anaconda, I could encode MP4 video with fourcc avc1, which let me get video format like this:

Video: h264 (High) (avc1 / 0x31637661), yuv420p

That's all right.

If you installed this package via pip install opencv-python then there's no encoding support for x264 because it's under GPL license. Upgrading FFmpeg won't help because opencv-python ships with its own FFmpeg. See: #81

You'll have to compile OpenCV manually to get support for H264 encoding.

I compile opencv with BUILD_opencv_python3=ON and the opencv_python I get could use fourcc AVC1.

HAHAHA

Could you guide me through the procedure?

On Sun, 17 Nov 2019 at 9:44 PM, FantasyJXF notifications@github.com wrote:

If you installed this package via pip install opencv-python then there's
no encoding support for x264 because it's under GPL license. Upgrading
FFmpeg won't help because opencv-python ships with its own FFmpeg. See:

81 https://github.com/skvark/opencv-python/issues/81

You'll have to compile OpenCV manually to get support for H264 encoding.

I compile opencv with BUILD_opencv_python3=ON and the opencv_python I get
could use fourcc AVC1.

HAHAHA


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/skvark/opencv-python/issues/100?email_source=notifications&email_token=AHJ6FQQKCJM4ZXKX6SY2533QUFUW3A5CNFSM4FDAIIIKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEIPYTA#issuecomment-554761292,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AHJ6FQXMHILOUVXUP32S5YLQUFUW3ANCNFSM4FDAIIIA
.

Yeah, if you link OpenCV against local build of FFmpeg you will most likely get support for encoding to h264.

To compile OpenCV locally with this repo's toolchain:

  1. Clone this repository
  2. Optional: set up ENABLE_CONTRIB and ENABLE_HEADLESS environment variables to 1 if needed
  3. Optional: add additional Cmake arguments to CMAKE_ARGS environment variable
  4. Run python setup.py bdist_wheel

Note: Anaconda package is different than the package provided by this repository: https://github.com/conda-forge/opencv-feedstock

Env

  • CentOS 7.3
  • Python 3.6.8
  • OpenCV 3.4.6
  • OpenCV_Contrib_3.4.6
  • ffmpeg 4.1.3

cmake config

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.6/modules \
    -D PYTHON3_LIBRARY=/usr/local/python36/lib/libpython3.6m.a \
    -D PYTHON3_INCLUDE_DIR=/usr/local/python36/include/python3.6m \
    -D PYTHON3_EXECUTABLE=/usr/local/python36/bin/python3 \
    -D PYTHON3_PACKAGES_PATH=/usr/local/python36/lib/python3.6/site-packages \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D WITH_FFMPEG=ON \
    -D CMAKE_CXX_FLAGS="-std=c++11" ..

Test Code

import cv2 as cv
import numpy as np

vid = cv.VideoWriter('pyout1.mp4', cv.VideoWriter_fourcc(*'avc1'), 25, (300,300))
#vid = cv.VideoWriter('pyout.mp4', 0x00000021, 25, (300,300))

for i in range(250):
    img = np.random.randint(0,255, (300,300,3), dtype=np.uint8)
    vid.write(img)
vid.release()

And you would get the H264(AVC1) coded video.

Was this page helpful?
0 / 5 - 0 ratings