Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,91 +2,126 @@ import gradio as gr
|
|
| 2 |
from pydub import AudioSegment
|
| 3 |
import edge_tts
|
| 4 |
import os
|
|
|
|
| 5 |
import asyncio
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return f"{hrs:02}:{mins:02}:{secs:02},{millis:03}"
|
| 20 |
-
|
| 21 |
-
# Function to generate SRT with accurate timing per batch
|
| 22 |
-
async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
| 23 |
-
audio_file = f"batch_{batch_num}_audio.wav"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
|
| 44 |
-
srt_content += " ".join(segment_words) + "\n\n"
|
| 45 |
-
start_time = end_time
|
| 46 |
-
|
| 47 |
-
return srt_content, audio_file, start_time
|
| 48 |
-
|
| 49 |
-
# Batch processing function for SRT and audio generation
|
| 50 |
-
async def batch_process_srt_and_audio(script_text):
|
| 51 |
-
batches = [script_text[i:i+500] for i in range(0, len(script_text), 500)]
|
| 52 |
-
all_srt_content = ""
|
| 53 |
combined_audio = AudioSegment.empty()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
batch_audio = AudioSegment.from_file(audio_file)
|
| 62 |
combined_audio += batch_audio
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
#
|
| 66 |
os.remove(audio_file)
|
| 67 |
-
|
| 68 |
# Export combined audio and SRT
|
| 69 |
combined_audio.export("final_audio.wav", format="wav")
|
| 70 |
with open("final_subtitles.srt", "w") as srt_file:
|
| 71 |
-
srt_file.write(
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
return "final_subtitles.srt", "final_audio.wav"
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
gr.File(label="Download SRT File"),
|
| 86 |
gr.File(label="Download Audio File"),
|
| 87 |
gr.Audio(label="Play Audio")
|
| 88 |
-
]
|
| 89 |
-
|
| 90 |
-
)
|
|
|
|
| 91 |
|
| 92 |
app.launch()
|
|
|
|
| 2 |
from pydub import AudioSegment
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
+
import wave
|
| 6 |
import asyncio
|
| 7 |
+
import srt
|
| 8 |
+
|
| 9 |
+
# Function to calculate audio duration
|
| 10 |
+
def get_audio_length(audio_path):
|
| 11 |
+
with wave.open(audio_path, 'rb') as audio:
|
| 12 |
+
frames = audio.getnframes()
|
| 13 |
+
rate = audio.getframerate()
|
| 14 |
+
return frames / float(rate)
|
| 15 |
+
|
| 16 |
+
# Generate precise SRT entries for a text batch
|
| 17 |
+
def generate_accurate_srt(text, start_time, batch_index):
|
| 18 |
+
srt_entries = []
|
| 19 |
+
current_time = start_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
for line in text.splitlines():
|
| 22 |
+
end_time = current_time + get_audio_length_for_line(line)
|
| 23 |
+
|
| 24 |
+
srt_entries.append(
|
| 25 |
+
srt.Subtitle(
|
| 26 |
+
index=batch_index,
|
| 27 |
+
start=srt.timedelta(seconds=current_time),
|
| 28 |
+
end=srt.timedelta(seconds=end_time),
|
| 29 |
+
content=line
|
| 30 |
+
)
|
| 31 |
+
)
|
| 32 |
+
current_time = end_time
|
| 33 |
+
batch_index += 1
|
| 34 |
+
return srt_entries, current_time
|
| 35 |
+
|
| 36 |
+
# Process batches and accumulate precise SRT entries
|
| 37 |
+
async def batch_process_srt_and_audio(script_text, voice, batch_size=500, progress=gr.Progress()):
|
| 38 |
+
total_srt_entries = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
combined_audio = AudioSegment.empty()
|
| 40 |
+
cumulative_time = 0.0 # Track total time for accurate SRT start times
|
| 41 |
+
batch_index = 1
|
| 42 |
+
|
| 43 |
+
# Split text into manageable batches
|
| 44 |
+
for i in range(0, len(script_text), batch_size):
|
| 45 |
+
batch_text = script_text[i:i+batch_size]
|
| 46 |
+
audio_file = f"audio_batch_{i}.wav"
|
| 47 |
+
|
| 48 |
+
# Generate audio for each batch
|
| 49 |
+
tts = edge_tts.Communicate(batch_text, voice, rate="-25%")
|
| 50 |
+
await tts.save(audio_file)
|
| 51 |
+
|
| 52 |
+
# Get precise audio length for synchronization
|
| 53 |
+
batch_duration = get_audio_length(audio_file)
|
| 54 |
+
srt_entries, cumulative_time = generate_accurate_srt(batch_text, cumulative_time, batch_index)
|
| 55 |
+
|
| 56 |
+
# Append entries and audio for the batch
|
| 57 |
+
total_srt_entries.extend(srt_entries)
|
| 58 |
batch_audio = AudioSegment.from_file(audio_file)
|
| 59 |
combined_audio += batch_audio
|
| 60 |
+
batch_index += len(srt_entries)
|
| 61 |
+
|
| 62 |
+
# Remove individual batch audio file
|
| 63 |
os.remove(audio_file)
|
| 64 |
+
|
| 65 |
# Export combined audio and SRT
|
| 66 |
combined_audio.export("final_audio.wav", format="wav")
|
| 67 |
with open("final_subtitles.srt", "w") as srt_file:
|
| 68 |
+
srt_file.write(srt.compose(total_srt_entries))
|
| 69 |
+
|
| 70 |
+
# Final validation check
|
| 71 |
+
validate_srt_against_audio("final_subtitles.srt", "final_audio.wav")
|
| 72 |
|
| 73 |
return "final_subtitles.srt", "final_audio.wav"
|
| 74 |
|
| 75 |
+
# Validate SRT timing with total audio length
|
| 76 |
+
def validate_srt_against_audio(srt_file_path, audio_file_path):
|
| 77 |
+
audio_duration = get_audio_length(audio_file_path)
|
| 78 |
+
|
| 79 |
+
with open(srt_file_path, 'r') as file:
|
| 80 |
+
subtitles = list(srt.parse(file.read()))
|
| 81 |
+
|
| 82 |
+
for subtitle in subtitles:
|
| 83 |
+
if subtitle.end.total_seconds() > audio_duration:
|
| 84 |
+
subtitle.end = srt.timedelta(seconds=audio_duration)
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
with open(srt_file_path, 'w') as file:
|
| 88 |
+
file.write(srt.compose(subtitles))
|
| 89 |
+
|
| 90 |
+
# Gradio function with error handling
|
| 91 |
+
async def process_script(script_text, language, voice):
|
| 92 |
+
try:
|
| 93 |
+
srt_path, audio_path = await batch_process_srt_and_audio(script_text, voice)
|
| 94 |
+
return srt_path, audio_path, audio_path
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Error: {e}")
|
| 97 |
+
return "An error occurred. Please check the script text and try again.", None, None
|
| 98 |
+
|
| 99 |
+
# Dynamic voice selection based on language
|
| 100 |
+
def update_voice_options(language):
|
| 101 |
+
voices = {
|
| 102 |
+
"en-US": ["en-US-AndrewNeural", "en-US-JennyNeural"],
|
| 103 |
+
"es-ES": ["es-ES-AlvaroNeural", "es-ES-ElviraNeural"]
|
| 104 |
+
}
|
| 105 |
+
return gr.update(choices=voices.get(language, []), value=voices.get(language, [])[0])
|
| 106 |
+
|
| 107 |
+
# Gradio app setup
|
| 108 |
+
with gr.Blocks() as app:
|
| 109 |
+
gr.Markdown("# Text to Speech with Accurate SRT and Audio Generation")
|
| 110 |
+
|
| 111 |
+
language = gr.Dropdown(choices=["en-US", "es-ES"], label="Select Language", value="en-US")
|
| 112 |
+
voice = gr.Dropdown(choices=["en-US-AndrewNeural", "en-US-JennyNeural"], label="Select Voice")
|
| 113 |
+
|
| 114 |
+
language.change(fn=update_voice_options, inputs=language, outputs=voice)
|
| 115 |
+
|
| 116 |
+
script_text = gr.Textbox(label="Enter Script Text", lines=10)
|
| 117 |
+
|
| 118 |
+
outputs = [
|
| 119 |
gr.File(label="Download SRT File"),
|
| 120 |
gr.File(label="Download Audio File"),
|
| 121 |
gr.Audio(label="Play Audio")
|
| 122 |
+
]
|
| 123 |
+
|
| 124 |
+
submit_button = gr.Button("Generate Audio and SRT")
|
| 125 |
+
submit_button.click(process_script, inputs=[script_text, language, voice], outputs=outputs)
|
| 126 |
|
| 127 |
app.launch()
|