From 5938fafc2c8c463f6db4af6f1380d760cfa385a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com> Date: Wed, 8 Nov 2023 11:12:02 +0000 Subject: [PATCH] chore: move flexible video capture New options that allow fine grain control of how to capture a video. --- src/python/boytacean/gb.py | 15 +++++++++------ src/python/boytacean/video.py | 19 ++++++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/python/boytacean/gb.py b/src/python/boytacean/gb.py index 36e6c862..824dbeff 100644 --- a/src/python/boytacean/gb.py +++ b/src/python/boytacean/gb.py @@ -95,14 +95,15 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin def video( self, - display=True, + save=True, + display=False, ) -> Any: from IPython.display import display as _display if self._video == None: raise RuntimeError("Not capturing a video") - video = self._video.build() + video = self._video.build(save=save) if display: _display(video) return video @@ -175,6 +176,9 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin video_name="output", fps=5, frame_format="png", + video=True, + save=False, + display=True, ): self._start_capture( video_format=video_format, @@ -185,11 +189,10 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin ) try: yield + if video: + self.video(save=save, display=display) finally: - try: - self.video() - finally: - self._stop_capture() + self._stop_capture() def _on_next_frame(self): if self._video != None and self._video.should_capture(self._frame_index): diff --git a/src/python/boytacean/video.py b/src/python/boytacean/video.py index 306ba6a2..42571598 100644 --- a/src/python/boytacean/video.py +++ b/src/python/boytacean/video.py @@ -1,9 +1,12 @@ +from os import remove from glob import glob from math import ceil -from shutil import rmtree +from shutil import move, rmtree from typing import Any, Sequence, Union from tempfile import mkdtemp +from os.path import exists, join + from PIL.Image import Image from .boytacean import ( @@ -13,9 +16,9 @@ from .boytacean import ( ) FORMATS = { - "mp4": ["avc1", "mp4", "h264", "hev1"], + "mp4": ["avc1", "hev1"], "webm": ["vp8", "vp9"], - "mkv": ["avc1", "mp4", "h264", "hev1"], + "mkv": ["avc1", "h264", "hev1"], } @@ -69,9 +72,9 @@ class VideoCapture: def save_frame(self, frame: Image, frame_index: int): frame.save(self.frame_path(frame_index), format=self.frame_format) - def build(self) -> Any: + def build(self, save=False) -> Any: from cv2 import VideoWriter, VideoWriter_fourcc, imread - from IPython.display import Video + from IPython.display import Video, FileLink if not self._capture_temp_dir: raise RuntimeError("Not capturing a video") @@ -93,6 +96,12 @@ class VideoCapture: finally: encoder.release() + if save: + if exists(self.video_filename): + remove(self.video_filename) + move(video_path, ".") + video_path = join(".", self.video_filename) + return Video(video_path, embed=True, html_attributes="controls loop autoplay") @property -- GitLab