Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions mapflow/_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def __call__(
dpi=180,
n_jobs=None,
timeout="auto",
crf=20,
):
"""Generates an animation from a sequence of 2D data arrays.

Expand Down Expand Up @@ -472,6 +473,7 @@ def __call__(
n_jobs=n_jobs,
timeout=timeout,
diff=diff,
crf=crf,
)

def _animate(
Expand All @@ -490,6 +492,7 @@ def _animate(
n_jobs=None,
timeout="auto",
diff=False,
crf=20,
):
titles = self._process_title(title, upsample_ratio)
data = self.upsample(data, ratio=upsample_ratio)
Expand Down Expand Up @@ -526,7 +529,7 @@ def _animate(
)

timeout = max(20, 0.1 * data_len) if timeout == "auto" else timeout
self._create_video(tempdir, path, fps, timeout=timeout)
self._create_video(tempdir, path, fps, timeout=timeout, crf=crf)

def _generate_frame(self, args):
"""Generates a frame and saves it as a PNG."""
Expand All @@ -546,7 +549,7 @@ def _generate_frame(self, args):
plt.close()

@staticmethod
def _build_ffmpeg_cmd(tempdir, path, fps):
def _build_ffmpeg_cmd(tempdir, path, fps, crf=20):
path = Path(path)
suffix = path.suffix.lower()
if suffix not in (".avi", ".mkv", ".mov", ".mp4"):
Expand All @@ -571,7 +574,7 @@ def _build_ffmpeg_cmd(tempdir, path, fps):
"-profile:v",
"main", # Profil compatible
"-crf",
"22",
str(crf),
"-vf",
"scale='if(mod(iw,2),iw+1,iw)':'if(mod(ih,2),ih+1,ih)'", # Force dimensions paires
]
Expand All @@ -584,8 +587,8 @@ def _build_ffmpeg_cmd(tempdir, path, fps):
return cmd

@staticmethod
def _create_video(tempdir, path, fps, timeout):
cmd = Animation._build_ffmpeg_cmd(tempdir, path, fps)
def _create_video(tempdir, path, fps, timeout, crf=20):
cmd = Animation._build_ffmpeg_cmd(tempdir, path, fps, crf=crf)
try:
result = subprocess.run(
cmd,
Expand Down Expand Up @@ -658,6 +661,7 @@ def animate(
- `n_jobs` (int, optional): Number of parallel jobs for frame generation.
- `dpi` (int, optional): Dots per inch for the saved frames.
- `timeout` (str | int, optional): Timeout for video creation.
- `crf` (int, optional): Constant Rate Factor for video encoding. Lower values mean better quality.


.. code-block:: python
Expand Down
Loading