Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,11 @@ def get_audio_length(audio_path):
|
|
| 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 = []
|
|
@@ -45,17 +50,21 @@ async def batch_process_srt_and_audio(script_text, voice, batch_size=500, progre
|
|
| 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 |
|
|
|
|
| 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 = []
|
|
|
|
| 50 |
batch_text = script_text[i:i+batch_size]
|
| 51 |
audio_file = f"audio_batch_{i}.wav"
|
| 52 |
|
| 53 |
+
# Generate audio for each batch and save as WAV
|
| 54 |
tts = edge_tts.Communicate(batch_text, voice, rate="-25%")
|
| 55 |
await tts.save(audio_file)
|
| 56 |
|
| 57 |
+
# Check if saved file is a valid WAV file
|
| 58 |
+
if not is_wav_file(audio_file):
|
| 59 |
+
raise ValueError(f"Audio file {audio_file} is not a valid WAV file.")
|
| 60 |
+
|
| 61 |
# Get precise audio length for synchronization
|
| 62 |
batch_duration = get_audio_length(audio_file)
|
| 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 |
|