程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python convert video to picture

編輯:Python
#coding=utf-8
import os
import cv2
# mp4 Storage path , Only under the path mp4
videos_src_path = r'D:\python_tool\tools/video/'
# Saved path , Will be created under the path mp4 File name folder to save pictures 
videos_save_path = r'D:\python_tool\tools/video'
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 number 
frame_count = 1
# How many frames to take one 
frame_rate=30
success = True
# Count 
num=0
while (success):
success, frame = cap.read()
if success == True:
if frame_count%frame_rate==0:
cv2.imwrite(each_video_save_full_path + each_video_name+"%06d.jpg" % num, frame)
num+=1
frame_count = frame_count + 1
print('Final frame:', num)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved