目的:
视频 FPS=30,需要把视频裁剪成 FPS=3 的图像集,即两分钟的视频裁剪出360张图像。
FPS:
- 每秒传输帧数(Frames Per Second)
- FPS 也可以理解为我们常说的“刷新率(单位为Hz)”,例如我们常在游戏里说的“FPS值”。
- FPS 是图像领域中的定义,是指画面每秒传输帧数,通俗来讲就是指动画或视频的画面数。FPS 是测量用于保存、显示动态视频的信息数量。每秒钟帧数越多,所显示的动作就会越流畅。通常,要避免动作不流畅的最低是30。某些计算机视频格式,每秒只能提供15帧。
示例代码(含注释):
import cv2
from tqdm import tqdm # 进度条def main():# 读取视频文件video_caputre = cv2.VideoCapture(r"G:\CSDN\python\videos2images\videos\IMG_3389.MP4")# 获取视频流的参数fps = video_caputre.get(cv2.CAP_PROP_FPS) # 帧率width = video_caputre.get(cv2.CAP_PROP_FRAME_WIDTH) # 宽度height = video_caputre.get(cv2.CAP_PROP_FRAME_HEIGHT) # 高度all_frames = video_caputre.get(cv2.CAP_PROP_FRAME_COUNT) # 总帧数print("fps:", fps, "\n", "width:", width, "\n", "height:", height, "\n", "all_frames:", all_frames)# 读取视频流(返回参数:是否读到视频流、图像帧)whether, frame = video_caputre.read()i = 0for _ in tqdm(range(int(all_frames))):# cv2.imshow("image", frame)# cv2.waitKey(0)i += 1# 取余数(每10帧保存一张图像)if i % 10 == 0:cv2.imwrite(r"G:\CSDN\python\videos2images\save_image_3_person\4_{}.png".format(i), frame)# 读取下一帧图像whether, frame = video_caputre.read()# 释放视频流video_caputre.release()print("video to images done!")if __name__ == "__main__":main()
>>>output
fps: 29.996961074122165
width: 1920.0
height: 1080.0
all_frames: 5429.0
100%|██████████| 5429/5429 [02:27<00:00, 36.79it/s]
video to images done!
>>>如有疑问,欢迎评论区一起探讨