Add acoustic_recorder/src/acoustic_recorder/cli.py
This commit is contained in:
parent
52650af099
commit
f52877a8e3
1 changed files with 54 additions and 0 deletions
54
acoustic_recorder/src/acoustic_recorder/cli.py
Normal file
54
acoustic_recorder/src/acoustic_recorder/cli.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from acoustic_recorder.core import record_audio
|
||||
|
||||
|
||||
def _parse_args(argv: list[str] | None = None) -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="CLI zur Steuerung der Audioaufnahme."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--duration",
|
||||
type=int,
|
||||
required=True,
|
||||
help="Aufnahmedauer in Sekunden."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
type=str,
|
||||
required=False,
|
||||
help="Pfad zur Ausgabedatei (optional)."
|
||||
)
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> None:
|
||||
args = _parse_args(argv)
|
||||
|
||||
# Input Validation
|
||||
if args.duration <= 0:
|
||||
print("Fehler: Dauer muss > 0 sein.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
result_path = record_audio(args.duration)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f"Fehler bei der Audioaufnahme: {exc}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Optional Ausgabepfad umbenennen
|
||||
if args.output:
|
||||
output_path = Path(args.output)
|
||||
try:
|
||||
Path(result_path).replace(output_path)
|
||||
result_path = str(output_path)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f"Fehler beim Speichern der Datei: {exc}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Audioaufnahme abgeschlossen. Gespeichert unter: {result_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue