ProcessSub.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from difflib import SequenceMatcher
  2. import os
  3. from gtts import gTTS
  4. from mutagen.mp3 import MP3
  5. import difflib
  6. #max_len = 3
  7. def similar(a, b):
  8. return SequenceMatcher(None, a, b).ratio()
  9. def audio_compare_from_text(gt,gen):
  10. tts=gTTS(text=gen, lang='zh')
  11. tts.save("gen_tmp.mp3")
  12. tts=gTTS(text=gt, lang='zh')
  13. tts.save("gt_tmp.mp3")
  14. audio = MP3("gen_tmp.mp3")
  15. gen_len = audio.info.length
  16. audio = MP3("gt_tmp.mp3")
  17. gt_len = audio.info.length
  18. os.remove('gen_tmp.mp3')
  19. os.remove('gt_tmp.mp3')
  20. return gt_len, gen_len
  21. #break in nested loop only break 1 layer
  22. def adjustSub_by_audio_similarity(gt_array, generated_array):
  23. for gen_idx in range(len(generated_array)):
  24. #print('------------------------------------------------------------------------')
  25. for gt_idx in range(len(gt_array)-max_len):
  26. for l in range(max_len):
  27. gt_text = gt_array[gt_idx]
  28. gen_text = generated_array[gen_idx]
  29. for idx_num in range(l):
  30. gt_text += gt_array[gt_idx+idx_num+1]
  31. gt_len, gen_len = audio_compare_from_text(gt_text,gen_text)
  32. #print(gt_text+'|'+str(gt_len)+'|'+gen_text+'|'+str(gen_len)+'|'+'SIMILARITY:'+str(similar(gt_text,gen_text)))
  33. if abs(gen_len - gt_len) < 0.3 and similar(gt_text,gen_text) > 0.5:
  34. generated_array[gen_idx] = gt_text
  35. break
  36. return generated_array
  37. '''
  38. gts = 'Hello Kitty 於2018年,加入YouTube開始活動,在自我介紹的影片裡,Kitty表示一直憧憬著,想在YouTube跟大家見面,一開頻道就吸引許多粉絲訂閱,目前有28萬訂閱者,接下來這位花生君,於2017年加入YouTube開始活動,他的外型太過特別,花生頭、紅色圍巾與紙尿布,被觀眾評價為,第一眼看上很噁心,但看著看著還挺可愛,目前有12萬訂閱者'.split(',')
  39. gens = ['Hello Kitty瑜2018年加入YouTube開始活動','再次我介紹的影片裡','Kitty表示一直憧憬著長在YouTube跟大家見面','一開頻道就吸引許多粉絲訂閱付錢有28萬訂閱者','接下來這位花生君瑜2017年加入YouTube開始湖','活動','他的外型太過特別花生桃紅色圍巾魚紙尿布','被觀眾評價為第一眼看上很噁心但看著看著還挺','秦可愛','目前有12萬訂閱者']
  40. adjs = adjustSub_by_gt_array(gts,gens)
  41. for s in adjs:
  42. print(s)
  43. '''
  44. def parse_script(file_path):
  45. with open(file_path, 'r') as f:
  46. lines = [line.strip() for line in f]
  47. dict_list = []
  48. for idx in range(int((len(lines)+1)/4)):
  49. script={}
  50. script['index'] = lines[idx * 4]
  51. time_raw = lines[idx * 4 + 1]
  52. script['content'] = lines[idx * 4 + 2]
  53. start = time_raw.split(' --> ')[0].split(':')
  54. stop = time_raw.split(' --> ')[1].split(':')
  55. start[2] = start[2].replace(',','.')
  56. stop[2] = stop[2].replace(',','.')
  57. start_sec = float(start[0])*3600 + float(start[1])*60 + float(start[2])
  58. stop_sec = float(stop[0])*3600 + float(stop[1])*60 + float(stop[2])
  59. duration = start_sec-stop_sec
  60. script['start'] = start_sec
  61. script['stop'] = stop_sec
  62. script['duration'] = abs(duration)
  63. dict_list.append(script)
  64. return dict_list
  65. gg = parse_script('out.txt')
  66. for g in gg:
  67. print(g)
  68. gts = 'Hello Kitty 於2018年,加入YouTube開始活動,在自我介紹的影片裡,Kitty表示一直憧憬著,想在YouTube跟大家見面,一開頻道就吸引許多粉絲訂閱,目前有28萬訂閱者,接下來這位花生君,於2017年加入YouTube開始活動,他的外型太過特別,花生頭、紅色圍巾與紙尿布,被觀眾評價為,第一眼看上很噁心,但看著看著還挺可愛,目前有12萬訂閱者'.split(',')
  69. gens = ['Hello Kitty瑜2018年加入YouTube開始活動','再次我介紹的影片裡','Kitty表示一直憧憬著長在YouTube跟大家見面','一開頻道就吸引許多粉絲訂閱付錢有28萬訂閱者','接下來這位花生君瑜2017年加入YouTube開始湖','活動','他的外型太過特別花生桃紅色圍巾魚紙尿布','被觀眾評價為第一眼看上很噁心但看著看著還挺','秦可愛','目前有12萬訂閱者']
  70. def adjustSub_by_text_similarity(gts,gens):
  71. combine2 = [''.join([i,j]) for i,j in zip(gts, gts[1:])]
  72. combine3 = [''.join([i,j,k]) for i,j,k in zip(gts, gts[1:], gts[2:])]
  73. alls = gts+combine2+combine3
  74. for idx in range(len(gens)):
  75. match_text = difflib.get_close_matches(gens[idx], alls, cutoff=0.1)
  76. if len(match_text) != 0:
  77. print('{ '+gens[idx]+' }校正後: '+match_text[0])
  78. gens[idx] = match_text[0]
  79. else:
  80. print('無校正:'+gens[idx])
  81. adjustSub_by_text_similarity(gts,gens)