Skip to content
Merged
Show file tree
Hide file tree
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
20 changes: 19 additions & 1 deletion tests/multimodal/media/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from PIL import Image

from vllm.assets.base import get_vllm_public_assets
from vllm.assets.video import video_to_ndarrays, video_to_pil_images_list
from vllm.assets.video import (
video_get_metadata,
video_to_ndarrays,
video_to_pil_images_list,
)
from vllm.multimodal.media import ImageMediaIO, VideoMediaIO
from vllm.multimodal.video import VIDEO_LOADER_REGISTRY, VideoLoader

Expand Down Expand Up @@ -112,6 +116,20 @@ def test_opencv_video_io_colorspace(tmp_path, is_color: bool, fourcc: str, ext:
assert np.nanmean(sim) > 0.99


def test_opencv_video_metadata_matches_sampled_frame_timeline(tmp_path):
image_path = f"{tmp_path}/test_metadata_image.png"
Image.new("RGB", (8, 8), color=(255, 0, 0)).save(image_path)
video_path = f"{tmp_path}/test_metadata_video.mp4"
create_video_from_image(image_path, video_path, num_frames=10, fps=5.0)

metadata = video_get_metadata(video_path, num_frames=4)

assert metadata["fps"] == pytest.approx(5.0)
assert metadata["duration"] == pytest.approx(2.0)
assert metadata["frames_indices"] == [0, 3, 6, 9]
assert metadata["total_num_frames"] == 4


NUM_FRAMES = 10
FAKE_OUTPUT_1 = np.random.rand(NUM_FRAMES, 1280, 720, 3)
FAKE_OUTPUT_2 = np.random.rand(NUM_FRAMES, 1280, 720, 3)
Expand Down
11 changes: 8 additions & 3 deletions vllm/assets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from .base import get_cache_dir


def _sample_frame_indices(total_frames: int, num_frames: int) -> npt.NDArray:
return np.linspace(0, total_frames - 1, num_frames, dtype=int)


@lru_cache
def download_video_asset(filename: str) -> str:
"""
Expand Down Expand Up @@ -47,7 +51,7 @@ def video_to_ndarrays(path: str, num_frames: int = -1) -> npt.NDArray:
frames = []

num_frames = num_frames if num_frames > 0 else total_frames
frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)
frame_indices = _sample_frame_indices(total_frames, num_frames)
for idx in range(total_frames):
ok = cap.grab() # next img
if not ok:
Expand Down Expand Up @@ -86,13 +90,14 @@ def video_get_metadata(path: str, num_frames: int = -1) -> dict[str, Any]:

if num_frames == -1 or num_frames > total_frames:
num_frames = total_frames
frame_indices = _sample_frame_indices(total_frames, num_frames)

metadata = {
"total_num_frames": num_frames,
"fps": duration / num_frames,
"fps": fps,
"duration": duration,
"video_backend": "opencv",
"frames_indices": list(range(num_frames)),
"frames_indices": frame_indices.tolist(),
# extra field used to control hf processor's video
# sampling behavior
"do_sample_frames": num_frames == total_frames,
Expand Down
Loading