-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit-chapters
executable file
·131 lines (114 loc) · 4.09 KB
/
edit-chapters
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
#!/usr/bin/env python3
# -*- python -*-
#
# Copyright 2018 by Chris Osborn <[email protected]>
#
# 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 os
import argparse
import datetime
import sys
import re
from dataclasses import dataclass
from enum import Enum, auto
import viddin
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument("file", help="Video file to examine")
parser.add_argument("--fix", action="store_true", help="Fix first chapeter marker to start at exactly 0")
parser.add_argument("--multiplier", type=float, help="Multiplier to shift chapter markers")
parser.add_argument("--chapters", help="File containing list of chapters to replace existing markers")
parser.add_argument("--add", nargs="+", help="Positions to add as chapters")
parser.add_argument("--delete", nargs="+", help="Chapters to delete")
parser.add_argument("--rename", nargs="+", help="Chapters to rename")
parser.add_argument("--debug", action="store_true", help="print debug info")
return parser
class Operation(Enum):
DELETE = auto()
ADD = auto()
RENAME = auto()
@dataclass
class Change:
chap: viddin.Chapter
op: Operation
def main():
args = build_argparser().parse_args()
if not os.path.exists(args.file):
print("No such file", args.file, file=sys.stderr)
exit(1)
video, ext = os.path.splitext(args.file)
vfile = viddin.Media(args.file)
if args.chapters:
chapters = viddin.loadChapterFile(args.chapters)
vfile.setChapters(chapters)
changes = []
if args.add is not None:
for pos in args.add:
cidx = pos.find("=")
cname = None
if cidx > 0:
cname = pos[cidx+1:]
pos = pos[:cidx]
pos = viddin.decodeTimecode(pos)
changes.append(Change(chap=viddin.Chapter(pos, cname), op=Operation.ADD))
if args.delete is not None:
for pos in args.delete:
if isinstance(pos, str):
if re.match("^[0-9]+$", pos):
pos = int(pos)
elif re.match("[:,.]", pos):
pos = viddin.decodeTimecode(pos)
idx, chap = vfile.chapterWithID(pos)
if idx is None:
print("Invalid chapter index: \"%s\"" % (chp), file=sys.stderr)
exit(1)
changes.append(Change(chap=chap, op=Operation.DELETE))
if args.rename is not None:
for chp in args.rename:
cidx = chp.find("=")
if cidx < 0:
print("Unknown chapter format: \"%s\"" % (chp), file=sys.stderr)
exit(1)
idx, chap = vfile.chapterWithID(chp[:cidx])
if idx is None:
print("Invalid chapter index: \"%s\"" % (chp), file=sys.stderr)
exit(1)
changes.append(Change(viddin.Chapter(position=chap.position, name=chp[1+cidx:]),
op=Operation.RENAME))
doEdit = 0
if changes:
change = [x.chap for x in changes if x.op == Operation.DELETE]
if change and vfile.deleteChapters(change):
doEdit += 1
change = [x.chap for x in changes if (x.op == Operation.ADD or x.op == Operation.RENAME)]
if change:
vfile.addChapters(change)
doEdit += 1
if args.multiplier:
chapters = [viddin.Chapter(x.position * args.multiplier, x.name) for x in vfile.chapters]
vfile.setChapters(chapters)
doEdit += 1
if args.fix and vfile.normalizeChapters():
doEdit += 1
if doEdit:
vfile.writeChapters()
else:
if len(vfile.chapters) == 0:
print("No chapter markers")
for idx, chap in enumerate(vfile.chapters):
print("%i:%s\t%s\t%0.3f" % (idx, chap.name, viddin.formatChapter(chap.position),
chap.position))
return
if __name__ == '__main__':
exit(main() or 0)