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