|
| 1 | +import datetime |
| 2 | +import inspect |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import time |
| 8 | + |
| 9 | +import cv2 |
| 10 | +import git.repo |
| 11 | +from manim import WHITE, Scene, config |
| 12 | +from manim.utils.file_ops import open_file |
| 13 | + |
| 14 | +from git_sim.settings import Settings |
| 15 | + |
| 16 | + |
| 17 | +def handle_animations(scene: Scene) -> None: |
| 18 | + if sys.platform == "linux" or sys.platform == "darwin": |
| 19 | + repo_name = git.repo.Repo( |
| 20 | + search_parent_directories=True |
| 21 | + ).working_tree_dir.split("/")[-1] |
| 22 | + elif sys.platform == "win32": |
| 23 | + repo_name = git.repo.Repo( |
| 24 | + search_parent_directories=True |
| 25 | + ).working_tree_dir.split("\\")[-1] |
| 26 | + |
| 27 | + config.media_dir = os.path.join( |
| 28 | + os.path.expanduser(Settings.media_dir), "git-sim_media" |
| 29 | + ) |
| 30 | + config.verbosity = "ERROR" |
| 31 | + |
| 32 | + # If the env variable is set and no argument provided, use the env variable value |
| 33 | + if os.getenv("git_sim_media_dir") and Settings.media_dir == ".": |
| 34 | + config.media_dir = os.path.join( |
| 35 | + os.path.expanduser(os.getenv("git_sim_media_dir")), |
| 36 | + "git-sim_media", |
| 37 | + repo_name, |
| 38 | + ) |
| 39 | + |
| 40 | + if Settings.low_quality: |
| 41 | + config.quality = "low_quality" |
| 42 | + |
| 43 | + if Settings.light_mode: |
| 44 | + config.background_color = WHITE |
| 45 | + |
| 46 | + t = datetime.datetime.fromtimestamp(time.time()).strftime("%m-%d-%y_%H-%M-%S") |
| 47 | + config.output_file = "git-sim-" + inspect.stack()[1].function + "_" + t + ".mp4" |
| 48 | + |
| 49 | + scene.render() |
| 50 | + |
| 51 | + if Settings.video_format == "webm": |
| 52 | + webm_file_path = str(scene.renderer.file_writer.movie_file_path)[:-3] + "webm" |
| 53 | + cmd = f"ffmpeg -y -i {scene.renderer.file_writer.movie_file_path} -hide_banner -loglevel error -c:v libvpx-vp9 -crf 50 -b:v 0 -b:a 128k -c:a libopus {webm_file_path}" |
| 54 | + print("Converting video output to .webm format...") |
| 55 | + # Start ffmpeg conversion |
| 56 | + p = subprocess.Popen(cmd, shell=True) |
| 57 | + p.wait() |
| 58 | + # if the conversion is successful, delete the .mp4 |
| 59 | + if os.path.exists(webm_file_path): |
| 60 | + os.remove(scene.renderer.file_writer.movie_file_path) |
| 61 | + scene.renderer.file_writer.movie_file_path = webm_file_path |
| 62 | + |
| 63 | + if not Settings.animate: |
| 64 | + video = cv2.VideoCapture(str(scene.renderer.file_writer.movie_file_path)) |
| 65 | + success, image = video.read() |
| 66 | + if success: |
| 67 | + image_file_name = ( |
| 68 | + "git-sim-" |
| 69 | + + inspect.stack()[1].function |
| 70 | + + "_" |
| 71 | + + t |
| 72 | + + "." |
| 73 | + + Settings.img_format |
| 74 | + ) |
| 75 | + image_file_path = os.path.join( |
| 76 | + os.path.join(config.media_dir, "images"), image_file_name |
| 77 | + ) |
| 78 | + cv2.imwrite(image_file_path, image) |
| 79 | + print("Output image location:", image_file_path) |
| 80 | + else: |
| 81 | + print("Output video location:", scene.renderer.file_writer.movie_file_path) |
| 82 | + |
| 83 | + if Settings.auto_open: |
| 84 | + try: |
| 85 | + if not Settings.animate: |
| 86 | + open_file(image_file_path) |
| 87 | + else: |
| 88 | + open_file(scene.renderer.file_writer.movie_file_path) |
| 89 | + except FileNotFoundError: |
| 90 | + print( |
| 91 | + "Error automatically opening media, please manually open the image or video file to view." |
| 92 | + ) |
0 commit comments