Skip to content

Small improvements for video loading #9183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/diffusers/utils/loading_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import tempfile
from typing import Callable, List, Optional, Union
from urllib.parse import unquote, urlparse

import PIL.Image
import PIL.ImageOps
Expand Down Expand Up @@ -80,12 +81,22 @@ def load_video(
)

if is_url:
video_data = requests.get(video, stream=True).raw
suffix = os.path.splitext(video)[1] or ".mp4"
response = requests.get(video, stream=True)
if response.status_code != 200:
raise ValueError(f"Failed to download video. Status code: {response.status_code}")

parsed_url = urlparse(video)
file_name = os.path.basename(unquote(parsed_url.path))

suffix = os.path.splitext(file_name)[1] or ".mp4"
video_path = tempfile.NamedTemporaryFile(suffix=suffix, delete=False).name

was_tempfile_created = True

video_data = response.iter_content(chunk_size=8192)
with open(video_path, "wb") as f:
f.write(video_data.read())
for chunk in video_data:
f.write(chunk)

video = video_path

Expand Down
Loading