乐闻世界logo
搜索文章和话题

What is the codec for mp4 videos in python OpenCV

1个答案

1

In the OpenCV library for Python, when processing MP4 videos, commonly used codecs include H.264 (also known as AVC) and H.265 (also known as HEVC). These codecs are widely employed for compressing video files to reduce file size while maintaining high visual quality.

For instance, when using the cv2.VideoWriter() function in OpenCV to create a video file, you can specify a four-character code to select the codec. For H.264, 'X264' is commonly used (sometimes 'avc1' or 'H264' is used), and for H.265, 'X265' can be used.

Here is a simple code example demonstrating how to write an MP4 video using the H.264 codec:

python
import cv2 cap = cv2.VideoCapture('input.mp4') fourcc = cv2.VideoWriter_fourcc(*'X264') out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (1920,1080)) while cap.isOpened(): ret, frame = cap.read() if ret: out.write(frame) else: break cap.release() out.release()

In this example, cv2.VideoCapture is used for reading the video, cv2.VideoWriter_fourcc for specifying the H.264 codec, and cv2.VideoWriter for writing the video. This approach allows you to handle high-quality video streams while controlling the size and quality of the output video.

2024年8月15日 11:52 回复

你的答案