-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_frames.py
68 lines (54 loc) · 1.55 KB
/
extract_frames.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
import sys
import subprocess
import os
from argparse import ArgumentParser
def initiate_extraction():
args = get_args()
validate_args(args)
extract_frames(args)
def get_args():
parser = ArgumentParser()
parser.add_argument(
"--start",
type=str,
dest="start",
required=True
)
parser.add_argument(
"--end",
type=str,
dest="end",
required=True
)
parser.add_argument(
"input",
type=str,
default="."
)
parser.add_argument(
"--fps",
type=str,
default="25"
)
return parser.parse_args()
def validate_args(args):
# Check if the provided arguments have the correct format
time_argument_validation(args.start)
time_argument_validation(args.end)
def time_argument_validation(time_arg):
splitted_arg = time_arg.split(':')
if (len(splitted_arg) != 3):
raise Exception('--start and --end must follow format hh:mm:ss')
for arg in splitted_arg:
if (len(arg) != 2 or int(arg[0]) >= 6):
raise Exception(
'each of --start and --end arguments must be in range [00,59]')
def extract_frames(args):
if not os.path.exists('frames/'):
os.mkdir('frames/')
input_name = args.input.split('/')[-1].split('.')
command = f'ffmpeg -i {args.input} -vf fps={args.fps} -ss {args.start} -to {args.end} ./frames/{input_name[0]}-frame%04d.jpg'
command = command.split(' ')
subprocess.call(command)
if "__name__==__main__":
initiate_extraction()