(
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,
lossless: bool = True,
)
| 85 | |
| 86 | |
| 87 | def array_to_video( |
| 88 | image_array: np.ndarray, |
| 89 | output_path: str, |
| 90 | fps: Union[int, float] = 30, |
| 91 | resolution: Optional[Union[Tuple[int, int], Tuple[float, float]]] = None, |
| 92 | disable_log: bool = False, |
| 93 | lossless: bool = True, |
| 94 | ) -> None: |
| 95 | if not isinstance(image_array, np.ndarray): |
| 96 | raise TypeError("Input should be np.ndarray.") |
| 97 | assert image_array.ndim == 4 |
| 98 | assert image_array.shape[-1] == 3 |
| 99 | if resolution: |
| 100 | height, width = resolution |
| 101 | width += width % 2 |
| 102 | height += height % 2 |
| 103 | else: |
| 104 | image_array = pad_for_libx264(image_array) |
| 105 | height, width = image_array.shape[1], image_array.shape[2] |
| 106 | if lossless: |
| 107 | command = [ |
| 108 | "ffmpeg", |
| 109 | "-y", # (optional) overwrite output file if it exists |
| 110 | "-f", |
| 111 | "rawvideo", |
| 112 | "-s", |
| 113 | f"{int(width)}x{int(height)}", # size of one frame |
| 114 | "-pix_fmt", |
| 115 | "bgr24", |
| 116 | "-r", |
| 117 | f"{fps}", # frames per second |
| 118 | "-loglevel", |
| 119 | "error", |
| 120 | "-threads", |
| 121 | "4", |
| 122 | "-i", |
| 123 | "-", # The input comes from a pipe |
| 124 | "-vcodec", |
| 125 | "libx264rgb", |
| 126 | "-crf", |
| 127 | "0", |
| 128 | "-an", # Tells FFMPEG not to expect any audio |
| 129 | output_path, |
| 130 | ] |
| 131 | else: |
| 132 | command = [ |
| 133 | "ffmpeg", |
| 134 | "-y", # (optional) overwrite output file if it exists |
| 135 | "-f", |
| 136 | "rawvideo", |
| 137 | "-s", |
| 138 | f"{int(width)}x{int(height)}", # size of one frame |
| 139 | "-pix_fmt", |
| 140 | "bgr24", |
| 141 | "-r", |
| 142 | f"{fps}", # frames per second |
| 143 | "-loglevel", |
| 144 | "error", |
no test coverage detected