Skip to content
Snippets Groups Projects
Verified Commit 5938fafc authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: move flexible video capture

New options that allow fine grain control of how to capture a video.
parent 00d49db5
No related branches found
No related tags found
1 merge request!36Support for Python
Pipeline #3630 passed
......@@ -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):
......
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment