我正在Spyder中使用openCV模块将黑色矩形放在文件夹内的所有视频文件上。在每个视频上都放一个黑色矩形之后,我调整帧的大小并将其写到新的视频文件中。 该输出视频的大小为2GB,而原始输入视频的大小仅为155MB。为了获得具有与输入视频完全相同的属性(大小,分辨率,总帧数...)的输出视频,我应该怎么做?
我的代码如下。调整大小时可能是我做错了事,还是VideoWriter使用了错误的参数?
谢谢。
import numpy as np
import cv2
import os
directory = "OUTPUT_Anonymous_Blackbox_videos"
parent_dir = os.path.dirname(os.getcwd())
path = os.path.join(parent_dir, directory)
try:
os.mkdir(path)
except OSError:
print("Creation of the directory %s failed" % path)
else:
print("Successfully created the directory %s " % path)
os.chdir(parent_dir + '\Merged_videos')
video_list = os.listdir()
for f in video_list:
next_video_name, img_type = os.path.splitext(f)
cap = cv2.VideoCapture(next_video_name + img_type)
fourcc = cv2.VideoWriter_fourcc('h','2','6','4')
out = cv2.VideoWriter(path + '/' + next_video_name + '_blackbox' + img_type,fourcc, 25.0, (1920,1080))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.rectangle(frame,(1310,40),(1850,100),(0,0,0),cv2.FILLED)
frame = cv2.resize(frame, (1920, 1080), interpolation = cv2.INTER_LINEAR)
out.write(frame)
# cv2.imshow('frame',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
else:
cap.release()
out.release()
cv2.destroyAllWindows()