import gradio as gr import edge_tts import asyncio import tempfile import os from moviepy.editor import AudioFileClip # Get all available voices async def get_voices(): voices = await edge_tts.list_voices() return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices} # Text to speech functionality async def text_to_speech(text, voice, rate, pitch): if not text.strip(): return None, gr.Warning("Please enter the text to convert.") if not voice: return None, gr.Warning("Please select a voice.") voice_short_name = voice.split(" - ")[0] rate_str = f"{rate:+d}%" pitch_str = f"{pitch:+d}Hz" communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str) with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: tmp_path = tmp_file.name await communicate.save(tmp_path) return tmp_path, None # Generate SRT file based on user preferences def generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph): total_paragraphs = len(paragraphs) # Calculate how long each segment will be displayed segment_duration = audio_duration / total_paragraphs # Total audio duration divided by total paragraphs current_time = 0 with open(srt_path, 'w', encoding='utf-8') as srt_file: for i, paragraph in enumerate(paragraphs): words = paragraph.split() lines = [words[j:j + words_per_line] for j in range(0, len(words), words_per_line)] lines = [' '.join(line) for line in lines] start_time = current_time end_time = min(start_time + segment_duration, audio_duration) # Ensure it doesn't exceed audio duration start_time_str = format_srt_time(start_time) end_time_str = format_srt_time(end_time) srt_file.write(f"{i + 1}\n{start_time_str} --> {end_time_str}\n") srt_file.write('\n'.join(lines) + '\n\n') current_time += segment_duration # Update current time for the next segment return srt_path def format_srt_time(seconds): millis = int((seconds - int(seconds)) * 1000) seconds = int(seconds) minutes = seconds // 60 hours = seconds // 3600 minutes %= 60 seconds %= 60 return f"{hours:02}:{minutes:02}:{seconds:02},{millis:03}" # Text to audio and SRT functionality async def text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_per_paragraph): audio_path, warning = await text_to_speech(text, voice, rate, pitch) if warning: return None, None, warning audio_clip = AudioFileClip(audio_path) audio_duration = audio_clip.duration # Generate SRT file based on the entire text base_name = os.path.splitext(audio_path)[0] srt_path = f"{base_name}_subtitle.srt" # Split input text into paragraphs based on larger gaps (two consecutive newlines) paragraphs = [p.strip() for p in text.split('\n\n') if p.strip()] generate_srt(paragraphs, audio_duration, srt_path, words_per_line, lines_per_paragraph) return audio_path, srt_path, None # Gradio interface function def tts_interface(text, voice, rate, pitch, words_per_line, lines_per_paragraph): audio_path, srt_path, warning = asyncio.run(text_to_audio_and_srt(text, voice, rate, pitch, words_per_line, lines_per_paragraph)) return audio_path, srt_path, warning # Create Gradio app async def create_demo(): voices = await get_voices() with gr.Blocks() as demo: gr.Markdown( """

Text to Speech with Subtitles

Convert your text to natural-sounding speech and generate subtitles (SRT) for your audio.

""", elem_id="header" ) with gr.Row(): with gr.Column(): text_input = gr.Textbox(label="Input Text", lines=5, placeholder="Enter text here...") voice_dropdown = gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value="") rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1) pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1) words_per_line = gr.Slider(minimum=3, maximum=8, value=5, label="Words per Line", step=1) lines_per_paragraph = gr.Slider(minimum=1, maximum=5, value=2, label="Lines per Paragraph", step=1) generate_button = gr.Button("Generate Audio and Subtitles", variant="primary") with gr.Column(): output_audio = gr.Audio(label="Generated Audio", type="filepath") output_srt = gr.File(label="Generated SRT", file_count="single") warning_msg = gr.Markdown(label="Warning", visible=False) generate_button.click( fn=tts_interface, inputs=[text_input, voice_dropdown, rate_slider, pitch_slider, words_per_line, lines_per_paragraph], outputs=[output_audio, output_srt, warning_msg] ) return demo # Run the app if __name__ == "__main__": demo = asyncio.run(create_demo()) demo.launch()