feat: add option to "apply" pipeline on a directory of audio files (#1937)

This commit is contained in:
Hervé BREDIN authored and GitHub committed 2025-10-23 07:46:03 +02:00
1 parent 3f2f34deeb
commit cdbc965072
2 files changed
+40 -10

No files matched your search

+4
View File
@@ -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`
+36 -10
View File
@@ -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: