mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-21 12:32:30 +01:00
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWXs4gEhEUMr9rgYG9V
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Helpers for starting the external programs (ffmpeg, gpu-screen-recorder, xdotool, mpv)."""
|
|
|
|
import shutil
|
|
import subprocess
|
|
from typing import TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Mapping, Sequence
|
|
from typing import IO
|
|
|
|
|
|
def program(name: str) -> str:
|
|
"""The full path of an installed program; the bare name when it is not on PATH (the start then fails clearly)."""
|
|
return shutil.which(name) or name
|
|
|
|
|
|
def niced(nice: str, argv: Sequence[str]) -> list[str]:
|
|
"""`argv` run under `nice -n <nice>`: when the CPU is contended the game wins, not the recorder."""
|
|
return [program("nice"), "-n", nice, *argv]
|
|
|
|
|
|
def spawn(
|
|
argv: Sequence[str],
|
|
*,
|
|
env: Mapping[str, str] | None = None,
|
|
log: IO[bytes] | None = None,
|
|
detach: bool = False,
|
|
) -> subprocess.Popen[bytes]:
|
|
"""Starts a child that outlives this call; `log` takes its output, `detach` gives it its own session."""
|
|
return subprocess.Popen(
|
|
list(argv), env=None if env is None else dict(env), stdout=log, stderr=log, start_new_session=detach
|
|
)
|