/
zero-bd-chapters.py
92 lines (82 loc) · 3.54 KB
/
zero-bd-chapters.py
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
'''
Split a multi-episode Blu-ray chapters file,
reset the first timestamp in each resulting
file to zero, adjust the rest accordingly.
'''
import sys, argparse
from util import bdTimestampToSeconds, secondsToBdTimestamp
def main():
parser = argparse.ArgumentParser(description='Split a multi-episode '
'Blu-ray chapter timestamps file to multiple chapter files, '
'adjusted for time.')
parser.add_argument('-f', '--file', metavar='<path>',
help='Input file', required=True)
parser.add_argument('-s', '--start', metavar='<num>', type=int,
help='Start offset -- e.g. 3 will start at the 3rd timestamp',
required=False, default=0)
parser.add_argument('-e', '--end', metavar='<num>', type=int,
help='End offset -- e.g. 2 will cut off trailing chapters starting '
'from the 2nd to last timestamp',
required=False, default=0)
parser.add_argument('-c', '--chaps', metavar='<num>', type=int,
help='Number of chapters per episode', required=True)
args = parser.parse_args()
total_chap = args.chaps
in_fname = args.file
chapter_set = []
all_sets = []
offset_ctr = 0
start_offset = args.start
end_offset = args.end
try:
with open(in_fname) as inFile:
for line in inFile:
if offset_ctr < start_offset:
offset_ctr += 1
continue
chapter_set.append(line)
if len(chapter_set) == total_chap:
all_sets.append(chapter_set)
chapter_set = []
else:
if end_offset != 0:
if len(chapter_set) > end_offset:
chapter_set = chapter_set[0:len(
chapter_set) - end_offset]
else:
overlap = end_offset - len(chapter_set)
remove_chaps = overlap % total_chap
remove_sets = overlap / total_chap
if remove_sets > 0:
all_sets = all_sets[0:-remove_sets]
if remove_chaps > 0:
chapter_set = all_sets[-1][0:-remove_chaps]
all_sets = all_sets[0:-1]
if len(all_sets) == 0 and len(chapter_set) == 0:
print 'No chapters found. Exiting...'
if len(all_sets) > 0:
print '%d set(s) of %d chapter(s) found.' % (len(all_sets),
total_chap)
if len(chapter_set) != 0 and len(chapter_set) < total_chap:
if len(all_sets) == 0:
print 'Not enough chapters to make a set of %d. Exiting...' % (
total_chap)
exit()
else:
print '%d chapter(s) left over.' % (len (chapter_set))
for chapter_set in all_sets:
first_chapter = bdTimestampToSeconds(chapter_set[0])
part = all_sets.index(chapter_set) + 1
out_fname = in_fname.split('.')[0] + '_out_' + str(part) + '.txt'
with open(out_fname,'w') as outFile:
for chapter in chapter_set:
chapter_time = bdTimestampToSeconds(chapter)
adjusted_time = secondsToBdTimestamp(chapter_time -
first_chapter)
outFile.write(adjusted_time+'\n')
print 'Chapters retimed and written to ' + out_fname
except IOError:
print 'Please enter a valid filename!'
exit()
if __name__ == "__main__":
main()