55from contextlib import contextmanager
66from typing import List , Optional , Union
77
8- import mediapy
98import numpy as np
109
1110import PIL .Image
@@ -145,8 +144,65 @@ def export_to_video(
145144 quality : float = 5.0 ,
146145 bitrate : Optional [int ] = None ,
147146 macro_block_size : Optional [int ] = 16 ,
148- ):
149- mediapy .write_video (path = output_video_path ,
150- images = video_frames ,
151- fps = fps ,
152- )
147+ ) -> str :
148+ """
149+ quality:
150+ Video output quality. Default is 5. Uses variable bit rate. Highest quality is 10, lowest is 0. Set to None to
151+ prevent variable bitrate flags to FFMPEG so you can manually specify them using output_params instead.
152+ Specifying a fixed bitrate using `bitrate` disables this parameter.
153+
154+ bitrate:
155+ Set a constant bitrate for the video encoding. Default is None causing `quality` parameter to be used instead.
156+ Better quality videos with smaller file sizes will result from using the `quality` variable bitrate parameter
157+ rather than specifiying a fixed bitrate with this parameter.
158+
159+ macro_block_size:
160+ Size constraint for video. Width and height, must be divisible by this number. If not divisible by this number
161+ imageio will tell ffmpeg to scale the image up to the next closest size divisible by this number. Most codecs
162+ are compatible with a macroblock size of 16 (default), some can go smaller (4, 8). To disable this automatic
163+ feature set it to None or 1, however be warned many players can't decode videos that are odd in size and some
164+ codecs will produce poor results or fail. See https://en.wikipedia.org/wiki/Macroblock.
165+ """
166+ # TODO: Dhruv. Remove by Diffusers release 0.33.0
167+ # Added to prevent breaking existing code
168+ if not is_imageio_available ():
169+ logger .warning (
170+ (
171+ "It is recommended to use `export_to_video` with `imageio` and `imageio-ffmpeg` as a backend. \n "
172+ "These libraries are not present in your environment. Attempting to use legacy OpenCV backend to export video. \n "
173+ "Support for the OpenCV backend will be deprecated in a future Diffusers version"
174+ )
175+ )
176+ return _legacy_export_to_video (video_frames , output_video_path , fps )
177+
178+ if is_imageio_available ():
179+ import imageio
180+ else :
181+ raise ImportError (BACKENDS_MAPPING ["imageio" ][1 ].format ("export_to_video" ))
182+
183+ try :
184+ imageio .plugins .ffmpeg .get_exe ()
185+ except AttributeError :
186+ raise AttributeError (
187+ (
188+ "Found an existing imageio backend in your environment. Attempting to export video with imageio. \n "
189+ "Unable to find a compatible ffmpeg installation in your environment to use with imageio. Please install via `pip install imageio-ffmpeg"
190+ )
191+ )
192+
193+ if output_video_path is None :
194+ output_video_path = tempfile .NamedTemporaryFile (suffix = ".mp4" ).name
195+
196+ if isinstance (video_frames [0 ], np .ndarray ):
197+ video_frames = [(frame * 255 ).astype (np .uint8 ) for frame in video_frames ]
198+
199+ elif isinstance (video_frames [0 ], PIL .Image .Image ):
200+ video_frames = [np .array (frame ) for frame in video_frames ]
201+
202+ with imageio .get_writer (
203+ output_video_path , fps = fps , quality = quality , bitrate = bitrate , macro_block_size = macro_block_size
204+ ) as writer :
205+ for frame in video_frames :
206+ writer .append_data (frame )
207+
208+ return output_video_path
0 commit comments