-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg-chapter-generator.py
52 lines (40 loc) · 1.48 KB
/
ffmpeg-chapter-generator.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
import argparse
def process_line(line):
time_str, chapter_name = line.strip().split("\t")
hh, mm, ss = map(int, time_str.split(":"))
line_ms = (hh * 3600 + mm * 60 + ss) * 1000
return line_ms, chapter_name
def convert_chapters(input_file, output_file):
with open(input_file, "r") as f:
lines = f.readlines()
output = []
output.append(";FFMETADATA1\n")
timebase = 1000 # Timebase in milliseconds
for index, line in enumerate(lines):
start_ms, chapter_name = process_line(line)
try:
next_start_ms, _ = process_line(lines[index + 1])
except IndexError:
next_start_ms = ""
print("START", start_ms)
print("END", next_start_ms)
print("--")
chapter_info = (
f"[CHAPTER]\n"
f"TIMEBASE=1/{timebase}\n"
f"START={start_ms}\n"
f"END={next_start_ms}\n"
f"title={chapter_name.strip()}\n"
)
output.append(chapter_info)
with open(output_file, "w") as f:
f.write("\n".join(output))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert chapters file to FFmpeg metadata format."
)
parser.add_argument("input_file", help="Path to the input chapters file.")
parser.add_argument("output_file", help="Path to the output FFmpeg metadata file.")
args = parser.parse_args()
convert_chapters(args.input_file, args.output_file)
print("Conversion complete.")