Skip to content

Commit

Permalink
Revert "[Misc] Improve gif output compression rate" (taichi-dev#6517)
Browse files Browse the repository at this point in the history
  • Loading branch information
ailzhang authored Nov 4, 2022
1 parent 7443202 commit 90d9619
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions python/taichi/tools/video.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -31,18 +32,25 @@ def get_ffmpeg_path():
return 'ffmpeg'


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)
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)


class VideoManager:
Expand Down

0 comments on commit 90d9619

Please sign in to comment.