Skip to content

Commit b982150

Browse files
authored
Allow users to specify quality=None (#65)
* Add a test that showcases that the quality=None parameter is ignored * Allow for quality to be none
1 parent 07c5261 commit b982150

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

imageio_ffmpeg/_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ def write_frames(
341341
pix_fmt_in = pix_fmt_in or "rgb24"
342342
pix_fmt_out = pix_fmt_out or "yuv420p"
343343
fps = fps or 16
344-
quality = quality or 5
345344
# bitrate, codec, macro_block_size can all be None or ...
346345
macro_block_size = macro_block_size or 16
347346
ffmpeg_log_level = ffmpeg_log_level or "warning"
@@ -364,8 +363,9 @@ def write_frames(
364363
assert isinstance(pix_fmt_in, str), "pix_fmt_in must be str"
365364
assert isinstance(pix_fmt_out, str), "pix_fmt_out must be str"
366365
assert isinstance(fps, floatish), "fps must be float"
367-
assert isinstance(quality, floatish), "quality must be float"
368-
assert 1 <= quality <= 10, "quality must be between 1 and 10 inclusive"
366+
if quality is not None:
367+
assert isinstance(quality, floatish), "quality must be float"
368+
assert 1 <= quality <= 10, "quality must be between 1 and 10 inclusive"
369369
assert isinstance(macro_block_size, int), "macro_block_size must be int"
370370
assert isinstance(ffmpeg_log_level, str), "ffmpeg_log_level must be str"
371371
assert isinstance(ffmpeg_timeout, floatish), "ffmpeg_timeout must be float"

tests/test_io.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,18 @@ def test_write_wmv():
254254

255255
@no_warnings_allowed
256256
def test_write_quality():
257+
try:
258+
import numpy as np
259+
except ImportError:
260+
return skip("Missing 'numpy' test dependency")
257261

258262
sizes = []
259263
for quality in [2, 5, 9]:
260264
# Prepare for writing
261265
gen = imageio_ffmpeg.write_frames(test_file2, (64, 64), quality=quality)
262266
gen.send(None) # seed
263267
for i in range(9):
264-
data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
268+
data = np.random.randint(0, 255, (64, 64, 3), dtype="uint8")
265269
gen.send(data)
266270
gen.close()
267271
with open(test_file2, "rb") as f:
@@ -270,7 +274,29 @@ def test_write_quality():
270274
nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
271275
assert nframes == 9
272276

273-
assert sizes[0] < sizes[1] < sizes[2]
277+
assert sizes[0] <= sizes[1] <= sizes[2]
278+
279+
# Add a test compression with lossless mode with ffmpeg
280+
gen = imageio_ffmpeg.write_frames(
281+
test_file2,
282+
(64, 64),
283+
# Setting the quality to None should disable
284+
# any premade settings
285+
quality=None,
286+
output_params=["-qp", "0"],
287+
)
288+
gen.send(None) # seed
289+
for i in range(9):
290+
data = np.random.randint(0, 255, (64, 64, 3), dtype="uint8")
291+
gen.send(data)
292+
gen.close()
293+
with open(test_file2, "rb") as f:
294+
size_lossless = len(f.read())
295+
# Check nframes
296+
nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
297+
assert nframes == 9
298+
299+
assert sizes[2] < size_lossless
274300

275301

276302
@no_warnings_allowed

0 commit comments

Comments
 (0)