aboutsummaryrefslogtreecommitdiff
path: root/devtools/create_toon/subtitles/submaker.py
blob: 177a04e0f1aee31c4194f87396e9fc065561c598 (plain)
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
#!/usr/bin/env python

'''
This script generates subtitles for Toonstrack cutscenes
from .SBV subtitiles.

Usage:
```
submaker.py INFILE.sbv OUTFILE.tss
````

Subtitles format:
<start frame> <end frame> <subtitle text>
'''
import sys

from datetime import datetime

TIME_FORMAT = '%H:%M:%S.%f'
BASETIME = datetime(1900, 1, 1)
FPS = 15

def time2frame(time, fps=FPS):
    return round(fps * (datetime.strptime('0' + time + '000', TIME_FORMAT) - BASETIME).total_seconds())

def sbv2tss(infile, fps=FPS):
    with open(infile, 'r') as sub_file:
        lines = sub_file.read().split('\n\n')

    # ignore empty lines
    lines = [line for line in lines if line]

    for line in lines:
        time_window, text = line.split('\n')[:2]
        start, end = time_window.split(',')
        start = time2frame(start, fps=fps)
        end = time2frame(end[:-1], fps=fps)

        yield '{start} {end} {line}\n'.format(start=start, end=end, line=text)

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('Usage: toon_submaker.py INFILE.sbv OUTFILE.tss')
        exit(1)

    infile = sys.argv[1]
    outfile = sys.argv[2]

    with open(outfile, 'w') as sub_file:
        for line in sbv2tss(infile):
            sub_file.write(line)