-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.py
98 lines (88 loc) · 3.54 KB
/
screenshot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import glob, sys, re, os
from argparse import ArgumentParser
import cv2
import numpy
def parse_args():
"""
Parse command line arguments and format arguments containing paths.
:return: tuple of (ArgumentParser, Namespace). Parser itself and all arguments.
"""
parser = ArgumentParser(description='Convert a single movie OR '
'Enter a folder path to convert all movie folders in it ')
parser.add_argument('fileLocation', help='Complete FileDirectory/Folder or a single filename(Enclose it inside quotations " ")',type=str)
parser.add_argument('-d','--dest',help='File location for output files (Enclose it inside quotations " "), Default location is the video location',type=str)
args = parser.parse_args()
if isinstance(args.fileLocation, str):
args.fileLocation = os.path.normpath(args.fileLocation.strip())
return parser, args
def check_for_errors(filesource):
"""
Check for errors, return corresponding
error statement if any errors occurred.
Otherwise return None.
:param filename: str. file path.
:param folder: str. Folder path containing moives.
:return: str or NoneType. Error statement or None.
"""
message = None
if filesource != None:
if not os.path.exists(filesource):
message = 'Error: path does not exist.'
elif filesource is None:
message = 'Error: You must provide either filepathname or folder path.'
else:
message = None
return message
def estimate_blur(image: numpy.array, threshold: int = 100):
if image.ndim == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur_map = cv2.Laplacian(image, cv2.CV_64F)
score = numpy.var(blur_map)
return score, bool(score < threshold)
def singleFile(filename,screenshot_no,threshold=50.0):
"""
Reads a single video file, extracts given no of screenshots from it and checks whether its blurry or not , then outputs it into jpg.
:param filename: str, File path and filename
screenshot_no: int, no of screenshots to take
:return: None
"""
output = os.path.dirname(filename)
cap = cv2.VideoCapture(filename)
cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1)
totalmsecs = cap.get(cv2.CAP_PROP_POS_MSEC)
per = 90 // screenshot_no
inc = per
for j, i in enumerate(range(screenshot_no), start=1):
cur_msec = totalmsecs * (per / 100)
cap.set(cv2.CAP_PROP_POS_MSEC, cur_msec)
success, image = cap.read()
scor, boo = estimate_blur(image, threshold)
if boo:
while boo:
cur_msec += 10000
cap.set(cv2.CAP_PROP_POS_MSEC, cur_msec)
success, image = cap.read()
scor, boo = estimate_blur(image, threshold)
outFile = os.path.join(output, f"frames{j}.jpg")
cv2.imwrite(outFile, image)
per += inc
cap.release()
def convert_folder(folder):
files = []
for ext in ('*.mp4'):
files.extend(glob.glob(os.path.join(folder, ext)))
for f in files:
read_convert_singleFile(f,location)
if __name__ == '__main__':
parser, args = parse_args()
src = args.fileLocation
to_folder = select_destination(args)
# checking for errors
error = check_for_errors(src)
if error:
sys.exit(error)
if os.path.isfile(src):
singleFile(src,to_folder)
elif os.path.isdir(src):
convert_folder(src,to_folder)
print("Conversion Completed!!")