The original opencv version(4.2-dev) can support "H264" codec. however your version(4.2.0.32) doesn't.
>>> cv2.__version__
'4.4.0'
i found opencv can write h264 video
import cv2
########3# attention, use avc1 instead of x264 or h264
fourcc_type = 'avc1'
# fourcc_type = 'mp4v'
output_path = 'output.mp4'
def main():
# open camera
vc = cv2.VideoCapture('/home/zj/test.mp4')
if not vc.isOpened():
print('Error: can not opencv camera')
exit(0)
ret, frame = vc.read()
w = int(vc.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = vc.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*fourcc_type)
vw = cv2.VideoWriter(output_path, fourcc, fps, (w, h), True)
while ret:
vw.write(frame)
ret, frame = vc.read()
# cv2.imshow('frame', frame)
# if cv2.waitKey(int(1 / fps * 1000)) & 0xFF == ord('q'):
# break
# cv2.destroyAllWindows()
vw.release()
if __name__ == '__main__':
main()
look into opencv-python/opencv/3rdparty/ffmpeg/readme.txt
FFMPEG build includes support for H264 encoder based on the OpenH264 library.
OpenH264 Video Codec provided by Cisco Systems, Inc.
See https://github.com/cisco/openh264/releases for details and OpenH264 license.
OpenH264 library should be installed separatelly. Downloaded binary file can be placed into global system path
(System32 or SysWOW64) or near application binaries (check documentation of "LoadLibrary" Win32 function from MSDN).
Or you can specify location of binary file via OPENH264_LIBRARY environment variable.
and i find libopenh264 in anaconda
envs/lib$ file libopenh264.*
libopenh264.a: current ar archive
libopenh264.so: symbolic link to libopenh264.so.2.1.1
libopenh264.so.2.1.1: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, not stripped
libopenh264.so.6: symbolic link to libopenh264.so.2.1.1
Hi @zjykzj
Following your steps, I have tried to install libopenh264 via conda.
conda install -c conda-forge openh264
but I am still getting this error in opencv-python 4.4.0.40
Could not find encoder for codec id 27: Encoder not found
I have also tried setting the OPENH264_LIBRARY
env variable, but got the same error.
Could you give me more details of how you achieved it?
Hi @zjykzj
Following your steps, I have tried to install libopenh264 via conda.
conda install -c conda-forge openh264
but I am still getting this error in opencv-python 4.4.0.40
Could not find encoder for codec id 27: Encoder not found
I have also tried setting the OPENH264_LIBRARY
env variable, but got the same error.
Could you give me more details of how you achieved it?
hi @pospospos2007 @native-api @mlorenzo-alice , have you solved this problem yet?
i checked my answer again, found there have some questions. Here is my complete solution
my problem
i want to use freetype in opencv, so i need to compile myself using this repo
opencv_contrib_python
here is my solution:
# download
$ git clone --recursive https://github.com/skvark/opencv-python.git
# set freetype
$ export CMAKE_ARGS="-DWITH_FREETYPE=ON"
# enable contrib
$ export ENABLE_CONTRIB=1
# compile
$ pip wheel . --verbose.
After completing the above operations, you can find opencv_contrib_python-4.5.1.48-cp38-cp38-linux_x86_64.whl
$ pip install opencv_contrib_python-4.5.1.48-cp38-cp38-linux_x86_64.whl
openh264
i also tried this operation
$ conda install -c conda-forge openh264
but it doesn't work, so i download the openh264-2.1.1-h780b84a_0.tar.bz2
from conda-forge / packages / openh264
use pip to install it
$ pip install --use-local openh264-2.1.1-h780b84a_0.tar.bz2
After completing the above operations, you should be able to find and use h264 in opencv
def get_output_file(self, path, fps=30):
Return a video writer object.
Args:
path (str): path to the output video file.
fps (int or float): frames per second.
return cv2.VideoWriter(
filename=path,
fourcc=cv2.VideoWriter_fourcc(*"avc1"),
fps=float(fps),
frameSize=(self.display_width, self.display_height),
isColor=True,
Thank you @zjykzj. Those instructions did work!
Note: I just had to replace pip install --use-local openh264-2.1.1-h780b84a_0.tar.bz2
with conda install --use-local openh264-2.1.1-h780b84a_0.tar.bz2
)
my fault ! thank you for fix it
My problem is solved with the following. May be all are not necessary for everyone but I am sharing what I did. I am using anaconda with python 3.8
Opencv 4.5.1 (Its must because for opencv less than 4.4 this codec is not working) using following command
pip install opencv-python
Opencv contrib 4.5.1 using following command
pip install opencv-contrib-python
Install Openh264 using following command
conda install openh264
av for ffmpeg using following command
pip install av
@zjykzj Can you show the solution for windows. How to solve this problem in windows
hi @imihassan, my daily working environment is Ubuntu 18.04, so I can't actually do it in windows
but there are may be two solutions:
use WSL in win10;
the operation of git/pip/conda/anaconda on Linux / Windows platform is consistent, so what you need to pay attention to is how to set environment variables and install the compiled version of windows openh264.
I'm glad to see that you've solved the problem
Any idea why on windows it doesn't require to rebuild opencv-python https://stackoverflow.com/questions/41972503/could-not-open-codec-libopenh264-unspecified-error, but on linux we need to ?
Also I am trying to rebuild on ubuntu 20, but libavcodec seem to depend on libx264, do you know which dependencies I need to install to build opencv-python without x264 but with libopenh264 ?
Any idea why on windows it doesn't require to rebuild opencv-python https://stackoverflow.com/questions/41972503/could-not-open-codec-libopenh264-unspecified-error, but on linux we need to ?
Afaik, on Linux the codec library must be linked to the OpenCV binary during the build. On Windows, OpenCV has a feature that allows it to load the external shared library during runtime.