Advertisement
Appendko

TTS_time

Nov 5th, 2017
3,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. #Python script to calculate time needed for google tts
  2. #Need to install "gTTS" and "mutagen" from pip
  3. #Maintained by Append ([email protected])
  4.  
  5. import time
  6. import shelve
  7. import ssl
  8.  
  9. from gtts import gTTS
  10. from mutagen.mp3 import MP3
  11.  
  12. FILE_SHELVE='result.shelve'
  13.      
  14. def calc_ttstime(speak,repeat):
  15.     """
  16.    For the string "speak"*repeat, measured its length after it is pronounced by Google TTS.
  17.    This function utilize gTTS package to generate mp3 file,
  18.    then measure the file length with mutagen package.
  19.    
  20.    speak: the text you want Google TTS to pronounce.
  21.           Can be integer(unicode) or string.
  22.    """
  23.     if type(speak) == int:
  24.         # 0x4E00 -> chr(0x4E00) -> chr(0x4E00)+chr(0x4E00)+chr(0x4E00)+...
  25.         # store the files using individual name
  26.         speakstr = chr(speak)*repeat
  27.         mp3_file=str('0x%4X'%speak)+".mp3"
  28.     elif type(speak) == str:    
  29.         # speak+speak+speak+speak+speak+speak+...
  30.         # store in a specific file
  31.         speakstr = speak*repeat
  32.         mp3_file="tmp.mp3"
  33.     # Generate the mp3 file using gTTS package
  34.     tts = gTTS(text=speakstr, lang='zh-TW', slow=False)
  35.     tts.save(mp3_file)
  36.     # Measure the length
  37.     audio = MP3(mp3_file)
  38.     return audio.info.length
  39.  
  40. def sort_shelve():
  41.     """
  42.    return the sorted data from current shelve.
  43.    """
  44.     tmp_shelve=shelve.open(FILE_SHELVE)
  45.     tmp_lst = sorted(tmp_shelve.values(),key=lambda x:x[1])
  46.     tmp_shelve.close()
  47.     return tmp_lst
  48.  
  49. def build_shelve():
  50.     """
  51.    generate calc_ttstime(x) for all characters in unicode CJK block.
  52.    """
  53.     #unicode CJK block: 0x4E00 ~ 0x9FFFF
  54.     for i in range(0x4E00,0x9FFF):
  55.         str_i = str('0x%4X'%i)
  56.         res_shelve = shelve.open(FILE_SHELVE,flag='c')
  57.         if str_i not in res_shelve:      
  58.             try:
  59.                 tic=time.time()
  60.                 res=(chr(i),calc_ttstime(i,10))
  61.                 toc=time.time()-tic
  62.                 print("%s, %s, %10.6f, %10.6f"%(str_i,res[0],res[1],toc))
  63.                 res_shelve[str_i]=res
  64.             except KeyboardInterrupt:
  65.                 break
  66.             except ssl.SSLError:
  67.                 print("%s, SSL Error, leave Blank"%str_i)
  68.             except:
  69.                 pass
  70.         res_shelve.close()
  71.  
  72. if __name__ == "__main__":
  73.     build_shelve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement