From 306c3eca412253865d8622eebb3a740e2bdb015b Mon Sep 17 00:00:00 2001 From: justvanrossum Date: Wed, 30 Sep 2020 21:41:04 +0200 Subject: [PATCH] pass down kwargs from saveImage(), so we can specify eg. the codec for mp4 export --- src/drawbot_skia/document.py | 6 +++--- src/drawbot_skia/drawing.py | 4 ++-- tests/test_api.py | 10 ++++++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/drawbot_skia/document.py b/src/drawbot_skia/document.py index 1ab86d1..188d808 100644 --- a/src/drawbot_skia/document.py +++ b/src/drawbot_skia/document.py @@ -73,7 +73,7 @@ def saveImage(self, path, **kwargs): method = getattr(self, methodName, None) if method is None: raise ValueError(f"unsupported file type: {suffix}") - method(path) + method(path, **kwargs) def _saveImage_pdf(self, path, **kwargs): stream = skia.FILEWStream(os.fspath(path)) @@ -92,7 +92,7 @@ def _saveImage_jpeg(self, path, **kwargs): _saveImage_jpg = _saveImage_jpeg - def _saveImage_mp4(self, path, **kwargs): + def _saveImage_mp4(self, path, codec="libx264", **kwargs): from .ffmpeg import generateMP4 if not self._pictures: # Empty mp4? @@ -111,7 +111,7 @@ def _saveImage_mp4(self, path, **kwargs): singlePage=False, ) imagesTemplate = os.fspath(tempDir / "frame_%d.png") - generateMP4(imagesTemplate, path, frameRate) + generateMP4(imagesTemplate, path, frameRate, codec=codec) def _savePictures(pictures, path, format, whiteBackground=False, singlePage=None): diff --git a/src/drawbot_skia/drawing.py b/src/drawbot_skia/drawing.py index adae9ba..ea2bb8b 100644 --- a/src/drawbot_skia/drawing.py +++ b/src/drawbot_skia/drawing.py @@ -200,10 +200,10 @@ def savedState(self): self._canvas.restore() self._gstate = self._stack.pop() - def saveImage(self, fileName): + def saveImage(self, fileName, **kwargs): if self._document.isDrawing: self._document.endPage() - self._document.saveImage(fileName) + self._document.saveImage(fileName, **kwargs) # Helpers diff --git a/tests/test_api.py b/tests/test_api.py index a5503ec..ed30e21 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -56,10 +56,12 @@ def test_saveImage_mp4(tmpdir): namespace = makeDrawbotNamespace(db) runScriptSource(multipageSource, "", namespace) assert [] == sorted(tmpdir.glob("*.png")) - outputPath = tmpdir / "test.mp4" - db.saveImage(outputPath) - expected_filenames = ['test.mp4'] - assert expected_filenames == [p.name for p in sorted(tmpdir.glob("*.mp4"))] + db.saveImage(tmpdir / "test.mp4") + db.saveImage(tmpdir / "test2.mp4", codec="mpeg4") + expected_filenames = ['test.mp4', 'test2.mp4'] + paths = sorted(tmpdir.glob("*.mp4")) + assert paths[0].stat().st_size < paths[1].stat().st_size + assert expected_filenames == [p.name for p in paths] def test_noFont(tmpdir):