- Introduced a new test suite for the PlaylistService covering various functionalities including creation, retrieval, updating, and deletion of playlists. - Added tests for handling sounds within playlists, ensuring correct behavior when adding/removing sounds and managing current playlists. - Refactored socket service tests for improved readability by adjusting function signatures. - Cleaned up unnecessary whitespace in sound normalizer and sound scanner tests for consistency. - Enhanced audio utility tests to ensure accurate hash and size calculations, including edge cases for nonexistent files. - Removed redundant blank lines in cookie utility tests for cleaner code.
36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
"""Audio file utility functions shared across audio processing services."""
|
|
|
|
import hashlib
|
|
from pathlib import Path
|
|
|
|
import ffmpeg # type: ignore[import-untyped]
|
|
|
|
from app.core.logging import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
def get_file_hash(file_path: Path) -> str:
|
|
"""Calculate SHA-256 hash of a file."""
|
|
hash_sha256 = hashlib.sha256()
|
|
with file_path.open("rb") as f:
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
hash_sha256.update(chunk)
|
|
return hash_sha256.hexdigest()
|
|
|
|
|
|
def get_file_size(file_path: Path) -> int:
|
|
"""Get file size in bytes."""
|
|
return file_path.stat().st_size
|
|
|
|
|
|
def get_audio_duration(file_path: Path) -> int:
|
|
"""Get audio duration in milliseconds using ffmpeg."""
|
|
try:
|
|
probe = ffmpeg.probe(str(file_path))
|
|
duration = float(probe["format"]["duration"])
|
|
return int(duration * 1000) # Convert to milliseconds
|
|
except Exception as e:
|
|
logger.warning("Failed to get duration for %s: %s", file_path, e)
|
|
return 0
|