aboutsummaryrefslogtreecommitdiff
path: root/devtools
diff options
context:
space:
mode:
authorBLooperZ2019-11-08 01:34:35 +0200
committerEugene Sandulenko2019-11-18 02:02:14 +0100
commitd30ee1df37c2b0f38768ba452ca1ffae9a3ea628 (patch)
treed7aeeb981df83da23af19789de31e998a0b2ef97 /devtools
parent47462e9810b19809688a2b26fb3a4043da20a802 (diff)
downloadscummvm-rg350-d30ee1df37c2b0f38768ba452ca1ffae9a3ea628.tar.gz
scummvm-rg350-d30ee1df37c2b0f38768ba452ca1ffae9a3ea628.tar.bz2
scummvm-rg350-d30ee1df37c2b0f38768ba452ca1ffae9a3ea628.zip
TOON: add create subtitles tools
Diffstat (limited to 'devtools')
-rw-r--r--devtools/create_toon/subtitles/pakdir.py56
-rw-r--r--devtools/create_toon/subtitles/submaker.py52
2 files changed, 108 insertions, 0 deletions
diff --git a/devtools/create_toon/subtitles/pakdir.py b/devtools/create_toon/subtitles/pakdir.py
new file mode 100644
index 0000000000..7e3ad9411b
--- /dev/null
+++ b/devtools/create_toon/subtitles/pakdir.py
@@ -0,0 +1,56 @@
+#!/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, ext = os.path.splitext(os.path.basename(filename))
+ assert ext.lower() == '.sbv', ext.lower()
+ 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))
diff --git a/devtools/create_toon/subtitles/submaker.py b/devtools/create_toon/subtitles/submaker.py
new file mode 100644
index 0000000000..40a9f9b1c7
--- /dev/null
+++ b/devtools/create_toon/subtitles/submaker.py
@@ -0,0 +1,52 @@
+#!/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 io
+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)