import os
import cv2 as cv
class VideoProcess(object):
def __init__(self, fps=25, output_path="./output_path/"):
self.fps = fps
self.output_path = output_path
# if not os.path.exists(self.output_path):
# os.mkdir(self.output_path)
def video2frames(self, video_path):
video_capture = cv.VideoCapture(video_path) # 读取视频文件
count = 0
while True:
ret, frame = video_capture.read() # 按帧读取视频,
if not ret: # 判断视频解析是否结束
break
img_name = self.output_path + f"{count+1:08d}" + ".jpg"
cv.imwrite(img_name, frame)
count += 1
print(f"已经存储{count+1:08d}张图片了...")
# print("已经存储{%6d}张图片了...".format(count))
print("convert successfully")
def frames2video(self, image_dir, video_width, video_height):
# video_dir = os.path.join(self.output_path)
fourcc = cv.VideoWriter_fourcc('M', 'J', 'P', 'G')
frame_size = (video_width, video_height)
videowriter = cv.VideoWriter(self.output_path, fourcc, self.fps, frame_size)
frames = sorted(os.listdir(image_dir))
for frame in frames:
f_path = os.path.join(image_dir, frame)
image = cv.imread(f_path)
image = cv.resize(src=image, dsize=(video_width, video_height))
videowriter.write(image)
print(frame + " has been written!")
videowriter.release()
def demo_frame2video():
video_path = "./VideoFrame/video-3.avi"
frame2video_converter = VideoProcess(output_path=video_path)
image_dir = "./vidao_pics/"
video_width = 1500
video_height = 1000
frame2video_converter.frames2video(image_dir, video_width, video_height)
def demo_video2frame():
frame_path = "./frames-1/"
video2frame_converter = VideoProcess(output_path=frame_path)
video_path = "./1.ts"
video2frame_converter.video2frames(video_path)
if __name__ == "__main__":
demo_frame2video()
demo_video2frame()
视频转图片&图片生成视频
最新推荐文章于 2024-05-10 10:27:25 发布
884





