bestoai commited on
Commit
c2db727
·
verified ·
1 Parent(s): 099a00c

Update app.py

Browse files

output path set

Files changed (1) hide show
  1. app.py +55 -16
app.py CHANGED
@@ -44,28 +44,50 @@ async def text_to_speech(text, voice, rate, pitch, output_path):
44
  # Generate SRT file with specified lines of subtitles
45
  def generate_srt(words, audio_duration, srt_path, num_lines):
46
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
47
- divisor = len(words) // (5 * num_lines)
48
- if divisor == 0:
49
- segment_duration = audio_duration # Use full duration as fallback
50
- else:
51
- segment_duration = audio_duration / divisor # Calculate duration per segment
52
-
53
  current_time = 0
 
54
  for i in range(0, len(words), 5 * num_lines):
55
  lines = []
56
  for j in range(num_lines):
57
- line = ' '.join(words[i + j * 5:i + (j + 1) * 5])
58
  if line:
59
  lines.append(line)
60
-
61
  start_time = current_time
62
  end_time = start_time + segment_duration
 
63
  start_time_str = format_srt_time(start_time)
64
  end_time_str = format_srt_time(end_time)
65
  srt_file.write(f"{i // (5 * num_lines) + 1}\n{start_time_str} --> {end_time_str}\n" + "\n".join(lines) + "\n\n")
 
66
  current_time += segment_duration
67
 
68
  return srt_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
  def format_srt_time(seconds):
@@ -78,8 +100,9 @@ def format_srt_time(seconds):
78
  return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
79
 
80
  # Text to audio and SRT functionality
81
- async def text_to_audio_and_srt(text, voice, rate, pitch, num_lines):
82
- audio_path, warning = await text_to_speech(text, voice, rate, pitch)
 
83
  if warning:
84
  return None, None, warning
85
 
@@ -87,17 +110,33 @@ async def text_to_audio_and_srt(text, voice, rate, pitch, num_lines):
87
  audio_duration = audio_clip.duration
88
 
89
  # Generate SRT file based on the entire text
90
- base_name = os.path.splitext(audio_path)[0]
91
- srt_path = f"{base_name}_subtitle.srt"
92
  words = text.split()
93
- generate_srt(words, audio_duration, srt_path, num_lines)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
- return audio_path, srt_path, None
96
 
97
  # Gradio interface function
98
- def tts_interface(text, voice, rate, pitch, num_lines):
99
- audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, num_lines))
100
  return audio_path, srt_path, warning
 
 
 
101
 
102
  # Create Gradio app
103
  async def create_demo():
 
44
  # Generate SRT file with specified lines of subtitles
45
  def generate_srt(words, audio_duration, srt_path, num_lines):
46
  with open(srt_path, 'w', encoding='utf-8') as srt_file:
47
+ segment_duration = audio_duration / (len(words) // (5 * num_lines)) # Average duration for each segment
 
 
 
 
 
48
  current_time = 0
49
+
50
  for i in range(0, len(words), 5 * num_lines):
51
  lines = []
52
  for j in range(num_lines):
53
+ line = ' '.join(words[i + j * 5:i + (j + 1) * 5]) # 5 words per line
54
  if line:
55
  lines.append(line)
56
+
57
  start_time = current_time
58
  end_time = start_time + segment_duration
59
+
60
  start_time_str = format_srt_time(start_time)
61
  end_time_str = format_srt_time(end_time)
62
  srt_file.write(f"{i // (5 * num_lines) + 1}\n{start_time_str} --> {end_time_str}\n" + "\n".join(lines) + "\n\n")
63
+
64
  current_time += segment_duration
65
 
66
  return srt_path
67
+ # def generate_srt(words, audio_duration, srt_path, num_lines):
68
+ # with open(srt_path, 'w', encoding='utf-8') as srt_file:
69
+ # divisor = len(words) // (5 * num_lines)
70
+ # if divisor == 0:
71
+ # segment_duration = audio_duration # Use full duration as fallback
72
+ # else:
73
+ # segment_duration = audio_duration / divisor # Calculate duration per segment
74
+
75
+ # current_time = 0
76
+ # for i in range(0, len(words), 5 * num_lines):
77
+ # lines = []
78
+ # for j in range(num_lines):
79
+ # line = ' '.join(words[i + j * 5:i + (j + 1) * 5])
80
+ # if line:
81
+ # lines.append(line)
82
+
83
+ # start_time = current_time
84
+ # end_time = start_time + segment_duration
85
+ # start_time_str = format_srt_time(start_time)
86
+ # end_time_str = format_srt_time(end_time)
87
+ # srt_file.write(f"{i // (5 * num_lines) + 1}\n{start_time_str} --> {end_time_str}\n" + "\n".join(lines) + "\n\n")
88
+ # current_time += segment_duration
89
+
90
+ # return srt_path
91
 
92
 
93
  def format_srt_time(seconds):
 
100
  return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}"
101
 
102
  # Text to audio and SRT functionality
103
+
104
+ async def text_to_audio_and_srt(text, voice, rate, pitch, num_lines, output_audio_path, output_srt_path):
105
+ audio_path, warning = await text_to_speech(text, voice, rate, pitch, output_audio_path)
106
  if warning:
107
  return None, None, warning
108
 
 
110
  audio_duration = audio_clip.duration
111
 
112
  # Generate SRT file based on the entire text
 
 
113
  words = text.split()
114
+ generate_srt(words, audio_duration, output_srt_path, num_lines)
115
+
116
+ return audio_path, output_srt_path, None
117
+ # async def text_to_audio_and_srt(text, voice, rate, pitch, num_lines):
118
+ # audio_path, warning = await text_to_speech(text, voice, rate, pitch)
119
+ # if warning:
120
+ # return None, None, warning
121
+
122
+ # audio_clip = AudioFileClip(audio_path)
123
+ # audio_duration = audio_clip.duration
124
+
125
+ # # Generate SRT file based on the entire text
126
+ # base_name = os.path.splitext(audio_path)[0]
127
+ # srt_path = f"{base_name}_subtitle.srt"
128
+ # words = text.split()
129
+ # generate_srt(words, audio_duration, srt_path, num_lines)
130
 
131
+ # return audio_path, srt_path, None
132
 
133
  # Gradio interface function
134
+ def tts_interface(text, voice, rate, pitch, num_lines, output_audio_path="output_audio.mp3", output_srt_path="output_subtitle.srt"):
135
+ audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, num_lines, output_audio_path, output_srt_path))
136
  return audio_path, srt_path, warning
137
+ # def tts_interface(text, voice, rate, pitch, num_lines):
138
+ # audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, num_lines))
139
+ # return audio_path, srt_path, warning
140
 
141
  # Create Gradio app
142
  async def create_demo():