-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheditor.py
117 lines (95 loc) · 4.7 KB
/
editor.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sys
import argparse
from pathlib import Path
from inspect import cleandoc
from helpers.colors import Color
from helpers.validator import Validate
"""
argument_parser is used to give the end user the option to run commands from the CLI (command line interface)
The argument_parser exists of a parent parser that has all main arguments that can be used in combination with the subparsers (subcommands)
A subparser (subcommand) exists of their own arguments that can be passed, these can be required or optional.
When a command is ran it executes the feature linked to the subcommand.
"""
def argument_parser():
BASE_DIR = Path(__file__).resolve().parent
parent_parser = argparse.ArgumentParser(
prog='Python CLI Video Editor', description='An application to modify vids and more, Made By https://github.com/YassinAO/', add_help=False
)
"""
These commands can be used with any subcommand.
'input' is necessary to manipulate the targeted video(s).
The other ones are optional.
"""
parent_parser.add_argument('-i', '--input')
parent_parser.add_argument('-o', '--output')
parent_parser.add_argument('--overwrite', action='store_true')
parent_parser.add_argument('-b', '--bulk', action='store_true')
parent_parser.add_argument('--fps', type=int)
main_parser = argparse.ArgumentParser()
"""
'dest' is defined to see which subcommand is being used.
This is important because it tells the script which feature to execute.
"""
feature_subparsers = main_parser.add_subparsers(
help='sub-command help', title='actions', dest='command')
"""
Some arguments within the subcommand have 'nargs', 'default', 'const' & 'choices' defined.
- 'nargs' is used to specify the amount of arguments expected. '?' indicates 0 or 1.
- 'default' is used when argument isn't specified.
- 'const' is used when argument is specified but no value given.
- 'choices' are the values the end user can choose from.
'default' or 'const' will be passed as the value depending on the condition.
This is to avoid missing arguments for the executed feature.
"""
"""
A subparser (subcommand) has their own arguments.
The subparser also requires some conditions from the parent_parser,
so we define this in 'parents' to give it access to the parents arguments such as --input --output.
"""
# GIF FEATURE
gif_parser = feature_subparsers.add_parser(
'gif', parents=[parent_parser])
gif_parser.add_argument('-s', '--start', nargs='?',
default='00:00:05', const='00:00:05')
gif_parser.add_argument('-e', '--end', nargs='?',
default='00:00:10', const='00:00:10')
gif_parser.add_argument('-m', '--measure', nargs='?',
default='small', const='small', choices=['small', 'medium', 'large'])
gif_parser.add_argument('--sway', action='store_true')
# WATERMARK FEATURE
watermark_parser = feature_subparsers.add_parser(
'watermark', parents=[parent_parser])
watermark_parser.add_argument('-p', '--position', nargs='?',
default='bottom_right', const='bottom_right', choices=['top_left', 'top_right', 'bottom_left', 'bottom_right'])
watermark_parser.add_argument('-m', '--measure', nargs='?',
default='small', const='small', choices=['small', 'medium', 'large'])
# CUT FEATURE
cut_parser = feature_subparsers.add_parser(
'cut', parents=[parent_parser])
cut_parser.add_argument('-p', '--parts', type=int, nargs='?',
default=2, const=2)
# AUDIO FEATURE
audio_parser = feature_subparsers.add_parser(
'audio', parents=[parent_parser])
audio_parser.add_argument('--export', type=str, nargs='?',
default='.wav', const='.wav', choices=['.wav', '.mp3'])
# SNAPSHOT FEATURE
snapshot_parser = feature_subparsers.add_parser(
'snapshot', parents=[parent_parser])
snapshot_parser.add_argument('--interval', type=int, nargs='?',
default=1, const=1)
args = main_parser.parse_args()
args_dict = vars(args)
if args.input is None:
print(cleandoc(f'''
{Color.WARNING}missing -i OR --input argument{Color.ENDC}'''))
sys.exit()
elif args.command == 'gif' and args.bulk:
print(cleandoc(f'''
{Color.WARNING}gif feature doesn't support bulk manipulation.{Color.ENDC}'''))
sys.exit()
elif args.output is None:
args.output = BASE_DIR.joinpath('output', args.command)
return Validate(**args_dict).check_path()
if __name__ == '__main__':
argument_parser()