/
add-skip-intro
executable file
·120 lines (101 loc) · 3.74 KB
/
add-skip-intro
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
#!/usr/bin/env python3
# -*- python -*-
#
# Copyright 2018 by Chris Osborn <fozztexx@fozztexx.com>
#
# This file is part of viddin.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
import argparse
import os, sys
import re
import viddin
def build_argparser():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("chapter", help="chapter number of intro")
parser.add_argument("length", type=float, help="length of intro")
parser.add_argument("files", nargs="+", help="Video file to guess")
parser.add_argument("-f", "--force", action="store_true",
help="Force skip intro even if there's no matching silence")
parser.add_argument("-c", "--cuts", action="store_true",
help="Use scene cuts if there's no black")
parser.add_argument("-n", "--names", nargs=2, help="use names as chapter names")
return parser
def main():
args = build_argparser().parse_args()
names = ["Intro", "Skip Intro"]
if args.names:
names = args.names
for path in args.files:
print(path)
video, ext = os.path.splitext(path)
vfile = viddin.Media(path, None)
chapters = vfile.chapters
_, c1 = vfile.chapterWithID(names[0])
_, c2 = vfile.chapterWithID(names[1])
if c1 and c2:
continue
_, chap = vfile.chapterWithID(args.chapter)
if chap is None:
print("Unable to find specified chapter", path)
continue
intro = [chap]
offset = intro[0].position + args.length
if offset >= -2:
for chap in chapters:
if chap.position > offset + 2:
break
diff = abs(offset - abs(chap.position))
if diff < 2:
intro.append(viddin.Chapter(abs(chap.position), "Skip Intro"))
if len(intro) != 2:
viddin.findBlack(path)
viddin.findSilence(path)
black = viddin.loadSplits(video + ".blk")
silence = viddin.loadSplits(video + ".sil")
splits = []
for row in black:
match = viddin.bestSilence(row, silence)
if match:
splits.append((match[1] - match[0]) / 2 + match[0])
else:
splits.append(0 - ((row[1] - row[0]) / 2 + row[0]))
for row in splits:
if abs(row) > offset + 2:
break
if row >= 0 or args.force:
diff = abs(offset - abs(row))
if diff < 2:
intro.append(viddin.Chapter(abs(row), "Skip Intro"))
break
if args.cuts and len(intro) != 2:
viddin.findCuts(path)
cuts = viddin.loadSplits(video + ".cut")
cut_limit = [abs(offset - x[0]) for x in cuts if x[0] < offset + 2]
if len(cut_limit):
cut_best = min(cut_limit)
cut_idx = cut_limit.index(cut_best)
if cut_best < 0.75:
intro.append(viddin.Chapter(cuts[cut_idx][0], "Skip Intro"))
if len(intro) != 2:
print("Unable to find intro", path)
continue
intro.sort()
if intro[0].name != names[0]:
intro[0] = viddin.Chapter(intro[0].position, names[0])
if intro[1].name != names[1]:
intro[1] = viddin.Chapter(intro[1].position, names[1])
if vfile.addChapters(intro, normalizeFlag=True):
vfile.writeChapters()
return
if __name__ == '__main__':
exit(main() or 0)