0

I am getting unexpected results with writing a file using Python 3.7, OpenCV and using MPEG4 and H264 codecs. Everything I read states that H264 has a higher compression rate than MPEG-4, but my script produces the opposite results.

I’m using OpenCV v3.4.11 and OpenH264 v1.7. I first used OpenCV to capture a webcam stream for 30 seconds and write it to a file uncompressed.

I then wrote a script to read the video file and write to two files in an AVI container, one using the MPEG-4 codec and the other using the H264 codec.

Why is the MPEG-4 file smaller than the H264 file? Here is my script and results.

import cv2, os

infile = 'SourceVideo.AVI'
outfile1 = 'TestVideo-mpeg4.avi'
outfile2 = 'TestVideo-h264.avi'

cap = cv2.VideoCapture(infile)
fourccmpeg4 = cv2.VideoWriter_fourcc('m','p','4','v')
fourcch264 = cv2.VideoWriter_fourcc('h','2','6','4')
mpeg4 = cv2.VideoWriter(outfile1, fourccmpeg4, 30, (1280, 720))
h264 = cv2.VideoWriter(outfile2, fourcch264, 30, (1280, 720))
ret = True
while ret == True:
    ret, frame = cap.read()
    mpeg4.write(frame)
    h264.write(frame)
mpeg4.release()
h264.release()
cap.release()
print('MPEG4 Size = ', os.path.getsize(outfile1), ' bytes')
print('H264 Size = ', os.path.getsize(outfile2), ' bytes')


MPEG4 Size =  35160392  bytes
H264 Size =  50908015  bytes
slalomchip
  • 779
  • 2
  • 9
  • 25
  • Because it uses whatever default target bitrate / quality which was set for that particular codec in VideoWriter. You need to find a way to control the encoding settings, although I don't see a lot of options. – aergistal Oct 27 '21 at 07:23
  • That makes perfect sense. I found some hints (and issues) of being able to control the bitrate with libopenh264 but lobopenh264 but it appears that libopenh264 is not available on a Windows platform (linux and osx only). – slalomchip Oct 27 '21 at 11:24

0 Answers0