1
0
mirror of https://github.com/huggingface/diffusers.git synced 2026-01-29 07:22:12 +03:00

Small improvements for video loading (#9183)

* update

* update
This commit is contained in:
Dhruv Nair
2024-08-16 09:36:58 +05:30
committed by GitHub
parent 1a92bc05a7
commit 3e46043223

View File

@@ -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
@@ -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