字幕文字转语音

时间:2017-04-20 19:36:05

标签: multithreading time sleep text-to-speech pyttsx

我使用 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()

0 个答案:

没有答案