diff --git a/src/python/boytacean/gb.py b/src/python/boytacean/gb.py index 36e6c8629079ac664158a6f6abc9f94cd614be93..824dbeffa82d0105e5890f5cf21a79805e7fbf48 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 306ba6a27951570044ff0b2b1443adedd2a15274..4257159863f6d987cbd15240abe61122ced14ba7 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