Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Misc] Improve gif output compression rate #6289

Merged
merged 5 commits into from
Nov 4, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions python/taichi/tools/video.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import shutil

from taichi._lib.utils import get_os_name
from taichi.tools.image import imwrite

FRAME_FN_TEMPLATE = '%06d.png'
Expand Down Expand Up @@ -32,25 +31,13 @@ def get_ffmpeg_path():
return 'ffmpeg'


def mp4_to_gif(input_fn, output_fn, framerate):
# Generate the palette
palette_name = 'palette.png'
if get_os_name() == 'win':
command = get_ffmpeg_path(
) + f" -loglevel panic -i {input_fn} -vf 'palettegen' -y {palette_name}"
else:
command = get_ffmpeg_path(
) + f" -loglevel panic -i {input_fn} -vf 'fps={framerate}," \
f"scale=320:640:flags=lanczos,palettegen' -y {palette_name}"
# print command
os.system(command)

# Generate the GIF
command = get_ffmpeg_path(
) + f" -loglevel panic -i {input_fn} -i {palette_name} -lavfi paletteuse -y {output_fn}"
# print command
os.system(command)
os.remove(palette_name)
def mp4_to_gif(input_fn, output_fn, framerate, **kwargs):
from moviepy.editor import \
VideoFileClip # pylint: disable=import-outside-toplevel
neozhaoliang marked this conversation as resolved.
Show resolved Hide resolved

clip = VideoFileClip(input_fn)
prog = get_ffmpeg_path()
clip.write_gif(output_fn, fps=framerate, program=prog, **kwargs)


class VideoManager:
Expand Down