ZiyuG commited on
Commit
f08d9ef
·
verified ·
1 Parent(s): 0e400c0

Update audio.py

Browse files
Files changed (1) hide show
  1. audio.py +13 -3
audio.py CHANGED
@@ -3,6 +3,8 @@ from gtts import gTTS
3
  from pydub import AudioSegment
4
  import tempfile
5
  import os
 
 
6
 
7
  os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/local/bin/ffmpeg"
8
  # os.environ["IMAGEMAGICK_BINARY"] = "/usr/local/bin/convert"
@@ -12,7 +14,7 @@ def gen_audio(subtitles, tmpdir):
12
  # [[0, 3], "這裡請注意右臂"],
13
  # [[4, 8], "這裡請注意左臂"],
14
  # [[9, 12], "這裡請注意左手細節"]
15
- # ]
16
  subtitles = merge_subtitles(subtitles)
17
  print("===> Subtitles:")
18
  for subtitle in subtitles:
@@ -44,13 +46,19 @@ def gen_audio(subtitles, tmpdir):
44
  temp_audio_path = tempfile.mkdtemp()
45
  clips = []
46
 
 
 
 
 
 
47
  for i, (start_end, text) in enumerate(subtitles):
48
  start_time, end_time = start_end
49
  duration = end_time - start_time
50
 
51
- tts = gTTS(text=text, lang='zh')
52
  audio_path = os.path.join(temp_audio_path, f'subtitle_{i}.mp3')
53
- tts.save(audio_path)
 
54
 
55
  audio_segment = AudioSegment.from_mp3(audio_path)
56
 
@@ -104,6 +112,8 @@ def merge_subtitles(subtitles):
104
  break
105
 
106
  current_content = '這裡請注意' + '、'.join(list(set(current_content.replace("細節", "").split('、'))))
 
 
107
  merged_subtitles.append([[current_start, current_end], current_content])
108
 
109
  return merged_subtitles
 
3
  from pydub import AudioSegment
4
  import tempfile
5
  import os
6
+ import pyttsx3
7
+ from ms_tts import ms_tts_gen
8
 
9
  os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/local/bin/ffmpeg"
10
  # os.environ["IMAGEMAGICK_BINARY"] = "/usr/local/bin/convert"
 
14
  # [[0, 3], "這裡請注意右臂"],
15
  # [[4, 8], "這裡請注意左臂"],
16
  # [[9, 12], "這裡請注意左手細節"]
17
+ # ])
18
  subtitles = merge_subtitles(subtitles)
19
  print("===> Subtitles:")
20
  for subtitle in subtitles:
 
46
  temp_audio_path = tempfile.mkdtemp()
47
  clips = []
48
 
49
+ # engine = pyttsx3.init()
50
+ # engine.setProperty('voice', 'yue') # yue:粤语 cnm:普通话
51
+ # engine.setProperty('rate', 150) # 设置语速
52
+ # engine.setProperty('volume', 1.0) # 设置音量
53
+
54
  for i, (start_end, text) in enumerate(subtitles):
55
  start_time, end_time = start_end
56
  duration = end_time - start_time
57
 
58
+ # tts = gTTS(text=text, lang='yue')
59
  audio_path = os.path.join(temp_audio_path, f'subtitle_{i}.mp3')
60
+ # tts.save(audio_path)
61
+ ms_tts_gen(text, audio_path)
62
 
63
  audio_segment = AudioSegment.from_mp3(audio_path)
64
 
 
112
  break
113
 
114
  current_content = '這裡請注意' + '、'.join(list(set(current_content.replace("細節", "").split('、'))))
115
+ current_content = current_content.replace("左手、右手", "雙手").replace("右手、左手", "雙手")
116
+ current_content = current_content.replace("左臂、右臂", "雙臂").replace("右臂、左臂", "雙臂")
117
  merged_subtitles.append([[current_start, current_end], current_content])
118
 
119
  return merged_subtitles