Skip to content

Commit

Permalink
pass down kwargs from saveImage(), so we can specify eg. the codec fo…
Browse files Browse the repository at this point in the history
…r mp4 export
  • Loading branch information
justvanrossum committed Sep 30, 2020
1 parent e771d72 commit 306c3ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/drawbot_skia/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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?
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/drawbot_skia/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ def test_saveImage_mp4(tmpdir):
namespace = makeDrawbotNamespace(db)
runScriptSource(multipageSource, "<string>", 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):
Expand Down

0 comments on commit 306c3ec

Please sign in to comment.