hivecorp commited on
Commit
12ef859
·
verified ·
1 Parent(s): 7bf74d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -29
app.py CHANGED
@@ -4,9 +4,6 @@ import asyncio
4
  import tempfile
5
  import os
6
  from moviepy.editor import AudioFileClip, concatenate_audioclips
7
- from wand.image import Image
8
- from wand.drawing import Drawing
9
- from wand.color import Color
10
 
11
  # 获取所有可用的语音
12
  async def get_voices():
@@ -30,20 +27,23 @@ async def text_to_speech(text, voice, rate, pitch):
30
  return tmp_path, None
31
 
32
  # 生成SRT文件
33
- def generate_srt(words, audio_clips):
34
  srt_path = os.path.join(tempfile.gettempdir(), "output_subtitles.srt")
35
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
 
 
36
  current_time = 0
37
- for i, (word, audio_clip) in enumerate(zip(words, audio_clips)):
 
38
  start_time = current_time
39
- end_time = start_time + audio_clip.duration
40
 
41
  # Convert to SRT format
42
  start_time_str = format_srt_time(start_time)
43
  end_time_str = format_srt_time(end_time)
44
  srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n{word}\n\n")
45
 
46
- current_time += audio_clip.duration # Update current time
47
 
48
  return srt_path
49
 
@@ -58,30 +58,19 @@ def format_srt_time(seconds):
58
 
59
  # 文字转音频和SRT功能
60
  async def text_to_audio_and_srt(text, voice, rate, pitch):
61
- # Split text into words
62
- words = text.split()
63
- audio_clips = []
64
- subtitle_chunks = []
65
-
66
- # Generate audio for each chunk of 3 words
67
- for i in range(0, len(words), 3): # Adjust chunk size if needed
68
- chunk = ' '.join(words[i:i + 3]) # Create a chunk of 3 words
69
- audio, warning = await text_to_speech(chunk, voice, rate, pitch)
70
- if warning:
71
- return None, None, warning
72
-
73
- audio_clip = AudioFileClip(audio)
74
- audio_clips.append(audio_clip)
75
- subtitle_chunks.append(chunk)
76
 
77
- # Concatenate all audio clips
78
- final_audio_path = os.path.join(tempfile.gettempdir(), "output_audio.mp3")
79
- final_audio = concatenate_audioclips(audio_clips)
80
- final_audio.write_audiofile(final_audio_path)
 
 
81
 
82
- # 生成SRT文件
83
- srt_path = generate_srt(subtitle_chunks, audio_clips)
84
- return final_audio_path, srt_path, None
85
 
86
  # Gradio接口函数
87
  def tts_interface(text, voice, rate, pitch):
 
4
  import tempfile
5
  import os
6
  from moviepy.editor import AudioFileClip, concatenate_audioclips
 
 
 
7
 
8
  # 获取所有可用的语音
9
  async def get_voices():
 
27
  return tmp_path, None
28
 
29
  # 生成SRT文件
30
+ def generate_srt(words, audio_duration, fps=24):
31
  srt_path = os.path.join(tempfile.gettempdir(), "output_subtitles.srt")
32
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
33
+ # Split audio duration into segments for SRT
34
+ segment_duration = audio_duration / len(words) # Average duration per word
35
  current_time = 0
36
+
37
+ for i, word in enumerate(words):
38
  start_time = current_time
39
+ end_time = start_time + segment_duration
40
 
41
  # Convert to SRT format
42
  start_time_str = format_srt_time(start_time)
43
  end_time_str = format_srt_time(end_time)
44
  srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n{word}\n\n")
45
 
46
+ current_time += segment_duration # Update current time
47
 
48
  return srt_path
49
 
 
58
 
59
  # 文字转音频和SRT功能
60
  async def text_to_audio_and_srt(text, voice, rate, pitch):
61
+ # Generate audio for the entire text
62
+ audio_path, warning = await text_to_speech(text, voice, rate, pitch)
63
+ if warning:
64
+ return None, None, warning
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ audio_clip = AudioFileClip(audio_path)
67
+ audio_duration = audio_clip.duration
68
+
69
+ # Generate SRT file based on the entire text
70
+ words = text.split()
71
+ srt_path = generate_srt(words, audio_duration)
72
 
73
+ return audio_path, srt_path, None
 
 
74
 
75
  # Gradio接口函数
76
  def tts_interface(text, voice, rate, pitch):