2

I'm very new to codecs. I have 100 frames and I want to convert them into a video using H265 encoder. previously I tried with h264 and video generated with file size 2.5MB. Now when I tried with hvc1 and the output video file is much bigger(65MB). I also tried with hev1 still getting the same 65MB file. But heard that h265 will generate a lesser file compared to h264. Can anyone help me with this problem?

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()

am I using the correct codec ids of hevc for in this code?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • H.265 is not guarantee to produce smaller file. In terms of quality, H.265 most likely produce better quality for same file size as H.264 or smaller file size with the same quality as H.264. OpenCV has [no interface](https://stackoverflow.com/questions/25998799/specify-compression-quality-in-python-for-opencv-video-object) to let you configure the compression rate. – Rotem Sep 22 '21 at 21:49
  • You may use [MediaInfo](https://mediaarea.net/en/MediaInfo) for verifying that the output is actually H.264 (AVC) and H.265 (HEVC). Do you have the ["nofree" version](https://stackoverflow.com/questions/59023363/encoding-hevc-video-using-opencv-and-ffmpeg-backend) of OpenCV? – Rotem Sep 22 '21 at 21:49
  • use ***`hvc1`*** fourcc. and hope that OpenCV was built with ffmpeg and ffmpeg was built with appropriate codecs. – Christoph Rackwitz Jul 15 '22 at 11:42
  • You can directly solve your problem using ffmpeg – Ali Gökkaya Jul 22 '22 at 15:15

0 Answers0