Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,11 +13,6 @@ def get_audio_length(audio_path):
|
|
| 13 |
rate = audio.getframerate()
|
| 14 |
return frames / float(rate)
|
| 15 |
|
| 16 |
-
# Ensure that the file starts with "RIFF" (WAV header)
|
| 17 |
-
def is_wav_file(filepath):
|
| 18 |
-
with open(filepath, 'rb') as f:
|
| 19 |
-
return f.read(4) == b'RIFF'
|
| 20 |
-
|
| 21 |
# Generate precise SRT entries for a text batch
|
| 22 |
def generate_accurate_srt(text, start_time, batch_index):
|
| 23 |
srt_entries = []
|
|
@@ -48,28 +43,28 @@ async def batch_process_srt_and_audio(script_text, voice, batch_size=500, progre
|
|
| 48 |
# Split text into manageable batches
|
| 49 |
for i in range(0, len(script_text), batch_size):
|
| 50 |
batch_text = script_text[i:i+batch_size]
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
# Generate audio for each batch and save as
|
| 54 |
tts = edge_tts.Communicate(batch_text, voice, rate="-25%")
|
| 55 |
-
await tts.save(
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
batch_duration = get_audio_length(
|
| 63 |
srt_entries, cumulative_time = generate_accurate_srt(batch_text, cumulative_time, batch_index)
|
| 64 |
|
| 65 |
# Append entries and audio for the batch
|
| 66 |
total_srt_entries.extend(srt_entries)
|
| 67 |
-
batch_audio = AudioSegment.from_file(audio_file, format="wav") # Explicitly specify format
|
| 68 |
combined_audio += batch_audio
|
| 69 |
batch_index += len(srt_entries)
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
os.remove(
|
| 73 |
|
| 74 |
# Export combined audio and SRT
|
| 75 |
combined_audio.export("final_audio.wav", format="wav")
|
|
|
|
| 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 = []
|
|
|
|
| 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 |
+
mp3_file = f"audio_batch_{i}.mp3" # Save as MP3 first
|
| 47 |
+
wav_file = f"audio_batch_{i}.wav" # Convert to WAV
|
| 48 |
|
| 49 |
+
# Generate audio for each batch and save as MP3
|
| 50 |
tts = edge_tts.Communicate(batch_text, voice, rate="-25%")
|
| 51 |
+
await tts.save(mp3_file)
|
| 52 |
|
| 53 |
+
# Convert MP3 to WAV
|
| 54 |
+
batch_audio = AudioSegment.from_file(mp3_file, format="mp3")
|
| 55 |
+
batch_audio.export(wav_file, format="wav")
|
| 56 |
|
| 57 |
+
# Ensure WAV conversion succeeded and calculate duration
|
| 58 |
+
batch_duration = get_audio_length(wav_file)
|
| 59 |
srt_entries, cumulative_time = generate_accurate_srt(batch_text, cumulative_time, batch_index)
|
| 60 |
|
| 61 |
# Append entries and audio for the batch
|
| 62 |
total_srt_entries.extend(srt_entries)
|
|
|
|
| 63 |
combined_audio += batch_audio
|
| 64 |
batch_index += len(srt_entries)
|
| 65 |
|
| 66 |
+
# Clean up temporary MP3 file
|
| 67 |
+
os.remove(mp3_file)
|
| 68 |
|
| 69 |
# Export combined audio and SRT
|
| 70 |
combined_audio.export("final_audio.wav", format="wav")
|