Skip to content

Commit 8ee052e

Browse files
authored
Use mediapy (#219)
Signed-off-by: Kunjan patel <kunjanp@google.com>
1 parent fc46fcc commit 8ee052e

4 files changed

Lines changed: 13 additions & 64 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ flax>=0.10.2
99
optax>=0.2.3
1010
torch>=2.6.0
1111
torchvision>=0.20.1
12+
mediapy
1213
ftfy
1314
tensorboard>=2.17.0
1415
tensorboardx>=2.6.2.2

requirements_with_jax_ai_image.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
--extra-index-url https://download.pytorch.org/whl/cpu
44
jax>=0.6.2
55
jaxlib>=0.4.30
6+
mediapy
67
grain
78
google-cloud-storage>=2.17.0
89
absl-py

src/maxdiffusion/generate_wan.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
from typing import Sequence
1617
import jax
1718
import time
@@ -49,9 +50,11 @@ def run(config, pipeline=None, filename_prefix=""):
4950

5051
print("compile time: ", (time.perf_counter() - s0))
5152
saved_video_path = []
52-
for i in range(len(videos)):
53+
for i, video in enumerate(videos):
5354
video_path = f"{filename_prefix}wan_output_{config.seed}_{i}.mp4"
54-
export_to_video(videos[i], video_path, fps=config.fps)
55+
if os.path.exists(f"{config.base_output_dir}"):
56+
video_path = f"{config.base_output_dir}/{video_path}"
57+
export_to_video(video, video_path, fps=config.fps)
5558
saved_video_path.append(video_path)
5659

5760
s0 = time.perf_counter()

src/maxdiffusion/utils/export_utils.py

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from contextlib import contextmanager
66
from typing import List, Optional, Union
77

8+
import mediapy
89
import numpy as np
910

1011
import PIL.Image
@@ -144,65 +145,8 @@ def export_to_video(
144145
quality: float = 5.0,
145146
bitrate: Optional[int] = None,
146147
macro_block_size: Optional[int] = 16,
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
148+
):
149+
mediapy.write_video(path = output_video_path,
150+
images=video_frames,
151+
fps=fps,
152+
)

0 commit comments

Comments
 (0)