Skip to content

Instantly share code, notes, and snippets.

@byung-u
Last active December 28, 2016 06:29
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 byung-u/b0955f2a0f42b4c0b9d578f64ecee606 to your computer and use it in GitHub Desktop.
Save byung-u/b0955f2a0f42b4c0b9d578f64ecee606 to your computer and use it in GitHub Desktop.
구글 기계번역 후 자막의 시간 정보가 바뀌는데 이를 한번에 수정해주는 스크립트
#!/usr/bin/env python3.5
#
# 자막의 원본 파일의 시간 형식 : 00:36:47.450 --> 00:36:50.560
# 구글 기계번역 후의 시간 형식 : 00 : 37 : 24.250 -> 00 : 37 : 30.160
#
# 차이점
# 1. ':' 앞뒤로 공백 추가
# 2. '-'가 1개 사라짐
#
# 이를 원래의 자막 시간 형식으로 되돌려 주는 스크립트
# USAGE
# ./xxxx.py subtitle
# 수행하면 subtitle.2016122832 결과 파일
import re
import sys
from time import gmtime, strftime
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Usage: subtitle_time_remove_space.py FILE', file=sys.stderr)
sys.exit(2)
file_name = sys.argv[1]
out_file_name = '%s.%s' % (file_name, strftime("%Y%m%d%H%M%S", gmtime()))
# 00 : 37 : 24.250 -> 00 : 37 : 30.160
p = re.compile(r'\d+\d+\ :\ \d+\d+\ :\ \d+\d+\.\d+\d+\d+\ \-\>\ \d+\d+\ :\ \d+\d+\ :\ \d+\d+\.\d+\d+\d+')
fw = open(out_file_name, 'w')
with open(file_name) as f:
for line in f:
m = p.match(line)
if m is not None:
# 00:36:47.450 --> 00:36:50.560
fw.write(line.replace(' : ', ':').replace('->', '-->'))
else:
fw.write(line)
f.closed
fw.close()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment