您的位置:首页 > 编程语言 > Python开发

python 批量视频转换成图片

2020-05-30 21:43 381 查看

批量视频转换图片

最近自己在制作视频,需要将大量的视频转换成图片。自己写了段代码。

#coding=utf-8

import os
import cv2

videos_src_path = '/home/zhutianhao/train/'
videos_save_path = '/home/zhutianhao/img/'

videos = os.listdir(videos_src_path)
#videos = filter(lambda x: x.endswith('MP4'), videos)

for each_video in videos:
print('Video Name :', each_video)
# get the name of each video, and make the directory to save frames
each_video_name, _ = each_video.split('.')
os.mkdir(videos_save_path + '/' + each_video_name)

each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/'

# get the full path of each video, which will open the video tp extract frames
each_video_full_path = os.path.join(videos_src_path, each_video)

cap = cv2.VideoCapture(each_video_full_path)
frame_count = 1
success = True
while (success):
success, frame = cap.read()
if success == True:
cv2.imwrite(each_video_save_full_path + "%06d.jpg" % frame_count, frame)

frame_count = frame_count + 1
print('Final frame:', frame_count)

注释

1.

videos_src_path 代表输入路径,里面包含大量的视频
videos_save_path 代表存储的文件夹

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: