Skip to content

Commit f31d659

Browse files
authored
Revert "Use mediapy (#219)" (#224)
This reverts commit 8ee052e.
1 parent 8ee052e commit f31d659

4 files changed

Lines changed: 64 additions & 13 deletions

File tree

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ flax>=0.10.2
99
optax>=0.2.3
1010
torch>=2.6.0
1111
torchvision>=0.20.1
12-
mediapy
1312
ftfy
1413
tensorboard>=2.17.0
1514
tensorboardx>=2.6.2.2

requirements_with_jax_ai_image.txt

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

src/maxdiffusion/generate_wan.py

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

15-
import os
1615
from typing import Sequence
1716
import jax
1817
import time
@@ -50,11 +49,9 @@ def run(config, pipeline=None, filename_prefix=""):
5049

5150
print("compile time: ", (time.perf_counter() - s0))
5251
saved_video_path = []
53-
for i, video in enumerate(videos):
52+
for i in range(len(videos)):
5453
video_path = f"{filename_prefix}wan_output_{config.seed}_{i}.mp4"
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)
54+
export_to_video(videos[i], video_path, fps=config.fps)
5855
saved_video_path.append(video_path)
5956

6057
s0 = time.perf_counter()

src/maxdiffusion/utils/export_utils.py

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

8-
import mediapy
98
import numpy as np
109

1110
import 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

Comments
 (0)