-
Notifications
You must be signed in to change notification settings - Fork 1
/
trim-vanity
executable file
·132 lines (109 loc) · 3.98 KB
/
trim-vanity
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
import argparse
import os, sys
import glob
import re
sys.path.append("/usr/local/bin/")
from viddin import viddin
FLAGS="--film"
class EpisodePosition:
def __init__(self, title, chapter=None, start=None, end=None):
self.title = title
if isinstance(title, str) and re.match(r"^[0-9]+$", title):
self.title = int(title)
self.chapter = chapter
self.start = start
self.end = end
return
@property
def isFile(self):
if isinstance(self.title, str) and os.path.exists(self.title):
return True
raise ValueError("Not a file", isinstance(self.title, str), os.path.exists(self.title), self.title)
return False
def build_argparser():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("episode", help="episode number as SSxEE (2x05)")
parser.add_argument("titles", nargs="+", help="title numbers on DVD or files")
parser.add_argument("--dvd", help="DVD device")
parser.add_argument("--before", type=float, default=30,
help="Universal vanity card is before")
parser.add_argument("--extension", default="mkv",
help="extension to use for output, default is mkv")
parser.add_argument("--flags", default=FLAGS, help="flags to pass to rip-video")
parser.add_argument("--split-chapters", action="store_true",
help="each chapter is an episode")
return parser
def main():
args = build_argparser().parse_args()
if args.extension[0] != '.':
args.extension = "." + args.extension
m = re.match("([0-9]+)x([0-9]+)", args.episode)
if not m:
print("Invalid episode id:", args.episode)
exit(1)
season = int(m.group(1))
episode = int(m.group(2))
dvd = "/dev/dvd"
if args.dvd:
dvd = args.dvd
to_rip = []
for title in args.titles:
if not args.split_chapters:
to_rip.append(EpisodePosition(title=title))
else:
source = viddin.VideoSpec(dvd, int(title))
chapters = source.chapters + (viddin.Chapter(source.length, None), )
for idx, (chap, chap_next) in enumerate(zip(chapters, chapters[1:])):
to_rip.append(EpisodePosition(title=title, chapter=idx+1,
start=chap.position, end=chap_next.position))
for idx, title in enumerate(to_rip):
epid = "%ix%02i" % (season, episode + idx)
outpath = epid + args.extension
if not title.isFile:
temp = viddin.uniqueFile(outpath)
flags = ["--nosubtitles", "--title", str(title.title)]
if title.chapter:
flags.extend(["--start-at", str(title.start),
"--stop-at", str(title.start + args.before)])
else:
flags.extend(["--stop-at", str(args.before)])
cmd = ["rip-video", *flags, dvd, temp]
print(viddin.listToShell(cmd))
stat = viddin.runCommand(cmd, debugFlag=True)
if stat != 0:
print("Rip failed", file=sys.stderr)
exit(1)
viddin.findBlack(temp)
path, ext = os.path.splitext(temp)
else:
viddin.findBlack(title.title, args.before)
path, ext = os.path.splitext(title.title)
black = viddin.loadSplits(path + ".blk")
for filename in glob.glob(os.path.join(".", path) + ".*"):
filename = os.path.abspath(filename)
if filename == os.path.abspath(title.title):
continue
os.remove(filename)
offset = black[-1][0]
cmd = ["rip-video"]
cmd.extend(args.flags.split())
if title.chapter:
cmd.extend(["--start-at", str(title.start + offset),
"--stop-at", str(title.end)])
else:
cmd.extend(["--start-at", str(offset)])
if not title.isFile:
cmd.extend(["--title", str(title.title), dvd, outpath])
else:
cmd.extend([title.title, outpath])
print(viddin.listToShell(cmd))
stat = viddin.runCommand(cmd, debugFlag=True)
if stat != 0:
print("Rip failed", file=sys.stderr)
exit(1)
cmd = ["eject", dvd]
viddin.runCommand(cmd)
return
if __name__ == '__main__':
exit(main() or 0)