-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkv-renamer
executable file
·66 lines (54 loc) · 2.52 KB
/
mkv-renamer
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
#!/usr/bin/env python2.7
import os
import argparse
description = "Rename the output from MakeMKV. (C) 2015 Andrew Scagnelli"
add_help = "The input prefix to change (eg title in title00.mkv)"
output_help = "The output prefix"
offset_help = "The offset between the input and output files (the first value to be output)."
start_help = "The value to start counting from."
dir_help = "The directory to loop through."
extension_help = "File extension to monitor."
epilog = "(C) 2015 Andrew Scagnelli. Available under the GNU GPL."
def main():
parser = argparse.ArgumentParser(description=description,
epilog=epilog)
parser.add_argument('--output', required=True, help=output_help)
parser.add_argument('--prefix', required=False, default="title",
type=str, help=add_help)
parser.add_argument('--offset', required=False, default=1,
type=int, help=offset_help)
parser.add_argument('--start', required=False, default="0",
type=int, help=start_help)
parser.add_argument('--dir', required=False, default=".", help=dir_help)
parser.add_argument('--extension', required=False, default="mkv",
help=extension_help)
args = parser.parse_args()
count = args.start
for file in os.listdir(args.dir):
infile = args.prefix + str(count).zfill(2) + "." + args.extension
offset = count + int(args.offset)
output = args.output + str(offset).zfill(2) + "." + args.extension
if (os.path.isfile(output)):
print("Output file " + output + " already exists, cancelling run")
exit()
elif (not os.path.isfile(infile)):
print("Input file " + infile + " does not exist, cancelling run")
exit()
for file in os.listdir(args.dir):
if args.prefix not in file:
continue
infile = args.prefix + str(count).zfill(2) + "." + args.extension
offset = count + args.offset
output = args.output + str(offset).zfill(2) + "." + args.extension
print "Renaming " + infile + " to " + output
if (os.path.isfile(output)):
print("Output file " + output + " already exists, aborting")
exit()
elif (not os.path.isfile(infile)):
print("Input file " + infile + " does not exist, aborting")
exit()
else:
os.rename(args.dir + "/" + infile, args.dir + "/" + output)
count = count + 1
if __name__ == "__main__":
main()