我正在使用pyttsx和一些线程来阅读字幕。我希望时间准确。但是,使用当前代码,我的演讲已经遥遥领先。不是说它是必需的,但我使用的 .srt 是Sub
SRT 样本:
1
00:00:01,000 --> 00:00:06,000
KoreanDramaX.com
2
00:02:00,000 --> 00:02:05,000
<i>[Ten Miles of Peach Blossoms]</i>
3
00:02:05,000 --> 00:02:09,000
<i>[Episode 14]</i>
4
00:02:30,400 --> 00:02:32,560
I like your eyes the most.
我的代码:
#this fuction is used to pass string to TTS engine.
def speechafter(x):
engine = pyttsx.init()
engine.say(x.replace('[','').replace(']','').replace('\'','').replace('"','').replace('<i>','').replace('</i>',''))
engine.runAndWait()
# Here I am converting time to milliseconds.
def get_ms(t):
return (t.hours*60*60 + t.minutes*60 + t.seconds)*1000 + t.milliseconds
def start_speak(event):
set2 = 0
#Trying to adjust the time after each subtitle is played.
for sub in subs:
start = get_ms(subs[set2].start) # current sub being processed
start2 = get_ms(subs[(set2 - 1)].start) #Previous sub
if set2 <= 1:
newtime = get_ms(sub.start)
else:
# I'm not letting all the points to be processed at the beginning because I would not be able to pause and restart.
newtime = start - start2
print newtime
set2 += 1
time.sleep(newtime / 1000)
test = Thread(target=speechafter, kwargs={'x':sub.text})
test.start()