forked from FozzTexx/viddin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit-chapters
executable file
·109 lines (95 loc) · 3.4 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
#!/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 viddin 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
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.VideoSpec(args.file)
if args.chapters:
chapters = viddin.loadChapterFile(args.chapters)
vfile.setChapters(chapters)
doEdit = False
if not args.add is None:
change = []
for pos in args.add:
cidx = pos.find("=")
cname = None
if cidx > 0:
cname = pos[cidx+1:]
pos = pos[:cidx]
pos = viddin.decodeTimecode(pos)
change.append(viddin.Chapter(pos, cname))
vfile.addChapters(change)
doEdit = True
if not args.delete is None:
change = []
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)
change.append(pos)
doEdit = vfile.deleteChapters(change)
if not args.rename is 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)
vfile.addChapters([viddin.Chapter(chap.position, chp[cidx+1:])])
doEdit = True
if args.multiplier:
chapters = [viddin.Chapter(x.position * args.multipler, x.name) for x in vfile.chapters]
vfile.setChapters(chapters)
doEdit = True
if args.fix:
doEdit = vfile.normalizeChapters()
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" % (idx, chap.name, viddin.formatChapter(chap.position)))
return
if __name__ == '__main__':
exit(main() or 0)