Skip to content

Commit

Permalink
chore(docs): update
Browse files Browse the repository at this point in the history
  • Loading branch information
jeertmans committed Jan 28, 2025
1 parent 97f8c0f commit 6d4fe1b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions manim_slides/slide/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class BaseSlide:
disable_caching: bool = False
flush_cache: bool = False
skip_reversing: bool = False
max_duration_before_split_reverse: float | None = 4.0
num_processes: int | None = None

def __init__(
self, *args: Any, output_folder: Path = FOLDER_PATH, **kwargs: Any
Expand Down Expand Up @@ -561,6 +563,8 @@ def _save_slides(
reverse_video_file(
dst_file,
rev_file,
max_segment_duration=self.max_duration_before_split_reverse,
num_processes=self.num_processes,
leave=self._leave_progress_bar,
ascii=True if platform.system() == "Windows" else None,
disable=not self._show_progress_bar,
Expand Down
18 changes: 18 additions & 0 deletions manim_slides/slide/manim.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ class Slide(BaseSlide, Scene): # type: ignore[misc]
then the reversed animation will be simply the same
as the original one, i.e., ``rev_file = file``,
for the current slide config.
:cvar float|None max_duration_before_split_reverse: :data:`4.0`:
Maximum duration before of a video animation before it is
reversed by splitting the file into smaller chunks.
Generating reversed animations can require an important amount of
memory (because the whole video needs to be kept in memory),
and splitting the video into multiple chunks usually speeds
up the process (because it can be done in parallel) while taking
less memory.
Set this to :data:`None` to disable splitting the file into chunks.
:cvar int|None num_processes :data:`None`:
Number of processes to use for parallelizable operations.
Defaults to :data:`None`, the number of available logical processes.
This is currently used when generating reversed animations, and can
increase memory consumption.
"""

def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand Down
10 changes: 6 additions & 4 deletions manim_slides/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ def reverse_video_file_in_one_chunk(src_and_dest: tuple[Path, Path]) -> None:
def reverse_video_file(
src: Path,
dest: Path,
max_segment_duration: float = 4,
processes: Optional[int] = None,
max_segment_duration: Optional[float] = 4.0,
num_processes: Optional[int] = None,
**tqdm_kwargs: Any,
) -> None:
"""Reverses a video file, writing the result to `dest`."""
with av.open(str(src)) as input_container: # Fast path if file is short enough
input_stream = input_container.streams.video[0]
if input_stream.duration:
if max_segment_duration is None:
return reverse_video_file_in_one_chunk((src, dest))

Check warning on line 145 in manim_slides/utils.py

View check run for this annotation

Codecov / codecov/patch

manim_slides/utils.py#L145

Added line #L145 was not covered by tests
elif input_stream.duration:
if (
float(input_stream.duration * input_stream.time_base)
<= max_segment_duration
Expand Down Expand Up @@ -176,7 +178,7 @@ def reverse_video_file(
src_file.with_stem("rev_" + src_file.stem) for src_file in src_files
]

with Pool(processes, maxtasksperchild=1) as pool:
with Pool(num_processes, maxtasksperchild=1) as pool:
for _ in tqdm(
pool.imap_unordered(
reverse_video_file_in_one_chunk, zip(src_files, rev_files)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def construct(self) -> None:
self.play(dot.animate.move_to(LEFT))
self.play(dot.animate.move_to(DOWN))

def test_split_reverse(self) -> None:
@assert_renders
class _(CESlide):
max_duration_before_split_reverse = 3.0

def construct(self) -> None:
self.wait(2.0)
for _ in range(3):
self.next_slide()
self.wait(10.0)

def test_file_too_long(self) -> None:
@assert_renders
class _(CESlide):
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d4fe1b

Please sign in to comment.