MCPcopy Index your code
hub / github.com/modelscope/DiffSynth-Studio / VideoData

Class VideoData

diffsynth/data/video.py:83–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81
82
83class VideoData:
84 def __init__(self, video_file=None, image_folder=None, height=None, width=None, **kwargs):
85 if video_file is not None:
86 self.data_type = "video"
87 self.data = LowMemoryVideo(video_file, **kwargs)
88 elif image_folder is not None:
89 self.data_type = "images"
90 self.data = LowMemoryImageFolder(image_folder, **kwargs)
91 else:
92 raise ValueError("Cannot open video or image folder")
93 self.length = None
94 self.set_shape(height, width)
95
96 def raw_data(self):
97 frames = []
98 for i in range(self.__len__()):
99 frames.append(self.__getitem__(i))
100 return frames
101
102 def set_length(self, length):
103 self.length = length
104
105 def set_shape(self, height, width):
106 self.height = height
107 self.width = width
108
109 def __len__(self):
110 if self.length is None:
111 return len(self.data)
112 else:
113 return self.length
114
115 def shape(self):
116 if self.height is not None and self.width is not None:
117 return self.height, self.width
118 else:
119 height, width, _ = self.__getitem__(0).shape
120 return height, width
121
122 def __getitem__(self, item):
123 frame = self.data.__getitem__(item)
124 width, height = frame.size
125 if self.height is not None and self.width is not None:
126 if self.height != height or self.width != width:
127 frame = crop_and_resize(frame, self.height, self.width)
128 return frame
129
130 def __del__(self):
131 pass
132
133 def save_images(self, folder):
134 os.makedirs(folder, exist_ok=True)
135 for i in tqdm(range(self.__len__()), desc="Saving images"):
136 frame = self.__getitem__(i)
137 frame.save(os.path.join(folder, f"{i}.png"))
138
139
140def save_video(frames, save_path, fps, quality=9, ffmpeg_params=None):

Calls

no outgoing calls

Tested by

no test coverage detected