Update app.py
Browse filesfix compression
app.py
CHANGED
@@ -101,13 +101,18 @@ async def generate_audio_with_voice_prefix(text_segment, default_voice, rate, pi
|
|
101 |
if target_duration_ms is not None and os.path.exists(audio_path):
|
102 |
audio = AudioSegment.from_mp3(audio_path)
|
103 |
audio_duration_ms = len(audio)
|
104 |
-
|
|
|
|
|
105 |
speed_factor = audio_duration_ms / target_duration_ms
|
|
|
106 |
if speed_factor > 0:
|
107 |
-
# Use librosa for time stretching with better quality for speech
|
108 |
y, sr = librosa.load(audio_path, sr=None)
|
109 |
y_stretched = librosa.effects.time_stretch(y, rate=speed_factor)
|
110 |
sf.write(audio_path, y_stretched, sr)
|
|
|
|
|
|
|
111 |
return audio_path
|
112 |
return None
|
113 |
|
@@ -251,4 +256,4 @@ async def create_demo():
|
|
251 |
if __name__ == "__main__":
|
252 |
import soundfile as sf # Import soundfile here
|
253 |
demo = asyncio.run(create_demo())
|
254 |
-
demo.launch()
|
|
|
101 |
if target_duration_ms is not None and os.path.exists(audio_path):
|
102 |
audio = AudioSegment.from_mp3(audio_path)
|
103 |
audio_duration_ms = len(audio)
|
104 |
+
print(f"Generated audio duration: {audio_duration_ms}ms, Target duration: {target_duration_ms}ms") # Debug
|
105 |
+
|
106 |
+
if audio_duration_ms > target_duration_ms and target_duration_ms > 0:
|
107 |
speed_factor = audio_duration_ms / target_duration_ms
|
108 |
+
print(f"Speed factor (to reduce duration): {speed_factor}") # Debug
|
109 |
if speed_factor > 0:
|
|
|
110 |
y, sr = librosa.load(audio_path, sr=None)
|
111 |
y_stretched = librosa.effects.time_stretch(y, rate=speed_factor)
|
112 |
sf.write(audio_path, y_stretched, sr)
|
113 |
+
else:
|
114 |
+
print("Generated audio is not longer than target duration, no speed adjustment.") # Debug
|
115 |
+
|
116 |
return audio_path
|
117 |
return None
|
118 |
|
|
|
256 |
if __name__ == "__main__":
|
257 |
import soundfile as sf # Import soundfile here
|
258 |
demo = asyncio.run(create_demo())
|
259 |
+
demo.launch()
|