添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
冷冷的马铃薯  ·  H264 encoding failure ...·  4 小时前    · 
有胆有识的油条  ·  Can you support ...·  4 小时前    · 
没有腹肌的香菇  ·  How to build OpenCV ...·  4 小时前    · 
纯真的毛衣  ·  #5319 (ac3 decoder ...·  4 天前    · 
谈吐大方的毛衣  ·  Ffmpeg | Invalid data ...·  4 天前    · 
宽容的刺猬  ·  应力性骨折_百度百科·  1 月前    · 
暴走的树叶  ·  Vue3 简介 | DataFan UI·  4 月前    · 
玩滑板的黄花菜  ·  黑莲花美人_第 5 ...·  11 月前    · 
儒雅的企鹅  ·  Access attributes ...·  1 年前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected behaviour

The original opencv version(4.2-dev) can support "H264" codec. however your version(4.2.0.32) doesn't.

Actual behaviour

write mp4 to file from webcam

Write here what went wrong.
not support H264 format codec

Steps to reproduce

python3:
codec = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter("output/11.mp4" , codec, fps, (frame_width, frame_height))

  • example code
    python3:
    codec = cv2.VideoWriter_fourcc(*'H264')
    out = cv2.VideoWriter("output/11.mp4" , codec, fps, (frame_width, frame_height))
  • operating system
    mac os 10.14
  • architecture (e.g. x86)
  • opencv-python version
    4.2.0.32
  • libx264 that backs H.264 support in FFMpeg is under GPL which is incompatible with opencv-python 's MIT license. See https://www.ffmpeg.org/legal.html

    If you need it, you can make a custom build of FFMpeg and then of opencv-python , directing it to link against that copy. Look at the build scripts for how it's done and do the same with any necessary changes.
    (You are responsible for any legal considerations in the course of using the result, of course.)

    libx264 that backs H.264 support in FFMpeg is under GPL which is incompatible with opencv-python 's MIT license. See https://www.ffmpeg.org/legal.html

    If you need it, you can make a custom build of FFMpeg and then of opencv-python , directing it to link against that copy. Look at the build scripts for how it's done and do the same with any necessary changes.
    (You are responsible for any legal considerations in the course of using the result, of course.)

    Hi @pospospos2007 @native-api now i use opencv-python 4.4.0

    >>> 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.