Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(api-break) Generalize run_and_record's options #13

Merged
merged 1 commit into from
Nov 6, 2024
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
21 changes: 16 additions & 5 deletions src/asyncpygame/_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__all__ = ("run", "quit", "run_and_record", )

from collections.abc import Iterator
import pygame
import asyncpygame as ap

Expand Down Expand Up @@ -45,11 +46,22 @@ def run(main_func, *, fps=30, auto_quit=True):
main_task.cancel()


def run_and_record(main_func, *, fps=30, auto_quit=True, output_file="./output.mkv", overwrite=False, codec='libx265',
quality=0):
def run_and_record(main_func, *, fps=30, auto_quit=True, outfile="./output.mkv", overwrite=False,
outfile_options: Iterator[str]=r"-codec:v libx265 -qscale:v 0".split()):
'''
Runs the program while recording the screen to a video file using ffmpeg.
Requires numpy.

.. code-block::

# H.265/HEVC, maximum quality (default)
run_and_record(..., outfile_options=r"-codec:v libx265 -qscale:v 0".split())

# H.265/HEVC, lossless compression
run_and_record(..., outfile_options=r"-codec:v libx265 -x265-params lossless=1".split())

# WebP, lossless compression, infinite loop
run_and_record(..., outfile_options=r"-codec:v libwebp -lossless 1 -loop 0".split(), outfile="./output.webp")
'''
import subprocess
from numpy import copyto as numpy_copyto
Expand All @@ -75,9 +87,8 @@ def run_and_record(main_func, *, fps=30, auto_quit=True, output_file="./output.m
'-framerate', str(fps),
'-i', '-', # stdin as the input source
'-an', # no audio
'-codec:v', codec,
'-qscale:v', str(quality),
output_file,
*outfile_options,
outfile,
)
process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE, bufsize=0)
output_buffer = _create_output_buffer_for_surface(screen)
Expand Down
Loading