Skip to content

Commit

Permalink
Download audio files that look like URLs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rewbs committed Nov 9, 2023
1 parent f0fb9d7 commit f910d7b
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions scripts/deforum_helpers/video_audio_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import requests
import subprocess
import time
import tempfile
import re
import glob
import concurrent.futures
from pathlib import Path
from pkg_resources import resource_filename
from modules.shared import state, opts
from .general_utils import checksum, clean_gradio_path_strings, debug_print
Expand Down Expand Up @@ -236,6 +238,20 @@ def ffmpeg_stitch_video(ffmpeg_location=None, fps=None, outmp4_path=None, stitch
if add_soundtrack != 'None':
try:
audio_path = clean_gradio_path_strings(audio_path)
if (audio_path.startswith('http://') or audio_path.startswith('https://')):
url = audio_path
print(f"Downloading audio file from: {url}")
response = requests.get(url, stream=True)
response.raise_for_status()
temp_file = tempfile.NamedTemporaryFile(delete=False)
# Write the content of the downloaded file into the temporary file
with open(temp_file.name, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
audio_path = temp_file.name
print(f"Audio saved to: {audio_path}")

audio_add_start_time = time.time()
cmd = [
ffmpeg_location,
Expand All @@ -259,6 +275,9 @@ def ffmpeg_stitch_video(ffmpeg_location=None, fps=None, outmp4_path=None, stitch
except Exception as e:
add_soundtrack_status = f"\rError adding audio to video: {e}"
add_soundtrack_success = False
finally:
file_path = Path(temp_file.name)
file_path.unlink(missing_ok=True)

add_srt = opts.data.get("deforum_save_gen_info_as_srt", False) and opts.data.get("deforum_embed_srt", False) and srt_path is not None
add_srt_status = None
Expand Down

0 comments on commit f910d7b

Please sign in to comment.