0

I am using pyttsx and some threading to read subtitles. I want the timing to be accurate. However with the current code my speech is getting to far ahead. Not that it is needed but the .srt I'm using is Sub

SRT sample:

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.

My Code:

#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()
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • If timing is critical for you I would first synthesize to a file then play the files on schedule. It is not easy to expect pytts to respect the timings. – Nikolay Shmyrev Apr 24 '17 at 22:12
  • I have looked in to that and when using that way everything runs on time. However in doing this I am unable to pause. – John Shiveley Apr 26 '17 at 19:28

0 Answers0