mp4转图片
import cv2
from moviepy.editor import VideoFileClip
import numpy as npdef read_video_frames(video_path):clip = VideoFileClip(video_path)frames = []frame_count = 24for t in range(int(clip.duration * clip.fps)):print(t)frame = clip.get_frame(t / clip.fps)frames.append(frame)if len(frames) >= frame_count:breakclip.close()return np.array(frames)video_path = r"E:\data\pose\1114\AnimateDiff_00043.mp4"
frames_array = read_video_frames(video_path)
print(frames_array.shape)for frame in frames_array:frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)cv2.imshow('frame', frame)cv2.waitKey(0)
opencv转图片:
frames = []max_num_frames = 24cap = cv2.VideoCapture(video_path)frames=[]while True:ret_val, img0 = cap.read() #if img0 is not None:img0=cv2.resize(img0,(WIDTH, HEIGHT))img0=cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)frames.append(img0)if len(frames)==max_num_frames:break