首页 > 解决方案 > 如何使用 H265 编码视频?

问题描述

我对编解码器很陌生。我有 100 帧,我想使用 H265 编码器将它们转换为视频。以前我尝试过使用 h264 和文件大小为 2.5MB 的视频。现在,当我尝试使用 hvc1 并且输出视频文件更大(65MB)时。我还尝试使用 hev1 仍然获得相同的 65MB 文件。但是听说h265比h264生成的文件要小一些。谁能帮我解决这个问题?

import cv2
import os
import re
image_folder = 'backend/data'

video_name = 'backend/h265/output.mp4'

images = [img for img in os.listdir(image_folder) if img.endswith(".jpg")]
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
    return [
        int(text)
        if text.isdigit() else text.lower()
        for text in _nsre.split(s)]

sorted_images = sorted(images, key=natural_sort_key)
frame = cv2.imread(os.path.join(image_folder, sorted_images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'hev1')                #hvc1 and hev1 are two codec ids of hevc
video = cv2.VideoWriter(video_name, fourcc, 27, (width,height))
for image in sorted_images:
    video.write(cv2.imread(os.path.join(image_folder, image)))
cv2.destroyAllWindows()
video.release()

我是否在此代码中使用了正确的 hevc 编解码器 ID?

标签: python-3.xcv2codech.265

解决方案


推荐阅读