Spaces:
Sleeping
Sleeping
File size: 14,535 Bytes
165c203 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
#!/usr/bin/env python3
# zoom_video_composer.py v0.2.1
# https://github.com/mwydmuch/ZoomVideoComposer
# Copyright (c) 2023 Marek Wydmuch
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import click
from PIL import Image
import os
import shutil
from hashlib import md5
from multiprocessing import cpu_count
from joblib import Parallel, delayed
from tqdm import trange
from math import log, ceil, pow, sin, cos, pi
from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
from moviepy.editor import VideoFileClip, AudioFileClip
from concurrent.futures import ThreadPoolExecutor
import concurrent
from tqdm import tqdm
EASING_FUNCTIONS = {
"linear": lambda x: x,
"easeInSine": lambda x: 1 - cos((x * pi) / 2),
"easeOutSine": lambda x: sin((x * pi) / 2),
"easeInOutSine": lambda x: -(cos(pi * x) - 1) / 2,
"easeInQuad": lambda x: x * x,
"easeOutQuad": lambda x: 1 - (1 - x) * (1 - x),
"easeInOutQuad": lambda x: 2 * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 2) / 2,
"easeInCubic": lambda x: x * x * x,
"easeOutCubic": lambda x: 1 - pow(1 - x, 3),
"easeInOutCubic": lambda x: 4 * x * x * x
if x < 0.5
else 1 - pow(-2 * x + 2, 3) / 2,
}
DEFAULT_EASING_KEY = "easeInOutSine"
DEFAULT_EASING_FUNCTION = EASING_FUNCTIONS[DEFAULT_EASING_KEY]
RESAMPLING_FUNCTIONS = {
"nearest": Image.Resampling.NEAREST,
"box": Image.Resampling.BOX,
"bilinear": Image.Resampling.BILINEAR,
"hamming": Image.Resampling.HAMMING,
"bicubic": Image.Resampling.BICUBIC,
"lanczos": Image.Resampling.LANCZOS,
}
DEFAULT_RESAMPLING_KEY = "lanczos"
DEFAULT_RESAMPLING_FUNCTION = RESAMPLING_FUNCTIONS[DEFAULT_RESAMPLING_KEY]
def zoom_crop(image, zoom, resampling_func=Image.Resampling.LANCZOS):
width, height = image.size
zoom_size = (int(width * zoom), int(height * zoom))
crop_box = (
(zoom_size[0] - width) / 2,
(zoom_size[1] - height) / 2,
(zoom_size[0] + width) / 2,
(zoom_size[1] + height) / 2,
)
return image.resize(zoom_size, resampling_func).crop(crop_box)
def resize_scale(image, scale, resampling_func=Image.Resampling.LANCZOS):
width, height = image.size
return image.resize((int(width * scale), int(height * scale)), resampling_func)
def zoom_in_log(easing_func, i, num_frames, num_images):
return (easing_func(i / (num_frames - 1))) * num_images
def zoom_out_log(easing_func, i, num_frames, num_images):
return (1 - easing_func(i / (num_frames - 1))) * num_images
def zoom_in(zoom, easing_func, i, num_frames, num_images):
return zoom ** zoom_in_log(easing_func, i, num_frames, num_images)
def zoom_out(zoom, easing_func, i, num_frames, num_images):
return zoom ** zoom_out_log(easing_func, i, num_frames, num_images)
def get_px_or_fraction(value, reference_value):
if value <= 1:
value = reference_value * value
return int(value)
@click.command()
@click.argument(
"image_paths",
nargs=-1,
type=click.Path(exists=True),
required=True,
)
@click.option(
"-a",
"--audio_path",
type=click.Path(exists=True, dir_okay=False),
default=None,
help="Audio file path that will be added to the video.",
)
@click.option(
"-z",
"--zoom",
type=float,
default=2.0,
help="Zoom factor/ratio between images.",
show_default=True,
)
@click.option(
"-d",
"--duration",
type=float,
default=10.0,
help="Duration of the video in seconds.",
show_default=True,
)
@click.option(
"-e",
"--easing",
type=click.Choice(list(EASING_FUNCTIONS.keys())),
default=DEFAULT_EASING_KEY,
help="Easing function.",
show_default=True,
)
@click.option(
"-r",
"--direction",
type=click.Choice(["in", "out", "inout", "outin"]),
default="out",
help="Zoom direction. Inout and outin combine both directions.",
show_default=True,
)
@click.option(
"-o",
"--output",
type=click.Path(),
default="output.mp4",
help="Output video file.",
show_default=True,
)
@click.option(
"-t",
"--threads",
type=int,
default=-1,
help="Number of threads to use to generate frames. Use values <= 0 for number of available threads on your machine minus the provided absolute value.",
show_default=True,
)
@click.option(
"--tmp-dir",
type=click.Path(),
default="tmp",
help="Temporary directory to store frames.",
show_default=True,
)
@click.option(
"-f",
"--fps",
type=int,
default=30,
help="Frames per second of the output video.",
show_default=True,
)
@click.option(
"-w",
"--width",
type=float,
default=1,
help="Width of the output video. Values > 1 are interpreted as specific sizes in pixels. Values <= 1 are interpreted as a fraction of the width of the first image.",
show_default=True,
)
@click.option(
"-h",
"--height",
type=float,
default=1,
help="Height of the output video. Values > 1 are interpreted as specific sizes in pixels. Values <= 1 are interpreted as a fraction of the height of the first image.",
show_default=True,
)
@click.option(
"-s",
"--resampling",
type=click.Choice(list(RESAMPLING_FUNCTIONS.keys())),
default=DEFAULT_RESAMPLING_KEY,
help="Resampling techique to use when resizing images.",
show_default=True,
)
@click.option(
"-m",
"--margin",
type=float,
default=0.05,
help="Size of the margin to cut from the edges of each image for better blending with the next/previous image. Values > 1 are interpreted as specific sizes in pixels. Values <= 1 are interpreted as a fraction of the smaller size of the first image.",
show_default=True,
)
@click.option(
"--keep-frames",
is_flag=True,
default=False,
help="Keep frames in the temporary directory. Otherwise, it will be deleted after the video is generated.",
show_default=True,
)
@click.option(
"--skip-video-generation",
is_flag=True,
default=False,
help="Skip video generation. Useful if you only want to generate the frames. This option will keep the temporary directory similar to --keep-frames flag.",
show_default=True,
)
@click.option(
"--reverse-images",
is_flag=True,
default=False,
help="Reverse the order of the images.",
show_default=True,
)
def zoom_video_composer(
image_paths,
audio_path=None,
zoom=2.0,
duration=10.0,
easing=DEFAULT_EASING_KEY,
direction="out",
output="output.mp4",
threads=-1,
tmp_dir="tmp",
fps=30,
width=1,
height=1,
resampling=DEFAULT_RESAMPLING_KEY,
margin=0.05,
keep_frames=False,
skip_video_generation=False,
reverse_images=False,
):
"""Compose a zoom video from multiple provided images."""
# Read images
_image_paths = []
for image_path in image_paths:
if os.path.isfile(image_path):
_image_paths.append(image_path)
elif os.path.isdir(image_path):
for subimage_path in sorted(os.listdir(image_path)):
_image_paths.append(os.path.join(image_path, subimage_path))
image_paths = _image_paths
images = []
click.echo(f"Reading {len(image_paths)} image files ...")
for image_path in image_paths:
if not image_path.lower().endswith((".png", ".jpg", ".jpeg", ".webp")):
click.echo(f"Unsupported file type: {image_path}, skipping")
continue
image = Image.open(image_path)
images.append(image)
if len(images) < 2:
raise ValueError("At least two images are required to create a zoom video")
# Setup some additional variables
easing_func = EASING_FUNCTIONS.get(easing, None)
if easing_func is None:
raise ValueError(f"Unsupported easing function: {easing}")
resampling_func = RESAMPLING_FUNCTIONS.get(resampling, None)
if resampling_func is None:
raise ValueError(f"Unsupported resampling function: {resampling}")
num_images = len(images) - 1
num_frames = int(duration * fps)
num_frames_half = int(num_frames / 2)
tmp_dir_hash = os.path.join(
tmp_dir, md5("".join(image_paths).encode("utf-8")).hexdigest()
)
width = get_px_or_fraction(width, images[0].width)
height = get_px_or_fraction(height, images[0].height)
margin = get_px_or_fraction(margin, min(images[0].width, images[0].height))
# Create tmp dir
if not os.path.exists(tmp_dir_hash):
click.echo(f"Creating temporary directory for frames: {tmp_dir} ...")
os.makedirs(tmp_dir_hash, exist_ok=True)
if direction in ["out", "outin"]:
images.reverse()
if reverse_images:
images.reverse()
# Blend images (take care of margins)
click.echo(f"Blending {len(images)} images ...")
for i in trange(1, num_images + 1):
inner_image = images[i]
outer_image = images[i - 1]
inner_image = inner_image.crop(
(margin, margin, inner_image.width - margin, inner_image.height - margin)
)
# Some coloring for debugging purposes
# debug_colors = ['red', 'green', 'blue', 'yellow', 'cyan', 'magenta']
# layer = Image.new('RGB', inner_image.size, debug_colors[i % 6])
# inner_image = Image.blend(inner_image, layer, 0.25)
image = zoom_crop(outer_image, zoom, resampling_func)
image.paste(inner_image, (margin, margin))
images[i] = image
# Save image for debugging purposes
# image_path = os.path.join(tmp_dir_hash, f"_blending_step_1_{i:06d}.png")
# image.save(image_path)
images_resized = [resize_scale(i, zoom, resampling_func) for i in images]
for i in trange(num_images, 0, -1):
inner_image = images_resized[i]
image = images_resized[i - 1]
inner_image = resize_scale(inner_image, 1.0 / zoom, resampling_func)
# Some coloring for debugging purposes
# debug_colors = ['red', 'green', 'blue', 'yellow', 'cyan', 'magenta']
# layer = Image.new('RGB', inner_image.size, debug_colors[i % 6])
# inner_image = Image.blend(inner_image, layer, 0.25)
image.paste(
inner_image,
(
int((image.width - inner_image.width) / 2),
int((image.height - inner_image.height) / 2),
),
)
images_resized[i] = image
# Save image for debugging purposes
# image_path = os.path.join(tmp_dir_hash, f"_blending_step_2_{i:06d}.png")
# image.save(image_path)
images = images_resized
# Create frames
def process_frame(i): # to improve
if direction == "in":
current_zoom_log = zoom_in_log(easing_func, i, num_frames, num_images)
elif direction == "out":
current_zoom_log = zoom_out_log(easing_func, i, num_frames, num_images)
elif direction == "inout":
if i < num_frames_half:
current_zoom_log = zoom_in_log(
easing_func, i, num_frames_half, num_images
)
else:
current_zoom_log = zoom_out_log(
easing_func, i - num_frames_half, num_frames_half, num_images
)
elif direction == "outin":
if i < num_frames_half:
current_zoom_log = zoom_out_log(
easing_func, i, num_frames_half, num_images
)
else:
current_zoom_log = zoom_in_log(
easing_func, i - num_frames_half, num_frames_half, num_images
)
else:
raise ValueError(f"Unsupported direction: {direction}")
current_image_idx = ceil(current_zoom_log)
local_zoom = zoom ** (current_zoom_log - current_image_idx + 1)
if current_zoom_log == 0.0:
frame = images[0]
else:
frame = images[current_image_idx]
frame = zoom_crop(frame, local_zoom, resampling_func)
frame = frame.resize((width, height), resampling_func)
frame_path = os.path.join(tmp_dir_hash, f"{i:06d}.png")
frame.save(frame_path)
n_jobs = threads if threads > 0 else cpu_count() - threads
click.echo(f"Creating frames in {n_jobs} threads ...")
with ThreadPoolExecutor(max_workers=n_jobs) as executor:
futures = [executor.submit(process_frame, i) for i in range(num_frames)]
for _ in tqdm(concurrent.futures.as_completed(futures), total=num_frames):
pass
# Write video
click.echo(f"Writing video to: {output} ...")
image_files = [
os.path.join(tmp_dir_hash, f"{i:06d}.png") for i in range(num_frames)
]
video_clip = ImageSequenceClip(image_files, fps=fps)
video_write_kwargs = {"codec": "libx264"}
# Add audio
if audio_path:
click.echo(f"Adding audio from: {audio_path} ...")
audio_clip = AudioFileClip(audio_path)
audio_clip = audio_clip.subclip(0, video_clip.end)
video_clip = video_clip.set_audio(audio_clip)
video_write_kwargs["audio_codec"] = "aac"
video_clip.write_videofile(output, **video_write_kwargs)
# Remove tmp dir
if not keep_frames and not skip_video_generation:
shutil.rmtree(tmp_dir_hash, ignore_errors=False, onerror=None)
if not os.listdir(tmp_dir):
click.echo(f"Removing empty temporary directory for frames: {tmp_dir} ...")
os.rmdir(tmp_dir)
click.echo("Done!")
if __name__ == "__main__":
zoom_video_composer()
|