Convert an array to a video directly, gif not supported. Args: image_array (np.ndarray): shape should be (f * h * w * 3). output_path (str): output video file path. fps (Union[int, float, optional): fps. Defaults to 30. resolution (Optional[Union[Tuple[int, int],
(
image_array: np.ndarray,
output_path: str,
fps: Union[int, float] = 30,
resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None,
disable_log: bool = False,
)
| 87 | |
| 88 | |
| 89 | def array_to_video( |
| 90 | image_array: np.ndarray, |
| 91 | output_path: str, |
| 92 | fps: Union[int, float] = 30, |
| 93 | resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None, |
| 94 | disable_log: bool = False, |
| 95 | ) -> None: |
| 96 | """Convert an array to a video directly, gif not supported. |
| 97 | |
| 98 | Args: |
| 99 | image_array (np.ndarray): shape should be (f * h * w * 3). |
| 100 | output_path (str): output video file path. |
| 101 | fps (Union[int, float, optional): fps. Defaults to 30. |
| 102 | resolution (Optional[Union[Tuple[int, int], Tuple[float, float]]], |
| 103 | optional): (height, width) of the output video. |
| 104 | Defaults to None. |
| 105 | disable_log (bool, optional): whether close the ffmepg command info. |
| 106 | Defaults to False. |
| 107 | Raises: |
| 108 | FileNotFoundError: check output path. |
| 109 | TypeError: check input array. |
| 110 | |
| 111 | Returns: |
| 112 | None. |
| 113 | """ |
| 114 | if not isinstance(image_array, np.ndarray): |
| 115 | raise TypeError('Input should be np.ndarray.') |
| 116 | assert image_array.ndim == 4 |
| 117 | assert image_array.shape[-1] == 3 |
| 118 | prepare_output_path( |
| 119 | output_path, |
| 120 | allowed_suffix=['.mp4'], |
| 121 | tag='output video', |
| 122 | path_type='file', |
| 123 | overwrite=True) |
| 124 | if resolution: |
| 125 | height, width = resolution |
| 126 | width += width % 2 |
| 127 | height += height % 2 |
| 128 | else: |
| 129 | image_array = pad_for_libx264(image_array) |
| 130 | height, width = image_array.shape[1], image_array.shape[2] |
| 131 | command = [ |
| 132 | 'ffmpeg', |
| 133 | '-y', # (optional) overwrite output file if it exists |
| 134 | '-f', |
| 135 | 'rawvideo', |
| 136 | '-s', |
| 137 | f'{int(width)}x{int(height)}', # size of one frame |
| 138 | '-pix_fmt', |
| 139 | 'bgr24', |
| 140 | '-r', |
| 141 | f'{fps}', # frames per second |
| 142 | '-loglevel', |
| 143 | 'error', |
| 144 | '-threads', |
| 145 | '4', |
| 146 | '' |
nothing calls this directly
no test coverage detected