使用 Python 3.7、OpenCV 和 MPEG4 和 H264 编解码器编写文件时,我得到了意想不到的结果。我读到的所有内容都表明 H264 的压缩率高于 MPEG-4,但我的脚本产生了相反的结果。
我正在使用 OpenCV v3.4.11 和 OpenH264 v1.7。我首先使用 OpenCV 捕获 30 秒的网络摄像头流并将其写入未压缩的文件中。
然后我编写了一个脚本来读取视频文件并写入 AVI 容器中的两个文件,一个使用 MPEG-4 编解码器,另一个使用 H264 编解码器。
为什么 MPEG-4 文件比 H264 文件小?这是我的脚本和结果。
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