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

python將視頻轉為圖片

編輯:Python
#coding=utf-8
import os
import cv2
# mp4存放的路徑,路徑下只有mp4
videos_src_path = r'D:\python_tool\tools/video/'
# 保存的路徑,會在路徑下創建mp4文件名的文件夾保存圖片
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_count = 1
# 隔著多少幀取一張
frame_rate=30
success = True
# 計數
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