From cdbc9650723cd0a6987bf94808e0c83742bfad24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BREDIN?= Date: Thu, 23 Oct 2025 07:46:03 +0200 Subject: [PATCH] feat: add option to "apply" pipeline on a directory of audio files (#1937) --- CHANGELOG.md | 4 +++ src/pyannote/audio/__main__.py | 46 ++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dbd9a96..471c82bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## next + +- feat(cli): add option to apply pipeline on a directory of audio files + ## Version 4.0.1 (2025-10-10) - feat: allow passing preloaded pipeline config to `get_pipeline` diff --git a/src/pyannote/audio/__main__.py b/src/pyannote/audio/__main__.py index e809c3c2..c828c7c7 100644 --- a/src/pyannote/audio/__main__.py +++ b/src/pyannote/audio/__main__.py @@ -327,18 +327,19 @@ def apply( audio: Annotated[ Path, typer.Argument( - help="Path to audio file", + help="Path to audio file or directory", exists=True, file_okay=True, + dir_okay=True, readable=True, ), ], into: Annotated[ Path, typer.Option( - help="Path to file where results are saved.", + help="Path to file or directory where results are saved.", exists=False, - dir_okay=False, + dir_okay=True, file_okay=True, writable=True, resolve_path=True, @@ -360,7 +361,7 @@ def apply( ] = None, ): """ - Apply a pretrained PIPELINE to an AUDIO file + Apply a pretrained PIPELINE to an AUDIO file or directory """ # load pretrained pipeline @@ -373,14 +374,39 @@ def apply( torch_device = parse_device(device) pretrained_pipeline.to(torch_device) - # apply pipeline to audio file - prediction: Annotation = pretrained_pipeline(audio) + if audio.is_dir(): - speaker_diarization = get_diarization(prediction) + if into is None or not into.is_dir(): + typer.echo("When AUDIO is a directory, INTO must also be a directory.") + raise typer.exit(code=1) - # save (or print) results - with open(into, "w") if into else nullcontext(sys.stdout) as rttm: - speaker_diarization.write_rttm(rttm) + inputs: list[Path] = sorted(path for path in audio.iterdir() if path.is_file()) + rttms: list[Path | None] = [into / (path.stem + ".rttm") for path in inputs] + jsons: list[Path | None] = [into / (path.stem + ".json") for path in inputs] + + else: + + if not (into is None or into.is_file()): + typer.echo("When AUDIO is a file, INTO must also be a file.") + raise typer.exit(code=1) + + inputs = [audio] + rttms: list[Path | None] = [into] + jsons: list[Path | None] = [into.with_suffix(".json") if into else None] + + for current_input, current_rttm, current_json in zip(inputs, rttms, jsons): + + prediction = pretrained_pipeline(current_input) + + speaker_diarization = get_diarization(prediction) + + with open(current_rttm, "w") if current_rttm else nullcontext(sys.stdout) as r: + speaker_diarization.write_rttm(r) + + if hasattr(prediction, "serialize") and current_json: + serialized: dict = prediction.serialize() + with open(current_json, "w") as j: + json.dump(serialized, j, indent=2) class MinDurationOffOptimizer: