Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -240,6 +240,22 @@ async def generate_story_audios(story: StoryCreationDTO, base_output: str):
|
|
| 240 |
|
| 241 |
#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
from pydub import AudioSegment
|
| 244 |
import asyncio
|
| 245 |
|
|
@@ -276,7 +292,8 @@ async def concat_story_audio(story: StoryCreationDTO, base_output: str, final_pa
|
|
| 276 |
if scene.location.path:
|
| 277 |
sfx_file = await download_file_from_url(scene.location.path)
|
| 278 |
if sfx_file:
|
| 279 |
-
|
|
|
|
| 280 |
scene_audio = scene_audio.overlay(sfx_audio)
|
| 281 |
os.remove(sfx_file)
|
| 282 |
else:
|
|
@@ -286,7 +303,8 @@ async def concat_story_audio(story: StoryCreationDTO, base_output: str, final_pa
|
|
| 286 |
if scene.bgMusic and scene.bgMusic.musicPath:
|
| 287 |
bg_url = scene.bgMusic.musicPath
|
| 288 |
bg_file = await download_file_from_url(bg_url)
|
| 289 |
-
|
|
|
|
| 290 |
|
| 291 |
# Adjust volume
|
| 292 |
bg_audio = bg_audio - (1 - scene.bgMusic.volume) * 30 # approximate
|
|
|
|
| 240 |
|
| 241 |
#_______________ Concatenating the generated audios to make the final story (post-processing)_______________________
|
| 242 |
|
| 243 |
+
from pydub import AudioSegment
|
| 244 |
+
import os
|
| 245 |
+
|
| 246 |
+
def ensure_wav(file_path: str) -> str:
|
| 247 |
+
"""
|
| 248 |
+
Convert an MP3 (or other non-WAV) file to WAV.
|
| 249 |
+
Returns path to WAV file.
|
| 250 |
+
If already WAV, returns original path.
|
| 251 |
+
"""
|
| 252 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 253 |
+
if ext != ".wav":
|
| 254 |
+
wav_path = file_path.rsplit(".", 1)[0] + ".wav"
|
| 255 |
+
AudioSegment.from_file(file_path).export(wav_path, format="wav")
|
| 256 |
+
return wav_path
|
| 257 |
+
return file_path
|
| 258 |
+
|
| 259 |
from pydub import AudioSegment
|
| 260 |
import asyncio
|
| 261 |
|
|
|
|
| 292 |
if scene.location.path:
|
| 293 |
sfx_file = await download_file_from_url(scene.location.path)
|
| 294 |
if sfx_file:
|
| 295 |
+
sfx_file_wav = ensure_wav(sfx_file)
|
| 296 |
+
sfx_audio = AudioSegment.from_wav(sfx_file_wav)
|
| 297 |
scene_audio = scene_audio.overlay(sfx_audio)
|
| 298 |
os.remove(sfx_file)
|
| 299 |
else:
|
|
|
|
| 303 |
if scene.bgMusic and scene.bgMusic.musicPath:
|
| 304 |
bg_url = scene.bgMusic.musicPath
|
| 305 |
bg_file = await download_file_from_url(bg_url)
|
| 306 |
+
bg_file_wav = ensure_wav(bg_file)
|
| 307 |
+
bg_audio = AudioSegment.from_wav(bg_file_wav)
|
| 308 |
|
| 309 |
# Adjust volume
|
| 310 |
bg_audio = bg_audio - (1 - scene.bgMusic.volume) * 30 # approximate
|