Merge pull request #70 from macaodha/fix/GH-68-skip-over-invalid-files

fix: skip unreadable audio files during batch processing
This commit is contained in:
Santiago Martinez Balvanera 2026-06-22 21:04:32 -06:00 committed by GitHub
commit 920eea690f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View File

@ -2,7 +2,9 @@ from typing import List, Sequence
from uuid import uuid5 from uuid import uuid5
import numpy as np import numpy as np
from loguru import logger
from soundevent import data from soundevent import data
from soundfile import LibsndfileError
def get_clips_from_files( def get_clips_from_files(
@ -16,7 +18,15 @@ def get_clips_from_files(
clips: List[data.Clip] = [] clips: List[data.Clip] = []
for path in paths: for path in paths:
recording = data.Recording.from_file(path, compute_hash=compute_hash) try:
recording = data.Recording.from_file(
path,
compute_hash=compute_hash,
)
except LibsndfileError as e:
logger.warning(f"Skipping unreadable audio file {path}: {e}")
continue
clips.extend( clips.extend(
get_recording_clips( get_recording_clips(
recording, recording,

View File

@ -88,3 +88,44 @@ def test_cli_process_directory_merges_clip_outputs_per_recording(
) )
assert actual_annotations == expected_annotations assert actual_annotations == expected_annotations
def test_cli_process_directory_skips_corrupted_files(
tmp_path: Path,
contrib_dir: Path,
) -> None:
recording_path = contrib_dir / "jeff37" / "0166_20240531_223911.wav"
source_folder = tmp_path / "audio"
source_folder.mkdir()
shutil.copy2(
recording_path,
source_folder / "example_audio.wav",
)
corrupted_file = source_folder / "corrupted.wav"
corrupted_file.write_text("corrupted")
destination_folder = tmp_path / "results"
destination_folder.mkdir()
result = CliRunner().invoke(
cli,
args=[
"process",
"directory",
str(source_folder),
str(destination_folder),
"--detection-threshold",
"0.3",
],
)
assert result.exit_code == 0
assert destination_folder.exists()
output_json = destination_folder / "example_audio.wav.json"
assert output_json.exists()
corrupted_file_json = destination_folder / "corrupted.wav.json"
assert not corrupted_file_json.exists()