字幕量産ツールtxt2exo.pyへの入力用の字幕テキストに対して日本語用と英語用で異なるフォントを使い分けるためのタグを挿入するためのPython3のスクリプト。
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# 事前準備: | |
# 正規表現を用いてひらがな、カタカナ及び漢字並びにギリシャ文字とそれ以外の文字を | |
# 識別するために、regexパッケージを使用していますので、 | |
# https://pypi.org/project/regex/ | |
# からインストールする必要があります。 | |
# 使用法: タグを挿入したファイルは標準出力から出力されます。 | |
# python3 txt2exo_multifont.py [-t <半角文字に対するフォント等の設定>] [-m <全角文字に対するフォントの設定>] -f <字幕ファイル> | |
# | |
import io | |
import sys | |
import regex | |
import codecs | |
from struct import * | |
from binascii import hexlify | |
import argparse | |
# 標準出力の文字コードを変更する。 | |
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='UTF-8') | |
parser = argparse.ArgumentParser(description='Command line options of txt2exo_multifont.py') | |
parser.add_argument('-t','--text-config',type=str,default='s50,Futura Lt BT',help='Text configurations for 1-byte characters.') | |
parser.add_argument('-m','--multi-byte-text-config',type=str,default='s50,Meiryo UI',help='Text configurations for 1-byte characters.') | |
parser.add_argument('-f','--input-file',type=str,default='',help='Input file to configure.') | |
args = parser.parse_args() | |
params = vars(args) | |
textConfig = params['text_config'] | |
multiByteTextConfig = params['multi_byte_text_config'] | |
inFile = params['input_file'] | |
if (inFile == ''): | |
parser.print_help() | |
sys.exit(1) | |
with open(inFile,encoding='UTF-8') as f: | |
for line in f: | |
line = regex.sub(r'([^\p{Katakana}\p{Hiragana}\p{Han}\p{Greek}…ー。、→`・´\r\n]+)',"<"+textConfig+">\\1",line) | |
print(regex.sub(r'([\p{Katakana}\p{Hiragana}\p{Han}\p{Greek}…ー。、→`・´]+)',"<"+multiByteTextConfig+">\\1",line),end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment