Python笔记--02: 多视频帧提取

# coding=utf-8
import os
import cv2

def Extract(video_path, saved_path, extract_frequency):
	video = cv2.VideoCapture()
	if not video.open(video_path):
		print("can't open the video")
		exit(1)
	count = 1
	index = 1
	if not os.path.exists(saved_path):
		os.mkdir(saved_path)
	while True:
		_,frame = video.read()
		if frame is None:
			break
		if count % extract_frequency == 0:
			save_path_i = "{}/{}_{:>03d}.jpg".format(saved_path,video_path.split('/')[-1].split('.')[0],index)
			cv2.imwrite(save_path_i, frame)
			print(">>{} have been saved!......".format(save_path_i))
			index += 1
		count += 1
	video.release()
	print(">>Total frame is {:d} ".format(count))
	print(">>Total saved pictures is {:d}".format(index))
	return index


if __name__ == "__main__":
	import argparse
	'''
	parse = argparse.ArgumentParser(description="Extract frame from your video")
	parse.add_argument("--video",help="the input video file path")
	parse.add_argument("--output",help="the path for saving frame image")
	parse.add_argument("--extract_frequency",help="the extract frequency", default=20)
	args = parse.parse_args()

	Extract(args.video, args.output, args.extract_frequency)
	'''
	root_path = "/home/haix/Desktop/video_mouse"
	docs = ['v1','v2','v5','v6','v7','v11','v12','v13','v14','v15','v16','v17','v18','v19','v20','v21','v22','v23','v24','v25']
	total_saved_images = 0
	print(">>>>Start......\n")

	for i in range(len(docs)):
		video_name = docs[i] + ".mp4"
		doc_path = os.path.join(root_path, docs[i], video_name)
		output_path = os.path.join(root_path, docs[i], 'images')
		print(">>>>Start to process {}......\n".format(video_name))
		frame_count = Extract(doc_path, output_path, 30)
		total_saved_images += frame_count
		print("\n>>>>Total saved images is {:d}\n".format(total_saved_images))
	print(">>All videos have been processed!")

 

你可能感兴趣的:(python,Python,视频处理)