Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV to encode H264 video

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use OpenCV for H264 video coding. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Under Python, the precompiled opencv library is installed by pip, and the video coding in h364 format is realized.

1. Install OpenCV$ pip install opencv-python

It is recommended to install in python virtual environment, which is not easy to affect each other.

two。 Code example

Read the laptop with its own camera and save it as the simplest implementation of video.

Import cv2def main (): output_path ='. / output.avi' vc = cv2.VideoCapture (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 '4') # fourcc = cv2.VideoWriter_fourcc (' X,'V,'I,'D') # fourcc = cv2.VideoWriter_fourcc ('Hang,' E,'V','C') 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 (5) & 0xFF = = ord ('q'): cv2.destroyAllWindows () return-1if _ _ name__ = ='_ main__': main () 2.1 default encoding

Precompiled opencv supports encoding in XVID format by default, but does not support H264.

If you uncomment # fourcc = cv2.VideoWriter_fourcc ('Xboxes,' videos, 'Illumination,' D'), the program can run normally and save the video.

2.2 H264 support

If the code is configured as H264 encoding according to the sample code, the following error message is displayed. The camera can capture the image, but it cannot be saved as video normally.

Failed to load OpenH264 library: openh364-1.8.0-win64.dllPlease check environment and/or download library: https://github.com/cisco/openh364/releases[libopenh364 @ 000001e7a96ddec0] Incorrect library version loadedCould not open codec 'libopenh364': Unspecified error

When prompted, search the Github library for the corresponding dynamic library.

Pay attention to selecting the corresponding version. The version of OpenCV I installed is 4.4.0.42, and I am prompted that the dynamic library I need is openh364-1.8.0-win64.dll. Find the corresponding version in the Release page, extract the downloaded dll file and put it in the project directory at the same level as the py file.

Different versions of OpenCV may require different versions of the support library. There is no testing, just read the prompt.

If you consider generality, you can also add the path where dll is located to the environment variable Path.

This example is implemented on the Windows platform, and the same operation should be done on Linux. Download the corresponding h364 support library file and add it to the address where PATH can search.

Rerun the program to display the image normally and save it as a video file.

Open it with a player such as Potplayer, and you can see the information about the encoding format in the properties:

Video coding: H264-Native D3D9 DXVA Decoder (VLD) 2.3 H265 support

Want to try H265 coding in the same way.

Cancel the comment # fourcc = cv2.VideoWriter_fourcc ('hashes,' estranged, 'Vince,' C') and find the following error:

OpenCV: FFMPEG: tag 0x43564548 Universe HEVC'is not found (format 'avi / AVI (Audio Video Interleaved)')'

It seems that the current precompiled version of OpenCV does not support H265 coding. If you want to code in H265, you need to compile from the source code and add the corresponding support libraries during the compilation process.

Thank you for reading! This is the end of this article on "how to use OpenCV for H264 video coding". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report