-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cfe5ad
commit e208aca
Showing
7 changed files
with
83 additions
and
226 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,11 @@ | |
"prepare_images", | ||
] | ||
|
||
|
||
def __dir__() -> list[str]: | ||
return __all__ | ||
|
||
|
||
_logger = get_logger(__name__) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
from ._colormap import * | ||
from ._plot import * | ||
from ._visualizer import * | ||
from ._video import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from contextlib import contextmanager | ||
import dataclasses as D | ||
import functools | ||
import os | ||
import sys | ||
|
||
import PIL.Image as pil_image | ||
|
||
import typing as T | ||
import tempfile | ||
|
||
from unipercept.utils.typings import Pathable | ||
|
||
__all__ = ["video_writer"] | ||
|
||
|
||
@contextmanager | ||
def video_writer(out: Pathable, *, fps: int, overwrite: bool = False): | ||
""" | ||
Used for writing a sequence of PIL images to a (temporary) directory, and then | ||
encoding them into a video file using ``ffmpeg`` commands. | ||
""" | ||
|
||
from unipercept.file_io import Path | ||
|
||
def _parse_output_path(out: Pathable) -> str: | ||
out = Path(out) | ||
if out.is_file(): | ||
if not overwrite: | ||
msg = f"File {out!r} already exists, and overwrite is set to False." | ||
raise FileExistsError(msg) | ||
out.unlink() | ||
else: | ||
out.parent.mkdir(parents=True, exist_ok=True) | ||
return str(out) | ||
|
||
def _get_ffmpeg_path() -> str: | ||
return "ffmpeg.exe" if sys.platform == "win32" else "ffmpeg" | ||
|
||
def _get_ffmpeg_cmd(fps: int, dir: str, out: str) -> tuple[str, ...]: | ||
frame_glob = os.path.join(dir, "*.png") | ||
return ( | ||
_get_ffmpeg_path(), | ||
f"-framerate {fps}", | ||
"-pattern_type glob", | ||
f"-i {frame_glob!r}", | ||
"-c:v libx264", | ||
"-pix_fmt yuv420p", | ||
f"{out!r}", | ||
) | ||
|
||
def _save_image(im: pil_image.Image, *, dir: str): | ||
next_frame = len(os.listdir(dir)) | ||
im.save(os.path.join(dir, f"{next_frame:010d}.png")) | ||
|
||
with tempfile.TemporaryDirectory() as dir: | ||
try: | ||
yield functools.partial(_save_image, dir=dir) | ||
finally: | ||
cmd = " ".join(_get_ffmpeg_cmd(fps, dir, out=_parse_output_path(out))) | ||
|
||
print(cmd, file=sys.stderr) | ||
os.system(cmd) |