import cv2
import os
import time
class VideoFileIO(object):
'''
NAME
The Class of Video'IO
PACKAGE CONTENTS
extract_frames: Video extract picture
'''
def __init__(self):
pass
def extract_frames(self, file_path:str, extract_fps:int=None, save_path:str=None):
self.cap = cv2.VideoCapture(file_path)
out_basename = os.path.splitext(os.path.basename(file_path))[0]
# assert self.cap.isOpened()
if not self.cap.isOpened():
print("can not open the video!")
exit(1) # 等价于 return 1 异常
pass
save_path = save_path if save_path is not None else out_basename
os.makedirs(save_path) if not os.path.exists(save_path) else None
if extract_fps is None:
extract_fps = int(self.cap.get(cv2.CAP_PROP_FPS))
pass
fourcc = int(self.cap.get(cv2.CAP_PROP_FOURCC))
frams = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
count = 0
while self.cap.isOpened():
ret, frame = self.cap.read()
if frame is None:
print("extract frames is finish!")
break
if count % extract_fps == 0:
cv2.imwrite(os.path.join(save_path, out_basename + '_' + str(count).zfill(len(str(frams))) +'.jpg'), frame)
pass
count+=1
pass
self.cap.release()
return fourcc, frams
def synthetic_video(self, file_dir:str, save_dir:str=r'.', size:tuple=(1920, 1080), fps:int=30, max_save_frames:int=100):
if not os.path.isdir(file_dir):
print("file_dir is not a dir!")
exit(1)
pass
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pass
file_list = os.listdir(file_dir)
assert len(file_list)!=0, "None file"
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
cap = cv2.VideoWriter(os.path.join(save_dir, "res_"+os.path.basename(file_dir)+'.mp4'), fourcc, fps, size)
assert cap.isOpened()
for i in range(0, min(len(file_list), len(file_list))):
frame_path = os.path.join(file_dir, file_list[i])
if os.path.isfile(frame_path):
frame = cv2.imread(frame_path)
frame = cv2.resize(frame, size)
cap.write(frame)
pass
pass
cap.release()
pass
def record_video(self, save_dir:str=r'.', size:tuple=(1280, 1024), fps:int=25):
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
if not cap.isOpened():
print("can not open the video!")
exit(1) # 等价于 return 1 异常
pass
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
pass
runtime = time.localtime()
runtime = [runtime.tm_year, runtime.tm_mon, runtime.tm_mday, runtime.tm_hour, runtime.tm_min, runtime.tm_sec]
file_name = '_'.join('%s' %id for id in runtime) + '.mp4'
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
vw = cv2.VideoWriter(os.path.join(save_dir, file_name), fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if ret:
vw.write(frame)
cv2.imshow('capture', frame)
if cv2.waitKey(10) & 0xFF == ord('q'): # 按键盘Q键退出
break
else:
continue
cv2.destroyAllWindows()
cap.release()
vw.release()
pass
pass
cvid = VideoFileIO()
# cvid.extract_frames(file_path=r'VID_20220702_142227.mp4', extract_fps=1)
# cvid.synthetic_video(file_dir=r'visualized_test_results')
cvid.record_video()
python cv2 封装一个视频IO_todo
于 2022-07-02 23:07:29 首次发布