-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg-chapters-converter.py
executable file
·58 lines (50 loc) · 1.39 KB
/
ffmpeg-chapters-converter.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
#!/usr/bin/python3
import re
chapters = list()
with open('chapters.txt', 'r') as f:
for line in f:
line = line.replace('\ufeff', '')
x = re.match(r"(\S{7}\d{2})=(\d{2}):(\d{2}):(\d{2})\.(\d{3})", line)
if x:
hrs = int(x.group(2))
mins = int(x.group(3))
secs = int(x.group(4))
milisecs = int(x.group(5))
minutes = (hrs * 60) + mins
seconds = secs + (minutes * 60)
timestamp = (seconds * 1000 + milisecs)
chap = {
"startTime": timestamp
}
continue
y = re.match(r"CHAPTER\d{2}NAME=([\S\ ]+)", line)
if y:
# every other line doesn't contain any new information but
# is formatted differently
y = re.match(r"CHAPTER\d{2}NAME=([\S\ ]+)", line)
title = y.group(0)
chap["title"] = title
chapters.append(chap)
text = """;FFMETADATA1
"""
template = """
[CHAPTER]
TIMEBASE=1/1000
START={start}
END={end}
title={title}
"""
for i in range(len(chapters) - 1):
chap = chapters[i]
title = chap['title']
start = chap['startTime']
end = chapters[i + 1]['startTime'] - 1
text += """
[CHAPTER]
TIMEBASE=1/1000
START={start}
END={end}
title={title}
""".format(start=start, end=end, title=title)
with open("chapters.ffmpeg.txt", "a") as myfile:
myfile.write(text)