|
3 | 3 | import struct |
4 | 4 | import tempfile |
5 | 5 | from contextlib import contextmanager |
6 | | -from typing import List, Union |
| 6 | +from typing import List, Optional, Union |
7 | 7 |
|
8 | 8 | import numpy as np |
9 | 9 | import PIL.Image |
@@ -139,8 +139,34 @@ def _legacy_export_to_video( |
139 | 139 |
|
140 | 140 |
|
141 | 141 | def export_to_video( |
142 | | - video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], output_video_path: str = None, fps: int = 10 |
| 142 | + video_frames: Union[List[np.ndarray], List[PIL.Image.Image]], |
| 143 | + output_video_path: str = None, |
| 144 | + fps: int = 10, |
| 145 | + quality: float = 5.0, |
| 146 | + bitrate: Optional[int] = None, |
| 147 | + macro_block_size: Optional[int] = 16, |
143 | 148 | ) -> str: |
| 149 | + """ |
| 150 | + quality: |
| 151 | + Video output quality. Default is 5. Uses variable bit rate. |
| 152 | + Highest quality is 10, lowest is 0. |
| 153 | + Set to None to prevent variable bitrate flags to FFMPEG so you can manually specify them using output_params instead. |
| 154 | + Specifying a fixed bitrate using `bitrate` disables this parameter. |
| 155 | +
|
| 156 | + bitrate: |
| 157 | + Set a constant bitrate for the video encoding. |
| 158 | + Default is None causing `quality` parameter to be used instead. |
| 159 | + Better quality videos with smaller file sizes will result from using the `quality` variable bitrate parameter rather |
| 160 | + than specifiying a fixed bitrate with this parameter. |
| 161 | +
|
| 162 | + macro_block_size: |
| 163 | + Size constraint for video. |
| 164 | + Width and height, must be divisible by this number. |
| 165 | + If not divisible by this number imageio will tell ffmpeg to scale the image up to the next closest size divisible by this number. |
| 166 | + Most codecs are compatible with a macroblock size of 16 (default), some can go smaller (4, 8). |
| 167 | + To disable this automatic feature set it to None or 1, however be warned many players can't decode videos that are odd in size and |
| 168 | + some codecs will produce poor results or fail. See https://en.wikipedia.org/wiki/Macroblock. |
| 169 | + """ |
144 | 170 | # TODO: Dhruv. Remove by Diffusers release 0.33.0 |
145 | 171 | # Added to prevent breaking existing code |
146 | 172 | if not is_imageio_available(): |
@@ -177,7 +203,9 @@ def export_to_video( |
177 | 203 | elif isinstance(video_frames[0], PIL.Image.Image): |
178 | 204 | video_frames = [np.array(frame) for frame in video_frames] |
179 | 205 |
|
180 | | - with imageio.get_writer(output_video_path, fps=fps) as writer: |
| 206 | + with imageio.get_writer( |
| 207 | + output_video_path, fps=fps, quality=quality, bitrate=bitrate, macro_block_size=macro_block_size |
| 208 | + ) as writer: |
181 | 209 | for frame in video_frames: |
182 | 210 | writer.append_data(frame) |
183 | 211 |
|
|
0 commit comments