From c2d2ec96b2bc3672d43a9ed5268282f191986730 Mon Sep 17 00:00:00 2001 From: "jason.mei" Date: Mon, 29 Dec 2025 18:11:04 +0800 Subject: [PATCH] feat(vidu,wan_ai): add video dimension extraction with av library - Add `get_dimensions()` method to VideoFromFile fallback class across all vidu and wan_ai modules - Implement PyAV (av) library integration to extract video width and height from container streams - Add dimension caching with `_width` and `_height` attributes to avoid redundant parsing - Implement fallback dimensions (1280x720) when video parsing fails - Add proper stream seeking to reset file position after dimension extraction - Update requirements.txt to include av library dependency - Ensure consistent video dimension handling across vidu_t2v, vidu_i2v, vidu_ref2v, vidu_startend2v, vidu_extend, wan_ai_t2v, and wan_ai_i2v modules --- py/vidu_extend.py | 18 ++++++++++++++++++ py/vidu_i2v.py | 18 ++++++++++++++++++ py/vidu_ref2v.py | 18 ++++++++++++++++++ py/vidu_startend2v.py | 18 ++++++++++++++++++ py/vidu_t2v.py | 18 ++++++++++++++++++ py/wan_ai_i2v.py | 18 ++++++++++++++++++ py/wan_ai_t2v.py | 19 ++++++++++++++++++- requirements.txt | 3 ++- 8 files changed, 128 insertions(+), 2 deletions(-) diff --git a/py/vidu_extend.py b/py/vidu_extend.py index 9e094c0..1a8e67f 100644 --- a/py/vidu_extend.py +++ b/py/vidu_extend.py @@ -15,10 +15,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) MODELS = ["viduq2-pro", "viduq2-turbo"] diff --git a/py/vidu_i2v.py b/py/vidu_i2v.py index 5afe8dc..ad56ec3 100644 --- a/py/vidu_i2v.py +++ b/py/vidu_i2v.py @@ -15,10 +15,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) MODELS = ["viduq2-pro", "viduq2-turbo"] diff --git a/py/vidu_ref2v.py b/py/vidu_ref2v.py index e39ddea..fffd9af 100644 --- a/py/vidu_ref2v.py +++ b/py/vidu_ref2v.py @@ -16,10 +16,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) ASPECT_RATIOS = ["16:9", "9:16", "3:4", "4:3", "1:1"] diff --git a/py/vidu_startend2v.py b/py/vidu_startend2v.py index 3a1ff38..38c7a50 100644 --- a/py/vidu_startend2v.py +++ b/py/vidu_startend2v.py @@ -15,10 +15,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) MODELS = ["viduq2-pro", "viduq2-turbo"] diff --git a/py/vidu_t2v.py b/py/vidu_t2v.py index 3bc1e03..db302cc 100644 --- a/py/vidu_t2v.py +++ b/py/vidu_t2v.py @@ -14,10 +14,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) ASPECT_RATIOS = ["16:9", "9:16", "3:4", "4:3", "1:1"] diff --git a/py/wan_ai_i2v.py b/py/wan_ai_i2v.py index 3d685db..489801a 100644 --- a/py/wan_ai_i2v.py +++ b/py/wan_ai_i2v.py @@ -11,10 +11,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) class Modelverse_WanAII2V: def __init__(self): diff --git a/py/wan_ai_t2v.py b/py/wan_ai_t2v.py index d063e94..27c94db 100644 --- a/py/wan_ai_t2v.py +++ b/py/wan_ai_t2v.py @@ -11,11 +11,28 @@ except ImportError: try: from comfy.model_management import VideoFromFile except ImportError: - # Fallback class when VideoFromFile is not available + import av + class VideoFromFile: def __init__(self, video_io): self.video_io = video_io self.video_data = video_io.getvalue() if hasattr(video_io, 'getvalue') else video_io + self._width = None + self._height = None + + def get_dimensions(self): + if self._width is None: + self.video_io.seek(0) + try: + container = av.open(self.video_io) + stream = container.streams.video[0] + self._width = stream.width + self._height = stream.height + container.close() + except: + self._width, self._height = 1280, 720 + self.video_io.seek(0) + return (self._width, self._height) class Modelverse_WanAIT2V: def __init__(self): diff --git a/requirements.txt b/requirements.txt index 2cbf9ad..5add0cd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ requests >= 2.32.4 pillow >= 11 -openai >= 1.107.0 \ No newline at end of file +openai >= 1.107.0 +GitPython==3.1.45 \ No newline at end of file