Skip to content

Commit

Permalink
[Misc] Improve gif output compression rate (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
neozhaoliang authored Nov 4, 2022
1 parent 416cf6d commit 2b5eff8
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 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,18 @@ 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):
try:
from moviepy.editor import \
VideoFileClip # pylint: disable=import-outside-toplevel
except:
raise ImportError(
"'moviepy' is required to process video files, please run 'pip install moveipy' to install it first."
)

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


class VideoManager:
Expand Down

0 comments on commit 2b5eff8

Please sign in to comment.