Initial commit
This commit is contained in:
55
devtools/create_toon/subtitles/pakdir.py
Normal file
55
devtools/create_toon/subtitles/pakdir.py
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
This script generates subtitles for Toonstrack cutscenes
|
||||
in a single SUBTITLES.PAK file from given directory of .SBV subtitles
|
||||
|
||||
Usage:
|
||||
```
|
||||
pakdir.py SUBTITLES_DIR
|
||||
````
|
||||
"""
|
||||
|
||||
import struct
|
||||
from itertools import chain
|
||||
|
||||
from submaker import sbv2tss
|
||||
|
||||
def write_uint32_le(number):
|
||||
return struct.pack('<I', number)
|
||||
|
||||
def calculate_index_length(pak_index):
|
||||
return sum(len(write_index_entry(fname, 0)) for fname in pak_index)
|
||||
|
||||
def write_index_entry(fname, offset):
|
||||
return write_uint32_le(offset) + fname.encode() + b'\00'
|
||||
|
||||
def generate_index(data_files):
|
||||
end = ('\00\00\00\00', b'')
|
||||
pak_index, rdata = zip(*chain(data_files, (end,)))
|
||||
off = calculate_index_length(pak_index)
|
||||
for fname, fdata in zip(pak_index, rdata):
|
||||
yield write_index_entry(fname, off), fdata
|
||||
off += len(fdata)
|
||||
|
||||
def create_entry(filename):
|
||||
name, _ = os.path.splitext(os.path.basename(filename))
|
||||
return name + '.tss', ''.join(sbv2tss(filename)).encode()
|
||||
|
||||
if __name__ == "__main__":
|
||||
import os
|
||||
import glob
|
||||
import sys
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: pakdir.py SUBTITLES_DIR")
|
||||
exit(1)
|
||||
|
||||
paths = sys.argv[1:]
|
||||
paths = (os.path.join(path, '*.sbv') for path in paths)
|
||||
files = sorted(set(chain.from_iterable(glob.iglob(r) for r in paths)))
|
||||
print(files)
|
||||
index, data = zip(*generate_index(create_entry(filename) for filename in files))
|
||||
with open('SUBTITLES.PAK', 'wb') as output:
|
||||
output.write(b''.join(index))
|
||||
output.write(b''.join(data))
|
||||
51
devtools/create_toon/subtitles/submaker.py
Normal file
51
devtools/create_toon/subtitles/submaker.py
Normal file
@@ -0,0 +1,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)
|
||||
Reference in New Issue
Block a user