Skip to content

Instantly share code, notes, and snippets.

@pandanote-info
Last active October 1, 2018 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandanote-info/41ddc167763279f4c9044e01edb2bd15 to your computer and use it in GitHub Desktop.
Save pandanote-info/41ddc167763279f4c9044e01edb2bd15 to your computer and use it in GitHub Desktop.
AviUtl用字幕の量産ツールです。Python3で記述しています。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# テキストファイルを1行ずつ読み込み、exoフォーマットで記述されたAviUtlの
# 字幕のテンプレートに挿入して、exoフォーマットで標準出力に出力するプログラム。
# Copyright 2017,2018 pandanote.info (https://pandanote.info/)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# 画面サイズ、オーディオレート及びサンプリング周波数はテンプレートファイルの
# 設定がそのまま使用されます。
# 使用法:
# python3 txt2exo.py <テンプレートファイル> <テキストファイル>
#
# 修正履歴:
# 2018/10/01: テンプレートファイルで定義されているテキストオブジェクトに
# 中間点が設定されていた場合に対応するためのコードを追加した。
#
import io
import sys
import re
import codecs
from struct import *
from binascii import hexlify
# 標準出力の文字コードを変更する。
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='cp932')
if len(sys.argv) <= 2:
print("Usage: txt2exo.py <template file> <text file>")
sys.exit(1)
templatefile = sys.argv[1]
textfile = sys.argv[2]
exedit = {}
zero = {}
# 現在のセクションの文字列をそのまま入れます。
currentsection = "exedit"
originalstart = 0
originalend = 0
sectionsnum = 0
with open(templatefile,mode='r',encoding='shift_jis') as f:
for line in f:
m = re.match(r'\[([^\]]+)',line)
if m:
if m.group(1) != "exedit":
currentsection = m.group(1)
mm = re.match(r'^\d+$',m.group(1))
if mm:
sectionsnum = int(currentsection)
elif re.match(r'[^\r\n\s]',line):
kv = line.split('=',1)
if currentsection == "exedit":
exedit[kv[0]] = kv[1]
else:
if currentsection not in zero:
zero[currentsection] = {}
zero[currentsection][kv[0]] = kv[1]
if kv[0] == 'start':
if (currentsection == 0):
originalstart = int(kv[1])
elif kv[0] == 'end':
originalend = int(kv[1])
texts = [line.rstrip('\n').rstrip('\r') for line in open(textfile,"r",encoding="UTF-8") if re.match(r'^[^\r\n]',line)]
print("[exedit]")
for k,v in exedit.items():
print("{0:s}={1:s}".format(k,v),end="")
sectionsnum = sectionsnum + 1
sectioncount = 0
deltaframes = originalend-originalstart
startpos = originalstart
currentsection = 0
for text in texts:
for k,v in zero.items():
m = re.match(r'^\d+$',k)
if m:
currentsection = sectioncount+int(k)
print("[{0:d}]".format(currentsection))
for vk,vv in v.items():
if vk == "start":
print("{0:s}={1:d}".format(vk,startpos+int(vv)))
elif vk == "end":
print("{0:s}={1:d}".format(vk,startpos+int(vv)))
else:
print("{0:s}={1:s}".format(vk,vv),end="")
else:
sectionname = re.sub(r'^\d+',str(currentsection),k)
print("[{0:s}]".format(sectionname))
for vk,vv in v.items():
if vk == "text":
text_in_utf16 = hexlify(text.encode('utf-16'))
hex_str = text_in_utf16.decode("ascii")[4:]
print("text={0:s}".format(hex_str),end="")
for x in range(len(hex_str),4096,4):
print("0000",end="")
print("")
else:
print("{0:s}={1:s}".format(vk,vv),end="")
sectioncount = sectioncount + sectionsnum
startpos = startpos + deltaframes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment