Read a video/gif as an array of (f * h * w * 3). Args: input_path (str): input path. resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]], optional): resolution(height, width) of output. Defaults to None. start (int, optional): s
(
input_path: str,
resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None,
start: int = 0,
end: Optional[int] = None,
disable_log: bool = False,
)
| 256 | |
| 257 | |
| 258 | def video_to_array( |
| 259 | input_path: str, |
| 260 | resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None, |
| 261 | start: int = 0, |
| 262 | end: Optional[int] = None, |
| 263 | disable_log: bool = False, |
| 264 | ) -> np.ndarray: |
| 265 | """ |
| 266 | Read a video/gif as an array of (f * h * w * 3). |
| 267 | |
| 268 | Args: |
| 269 | input_path (str): input path. |
| 270 | resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]], |
| 271 | optional): resolution(height, width) of output. |
| 272 | Defaults to None. |
| 273 | start (int, optional): start frame index. Inclusive. |
| 274 | If < 0, will be converted to frame_index range in [0, frame_num]. |
| 275 | Defaults to 0. |
| 276 | end (int, optional): end frame index. Exclusive. |
| 277 | Could be positive int or negative int or None. |
| 278 | If None, all frames from start till the last frame are included. |
| 279 | Defaults to None. |
| 280 | disable_log (bool, optional): whether close the ffmepg command info. |
| 281 | Defaults to False. |
| 282 | |
| 283 | Raises: |
| 284 | FileNotFoundError: check the input path. |
| 285 | |
| 286 | Returns: |
| 287 | np.ndarray: shape will be (f * h * w * 3). |
| 288 | """ |
| 289 | check_input_path( |
| 290 | input_path, |
| 291 | allowed_suffix=['.mp4', 'mkv', 'avi', '.gif'], |
| 292 | tag='input video', |
| 293 | path_type='file') |
| 294 | |
| 295 | info = vid_info_reader(input_path) |
| 296 | if resolution: |
| 297 | height, width = resolution |
| 298 | else: |
| 299 | width, height = int(info['width']), int(info['height']) |
| 300 | num_frames = int(info['nb_frames']) |
| 301 | start = (min(start, num_frames - 1) + num_frames) % num_frames |
| 302 | end = (min(end, num_frames - 1) + |
| 303 | num_frames) % num_frames if end is not None else num_frames |
| 304 | command = [ |
| 305 | 'ffmpeg', |
| 306 | '-i', |
| 307 | input_path, |
| 308 | '-filter_complex', |
| 309 | f'[0]trim=start_frame={start}:end_frame={end}[v0]', |
| 310 | '-map', |
| 311 | '[v0]', |
| 312 | '-pix_fmt', |
| 313 | 'bgr24', # bgr24 for matching OpenCV |
| 314 | '-s', |
| 315 | f'{int(width)}x{int(height)}', |
no test coverage detected