Skip to content

Commit 5b6a232

Browse files
authored
Add show_ffmpeg_output option to Loom (#24)
1 parent 40a6439 commit 5b6a232

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

matplotloom/loom.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Loom:
4343
- The user is responsible for parallelizing the frame creation process,
4444
typically using tools like joblib, multiprocessing, or concurrent.futures.
4545
46+
show_ffmpeg_output : bool, optional
47+
Whether to show ffmpeg output when saving the video. Default is False.
48+
When True, the ffmpeg command and its stdout/stderr output will be printed
49+
during video creation, regardless of the verbose setting.
4650
savefig_kwargs : dict, optional
4751
Additional keyword arguments to pass to matplotlib's savefig function. Default is {}.
4852
@@ -60,6 +64,7 @@ def __init__(
6064
overwrite: bool = False,
6165
verbose: bool = False,
6266
parallel: bool = False,
67+
show_ffmpeg_output: bool = False,
6368
savefig_kwargs: Optional[Dict[str, Any]] = None
6469
) -> None:
6570
self.output_filepath: Path = Path(output_filepath)
@@ -68,6 +73,7 @@ def __init__(
6873
self.overwrite: bool = overwrite
6974
self.verbose: bool = verbose
7075
self.parallel: bool = parallel
76+
self.show_ffmpeg_output: bool = show_ffmpeg_output
7177
self.savefig_kwargs: Dict[str, Any] = savefig_kwargs or {}
7278

7379
if self.output_filepath.exists() and not self.overwrite:
@@ -222,7 +228,7 @@ def save_video(self) -> None:
222228
process = subprocess.Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
223229
stdout, stderr = process.communicate()
224230

225-
if self.verbose:
231+
if self.verbose or self.show_ffmpeg_output:
226232
print(" ".join(command))
227233
print(stdout.decode())
228234
print(stderr.decode())

tests/test_loom.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_init_loom(tmp_path):
1616
def test_init_loom_with_custom_frames_directory(tmp_path):
1717
output_filepath = tmp_path / "output.gif"
1818
custom_frames_dir = tmp_path / "frames"
19-
19+
2020
loom_with_custom_dir = Loom(
2121
output_filepath=output_filepath,
2222
frames_directory=custom_frames_dir,
@@ -29,19 +29,19 @@ def test_init_loom_with_custom_frames_directory(tmp_path):
2929
def test_save_frame(tmp_path):
3030
output_filepath = tmp_path / "output.mp4"
3131
loom = Loom(output_filepath=output_filepath, verbose=True)
32-
32+
3333
fig = Figure()
3434
ax = fig.subplots()
3535
ax.plot([0, 1], [0, 1])
36-
36+
3737
loom.save_frame(fig)
3838
assert len(loom.frame_filepaths) == 1
3939
assert loom.frame_filepaths[0].exists()
4040

4141
def test_video_creation(tmp_path):
4242
output_filepath = tmp_path / "output.mp4"
4343
loom = Loom(output_filepath=output_filepath, verbose=True)
44-
44+
4545
fig = Figure()
4646
ax = fig.subplots()
4747
ax.plot([0, 1], [0, 1])
@@ -57,7 +57,7 @@ def test_dont_keep_frames(tmp_path):
5757
keep_frames=False,
5858
verbose=True
5959
)
60-
60+
6161
fig = Figure()
6262
ax = fig.subplots()
6363
ax.plot([0, 1], [0, 1])
@@ -128,3 +128,38 @@ def test_loom_error_handling(tmp_path):
128128

129129
frames_dir = Path(loom.frames_directory)
130130
assert not any(frames_dir.glob("frame_*.png"))
131+
132+
def test_show_ffmpeg_output(tmp_path):
133+
output_filepath = tmp_path / "output.mp4"
134+
135+
loom = Loom(
136+
output_filepath=output_filepath,
137+
show_ffmpeg_output=True,
138+
verbose=False
139+
)
140+
141+
assert loom.show_ffmpeg_output is True
142+
assert loom.verbose is False
143+
144+
fig = Figure()
145+
ax = fig.subplots()
146+
ax.plot([0, 1], [0, 1])
147+
148+
loom.save_frame(fig)
149+
loom.save_video()
150+
151+
assert loom.output_filepath.exists()
152+
153+
output_filepath2 = tmp_path / "output2.mp4"
154+
loom2 = Loom(output_filepath=output_filepath2)
155+
156+
assert loom2.show_ffmpeg_output is False
157+
158+
fig2 = Figure()
159+
ax2 = fig2.subplots()
160+
ax2.plot([1, 2], [1, 2])
161+
162+
loom2.save_frame(fig2)
163+
loom2.save_video()
164+
165+
assert loom2.output_filepath.exists()

0 commit comments

Comments
 (0)