用OPenCV分割视频
使用OPencv将分割视频分割成帧
首先安装opencv库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
使用以下代码,将视频路径和帧图像保存路径换成自己的文件地址:
import cv2
def video2frame(videos_path, frames_save_path, time_interval):
'''
:param videos_path: 视频的存放路径
:param frames_save_path: 视频切分成帧之后图片的保存路径
:param time_interval: 保存间隔
:return:
'''
vidcap = cv2.VideoCapture(videos_path)
success, image = vidcap.read()
count = 0
while success:
success, image = vidcap.read()
count += 1
if count % time_interval == 0:
cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count)
# if count == 20:
# break
print(count)
if __name__ == '__main__':
videos_path = r''
frames_save_path = r''
time_interval = 2 # 隔一帧保存一次
video2frame(videos_path, frames_save_path, time_interval)