importcv2importimageiodefvideo_to_gif(video_path,gif_path,start_sec0,end_secNone,target_durationNone,gif_fps10,resize1.0): 将视频转换为GIF Args: video_path: 输入视频路径 gif_path: 输出GIF路径 start_sec: 开始秒数 end_sec: 结束秒数 target_duration: GIF目标时长秒None则保持原速 gif_fps: GIF帧率 resize: 缩放比例 capcv2.VideoCapture(video_path)ifnotcap.isOpened():print(f无法打开视频:{video_path})returnvideo_fpscap.get(cv2.CAP_PROP_FPS)total_framesint(cap.get(cv2.CAP_PROP_FRAME_COUNT))start_frameint(start_sec*video_fps)end_frameint(end_sec*video_fps)ifend_secelsetotal_frames cap.set(cv2.CAP_PROP_POS_FRAMES,start_frame)# 计算需要的总帧数和跳帧间隔video_duration(end_frame-start_frame)/video_fpsiftarget_duration:# 计算需要多少帧来达到目标时长total_gif_framesint(target_duration*gif_fps)# 计算视频中需要跳多少帧frame_step(end_frame-start_frame)/total_gif_frameselse:frame_step1frames[]print(f原视频时长:{video_duration:.1f}秒目标GIF时长:{target_durationiftarget_durationelsevideo_duration:.1f}秒)current_posstart_framewhilecurrent_posend_frame:cap.set(cv2.CAP_PROP_POS_FRAMES,int(current_pos))ret,framecap.read()ifnotret:break# BGR转RGBframe_rgbcv2.cvtColor(frame,cv2.COLOR_BGR2RGB)# 缩放ifresize!1.0:h,wframe_rgb.shape[:2]new_w,new_hint(w*resize),int(h*resize)frame_rgbcv2.resize(frame_rgb,(new_w,new_h))frames.append(frame_rgb)current_posframe_step cap.release()# 保存GIFprint(f正在保存GIF共{len(frames)}帧...)imageio.mimsave(gif_path,frames,fpsgif_fps,loop0)print(fGIF已保存:{gif_path})# 使用示例 - 16秒视频压缩到8秒if__name____main__:video_to_gif(video_pathvideo(2).mp4,gif_pathoutput.gif,start_sec0,end_sec16,# 原视频16秒target_duration8,# 压缩到8秒播放完gif_fps10,resize0.5)
视频转gif
importcv2importimageiodefvideo_to_gif(video_path,gif_path,start_sec0,end_secNone,target_durationNone,gif_fps10,resize1.0): 将视频转换为GIF Args: video_path: 输入视频路径 gif_path: 输出GIF路径 start_sec: 开始秒数 end_sec: 结束秒数 target_duration: GIF目标时长秒None则保持原速 gif_fps: GIF帧率 resize: 缩放比例 capcv2.VideoCapture(video_path)ifnotcap.isOpened():print(f无法打开视频:{video_path})returnvideo_fpscap.get(cv2.CAP_PROP_FPS)total_framesint(cap.get(cv2.CAP_PROP_FRAME_COUNT))start_frameint(start_sec*video_fps)end_frameint(end_sec*video_fps)ifend_secelsetotal_frames cap.set(cv2.CAP_PROP_POS_FRAMES,start_frame)# 计算需要的总帧数和跳帧间隔video_duration(end_frame-start_frame)/video_fpsiftarget_duration:# 计算需要多少帧来达到目标时长total_gif_framesint(target_duration*gif_fps)# 计算视频中需要跳多少帧frame_step(end_frame-start_frame)/total_gif_frameselse:frame_step1frames[]print(f原视频时长:{video_duration:.1f}秒目标GIF时长:{target_durationiftarget_durationelsevideo_duration:.1f}秒)current_posstart_framewhilecurrent_posend_frame:cap.set(cv2.CAP_PROP_POS_FRAMES,int(current_pos))ret,framecap.read()ifnotret:break# BGR转RGBframe_rgbcv2.cvtColor(frame,cv2.COLOR_BGR2RGB)# 缩放ifresize!1.0:h,wframe_rgb.shape[:2]new_w,new_hint(w*resize),int(h*resize)frame_rgbcv2.resize(frame_rgb,(new_w,new_h))frames.append(frame_rgb)current_posframe_step cap.release()# 保存GIFprint(f正在保存GIF共{len(frames)}帧...)imageio.mimsave(gif_path,frames,fpsgif_fps,loop0)print(fGIF已保存:{gif_path})# 使用示例 - 16秒视频压缩到8秒if__name____main__:video_to_gif(video_pathvideo(2).mp4,gif_pathoutput.gif,start_sec0,end_sec16,# 原视频16秒target_duration8,# 压缩到8秒播放完gif_fps10,resize0.5)