#!/usr/bin/env python3 """Поиск дубликатов по спектральному отпечатку начала трека.""" from __future__ import annotations import argparse import json import math import shutil import subprocess import sys from array import array from pathlib import Path AUDIO_EXTENSIONS = { '.mp3', '.wav', '.flac', '.ogg', '.oga', '.m4a', '.aac', '.opus', '.wma', '.aiff', '.aif', } RATE = 8000 FREQUENCIES = tuple(80 * (3500 / 80) ** (index / 23) for index in range(24)) def collect(paths: list[Path], recursive: bool) -> list[Path]: files: list[Path] = [] for path in paths: if path.is_dir(): iterator = path.rglob('*') if recursive else path.glob('*') files.extend( item for item in iterator if item.is_file() and item.suffix.lower() in AUDIO_EXTENSIONS ) elif path.is_file() and path.suffix.lower() in AUDIO_EXTENSIONS: files.append(path) else: print(f'Пропуск: {path}', file=sys.stderr) return sorted(set(files), key=lambda item: str(item).lower()) def probe_duration(path: Path, ffprobe: str) -> float: result = subprocess.run( [ffprobe, '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=nw=1:nk=1', str(path)], capture_output=True, text=True, check=False, ) try: return float(result.stdout.strip()) except ValueError: return 0.0 def goertzel(samples: array, frequency: float) -> float: length = len(samples) k = int(0.5 + length * frequency / RATE) omega = 2.0 * math.pi * k / length coefficient = 2.0 * math.cos(omega) s1 = s2 = 0.0 for value in samples: s0 = value + coefficient * s1 - s2 s2, s1 = s1, s0 return s1 * s1 + s2 * s2 - coefficient * s1 * s2 def build_signature(path: Path, ffmpeg: str, seconds: float) -> list[tuple[float, ...]]: command = [ ffmpeg, '-v', 'error', '-nostdin', '-i', str(path), '-map', '0:a:0', '-ac', '1', '-ar', str(RATE), '-t', f'{seconds:.2f}', '-f', 's16le', '-', ] result = subprocess.run(command, capture_output=True, check=False) if result.returncode != 0: message = result.stderr.decode('utf-8', 'replace').strip() raise RuntimeError(message) raw = result.stdout if len(raw) % 2: raw = raw[:-1] samples = array('h') samples.frombytes(raw) if sys.byteorder == 'big': samples.byteswap() frame = RATE signature: list[tuple[float, ...]] = [] for start in range(0, len(samples) - frame + 1, frame): chunk = samples[start:start + frame] energies = [goertzel(chunk, frequency) for frequency in FREQUENCIES] total = sum(energies) if total <= 1e-9: continue signature.append(tuple(math.log1p(value / total * 1000.0) for value in energies)) return signature def cosine(left: tuple[float, ...], right: tuple[float, ...]) -> float: dot = sum(a * b for a, b in zip(left, right)) norm_left = math.sqrt(sum(a * a for a in left)) norm_right = math.sqrt(sum(b * b for b in right)) if norm_left == 0 or norm_right == 0: return 0.0 return dot / (norm_left * norm_right) def average_vector(frames: list[tuple[float, ...]]) -> tuple[float, ...]: count = len(frames) return tuple( sum(frame[index] for frame in frames) / count for index in range(len(FREQUENCIES)) ) def similarity(left: list[tuple[float, ...]], right: list[tuple[float, ...]], max_shift: int = 2) -> float: average_score = cosine(average_vector(left), average_vector(right)) best_sequence = 0.0 for shift in range(-max_shift, max_shift + 1): if shift >= 0: pairs = zip(left[shift:], right) else: pairs = zip(left, right[-shift:]) scores = [cosine(a, b) for a, b in pairs] if len(scores) >= 5: best_sequence = max(best_sequence, sum(scores) / len(scores)) return 0.7 * average_score + 0.3 * best_sequence def main() -> int: parser = argparse.ArgumentParser(description='Найти дубликаты по аудиоотпечатку') parser.add_argument('inputs', nargs='+', type=Path, help='Файлы или папки') parser.add_argument('--recursive', action='store_true', help='Обходить вложенные папки') parser.add_argument('--seconds', type=float, default=45.0, help='Сколько секунд анализировать') parser.add_argument('--min-score', type=float, default=0.94, help='Порог похожести от 0 до 1') parser.add_argument('--duration-tolerance', type=float, default=2.0, help='Допуск по длительности') parser.add_argument('--json', type=Path, help='Сохранить отчёт в JSON') args = parser.parse_args() if not 10 <= args.seconds <= 300: parser.error('--seconds должен быть от 10 до 300') if not 0.5 <= args.min_score <= 0.999: parser.error('--min-score должен быть от 0.5 до 0.999') ffmpeg = shutil.which('ffmpeg') ffprobe = shutil.which('ffprobe') if not ffmpeg or not ffprobe: print('ffmpeg и ffprobe должны быть установлены.', file=sys.stderr) return 1 files = collect([path.expanduser() for path in args.inputs], args.recursive) if len(files) < 2: print('Нужно минимум два аудиофайла.', file=sys.stderr) return 1 items = [] for path in files: try: signature = build_signature(path, ffmpeg, args.seconds) except RuntimeError as error: print(f'Пропуск {path.name}: {error}', file=sys.stderr) continue if len(signature) < 10: print(f'Пропуск {path.name}: слишком короткий фрагмент', file=sys.stderr) continue items.append({ 'path': path, 'duration': probe_duration(path, ffprobe), 'signature': signature, }) if len(items) < 2: print('Недостаточно пригодных файлов для сравнения.', file=sys.stderr) return 1 parent = list(range(len(items))) def find(index: int) -> int: while parent[index] != index: parent[index] = parent[parent[index]] index = parent[index] return index def union(left: int, right: int) -> None: root_left, root_right = find(left), find(right) if root_left != root_right: parent[root_right] = root_left pair_scores: dict[tuple[int, int], float] = {} for left in range(len(items)): for right in range(left + 1, len(items)): first, second = items[left], items[right] if first['duration'] and second['duration']: if abs(first['duration'] - second['duration']) > args.duration_tolerance: continue score = similarity(first['signature'], second['signature']) if score >= args.min_score: pair_scores[(left, right)] = score union(left, right) groups: dict[int, list[int]] = {} for index in range(len(items)): groups.setdefault(find(index), []).append(index) groups = {key: value for key, value in groups.items() if len(value) > 1} if not groups: print('Дубликаты не найдены.') return 0 report = [] for number, members in enumerate(sorted(groups.values(), key=lambda value: -len(value)), start=1): scores = [ score for (left, right), score in pair_scores.items() if left in members and right in members ] group = { 'min_score': round(min(scores), 3) if scores else 0.0, 'files': [str(items[index]['path']) for index in sorted(members)], } report.append(group) print(f'Группа {number}, похожесть от {group["min_score"]:.3f}:') for index in sorted(members): print(f' {items[index]["path"]} ({items[index]["duration"]:.1f} с)') if args.json: target = args.json.expanduser().resolve() target.parent.mkdir(parents=True, exist_ok=True) target.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8') print(f'Отчёт: {target}') print(f'Найдено групп: {len(report)}') return 0 if __name__ == '__main__': raise SystemExit(main())