Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from pydub import AudioSegment
|
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
import asyncio
|
|
|
|
| 6 |
|
| 7 |
# Function to get the length of an audio file in seconds
|
| 8 |
def get_audio_length(audio_file):
|
|
@@ -32,7 +33,8 @@ async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
|
| 32 |
# Initialize SRT content
|
| 33 |
srt_content = ""
|
| 34 |
words = batch_text.split()
|
| 35 |
-
|
|
|
|
| 36 |
start_time = start_offset
|
| 37 |
|
| 38 |
# Build SRT content with accurate timing
|
|
@@ -46,7 +48,7 @@ async def generate_accurate_srt(batch_text, batch_num, start_offset):
|
|
| 46 |
|
| 47 |
return srt_content, audio_file, start_time
|
| 48 |
|
| 49 |
-
# Batch processing function with concurrent processing and
|
| 50 |
async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
| 51 |
batches = [script_text[i:i+500] for i in range(0, len(script_text), 500)]
|
| 52 |
all_srt_content = ""
|
|
@@ -78,12 +80,17 @@ async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
|
| 78 |
completed_tasks += 1
|
| 79 |
progress(completed_tasks / total_tasks)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
srt_file.write(all_srt_content)
|
| 85 |
|
| 86 |
-
return
|
| 87 |
|
| 88 |
# Gradio interface function
|
| 89 |
async def process_script(script_text):
|
|
|
|
| 3 |
import edge_tts
|
| 4 |
import os
|
| 5 |
import asyncio
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
# Function to get the length of an audio file in seconds
|
| 9 |
def get_audio_length(audio_file):
|
|
|
|
| 33 |
# Initialize SRT content
|
| 34 |
srt_content = ""
|
| 35 |
words = batch_text.split()
|
| 36 |
+
num_segments = max(1, len(words) // 10) # Calculate number of segments based on ~10 words per segment
|
| 37 |
+
segment_duration = actual_length / num_segments # Duration for each segment
|
| 38 |
start_time = start_offset
|
| 39 |
|
| 40 |
# Build SRT content with accurate timing
|
|
|
|
| 48 |
|
| 49 |
return srt_content, audio_file, start_time
|
| 50 |
|
| 51 |
+
# Batch processing function with concurrent processing and progress indicator
|
| 52 |
async def batch_process_srt_and_audio(script_text, progress=gr.Progress()):
|
| 53 |
batches = [script_text[i:i+500] for i in range(0, len(script_text), 500)]
|
| 54 |
all_srt_content = ""
|
|
|
|
| 80 |
completed_tasks += 1
|
| 81 |
progress(completed_tasks / total_tasks)
|
| 82 |
|
| 83 |
+
# Generate unique names for the final files
|
| 84 |
+
unique_id = uuid.uuid4()
|
| 85 |
+
final_audio_path = f"final_audio_{unique_id}.wav"
|
| 86 |
+
final_srt_path = f"final_subtitles_{unique_id}.srt"
|
| 87 |
+
|
| 88 |
+
# Export combined audio and SRT with unique names
|
| 89 |
+
combined_audio.export(final_audio_path, format="wav")
|
| 90 |
+
with open(final_srt_path, "w") as srt_file:
|
| 91 |
srt_file.write(all_srt_content)
|
| 92 |
|
| 93 |
+
return final_srt_path, final_audio_path
|
| 94 |
|
| 95 |
# Gradio interface function
|
| 96 |
async def process_script(script_text):
|