-
Notifications
You must be signed in to change notification settings - Fork 9
/
ffmpeg-srt-scene
executable file
·56 lines (43 loc) · 1.38 KB
/
ffmpeg-srt-scene
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
#!/usr/bin/env python3
# ffmpeg-srt-scene: Given a scene index, render that scene with the subtitles baked in.
import argparse
import math
import pathlib
import subprocess
import srt
def timestamp(delta):
(hours, rem) = divmod(delta.total_seconds(), 3600)
(minutes, seconds) = divmod(rem, 60)
(millis, seconds) = math.modf(seconds)
return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}.{int(millis * 1000):03d}"
def main():
parser = argparse.ArgumentParser(
description="Render a scene with baked-in subtitles, using its SRT index"
)
parser.add_argument("input", type=pathlib.Path, help="The input video")
parser.add_argument(
"srt", type=pathlib.Path, help="The SRT file to index from",
)
parser.add_argument("index", type=int, help="The scene index")
parser.add_argument("output", type=pathlib.Path, help="The output")
options = parser.parse_args()
sub = list(srt.parse(options.srt.read_text()))[options.index - 1]
start_ts = timestamp(sub.start)
end_ts = timestamp(sub.end)
subprocess.run(
[
"ffmpeg",
"-i",
options.input,
"-vf",
f"subtitles={options.srt}",
"-copyts",
"-ss",
start_ts,
"-to",
end_ts,
options.output,
]
)
if __name__ == "__main__":
main()