Last active
December 10, 2015 03:58
-
-
Save scturtle/4378068 to your computer and use it in GitHub Desktop.
wild subber 野生字幕君
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import re | |
import os | |
import sys | |
import time | |
import bisect | |
import threading | |
subs = [] | |
p = re.compile('(\d\d):(\d\d):(\d\d),(\d\d\d)') | |
win = sys.platform.startswith('win') | |
lst = time.time() | |
cur = 0.0 | |
lastsub = '' | |
# getch | |
if win: | |
# for windows | |
from msvcrt import getch | |
cls = lambda: os.system('cls') | |
else: | |
# for unix | |
import tty, termios | |
cls = lambda: os.system('clear') | |
def getch(): | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
# getsub | |
def getsub(name): | |
t = open(name).read().splitlines() | |
t[0] = '1' # fix bom | |
i = 0 | |
while i < len(t): | |
if not t[i].isdigit(): | |
print 'Error at line', i+1 | |
exit() | |
i += 1 | |
times = p.findall(t[i]) | |
for j in xrange(2): | |
times[j] = map(int, times[j]) | |
times[j] = (times[j][0]*3600 + times[j][1]*60 + | |
times[j][2] + times[j][3]/1000.0) | |
i += 1 | |
lines = '' | |
while i < len(t) and t[i]: | |
lines += ('\r\n' if lines else '') + t[i] | |
i += 1 | |
# (start time, end time, lines) | |
subs.append((times[0], times[1], lines)) | |
i += 1 | |
class Subber(threading.Thread): | |
pause = False | |
def run(self): | |
global lst, cur, si, lastsub | |
while True: | |
time.sleep(0.1) | |
now = time.time() | |
dur = now - lst | |
lst = now | |
if self.pause: continue | |
cur += dur | |
sub = '\r\n{:02}:{:02}:{:02}'.format(int(cur / 3600), | |
int(cur % 3600 / 60), | |
int(cur % 60)) | |
# binary search to find the current subtitle line | |
si = bisect.bisect(subs, (cur, float('inf'), '')) | |
if si: si -= 1 | |
if subs[si][0] <= cur <= subs[si][1]: | |
sub = subs[si][2] + sub | |
if sub != lastsub: | |
cls() | |
print sub | |
lastsub = sub | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print 'Useage: {} moive.srt [start_seconds]'.format(sys.argv[0]) | |
exit() | |
getsub(sys.argv[1]) | |
if len(sys.argv) > 2: | |
cur = float(sys.argv[2]) | |
th = Subber() | |
th.daemon = True | |
th.start() | |
while True: | |
c = ord(getch()) | |
# left to backward 1 sec, right to forward 1 sec | |
if win: | |
cur += (-1 if c == 75 else (1 if c == 77 else 0)) | |
else: | |
cur += (-1 if c == 68 else (1 if c == 67 else 0)) | |
if c == 32: # space to pause | |
th.pause = not th.pause | |
if c in [3, 13]: # ctrl-c and enter to exit | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment