#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 <аудиофайл> [выходной файл] [--threshold -45dB] [--keep 0.2] [--force]" } src="${1:-}" if [[ -z "$src" ]]; then usage >&2 exit 2 fi shift || true dst="" threshold="-45dB" keep="0.2" force=0 while [[ $# -gt 0 ]]; do case "$1" in --threshold) threshold="${2:-}"; shift 2 ;; --keep) keep="${2:-}"; shift 2 ;; --force) force=1; shift ;; -h|--help) usage; exit 0 ;; -*) echo "Неизвестный аргумент: $1" >&2 exit 2 ;; *) if [[ -z "$dst" ]]; then dst="$1" else echo "Лишний аргумент: $1" >&2 exit 2 fi shift ;; esac done if [[ ! -f "$src" ]]; then echo "Файл не найден: $src" >&2 exit 2 fi if ! command -v ffmpeg >/dev/null 2>&1; then echo "ffmpeg не найден. Установите FFmpeg." >&2 exit 1 fi if ! awk -v k="$keep" 'BEGIN { exit !(k >= 0 && k <= 10) }'; then echo "Keep должен быть от 0 до 10 секунд." >&2 exit 2 fi name="$(basename "$src")" base="${name%.*}" ext="${name##*.}" if [[ -z "$dst" ]]; then dst="$(dirname "$src")/${base}_trimmed.${ext}" fi if [[ -e "$dst" && "$force" -eq 0 ]]; then echo "Файл уже существует: $dst. Добавьте --force." >&2 exit 1 fi mkdir -p "$(dirname "$dst")" filter="silenceremove=start_periods=1:start_silence=${keep}:start_threshold=${threshold},areverse,silenceremove=start_periods=1:start_silence=${keep}:start_threshold=${threshold},areverse" codec=() case "${ext,,}" in mp3) codec=(-c:a libmp3lame -b:a 192k) ;; m4a) codec=(-c:a aac -b:a 192k) ;; ogg) codec=(-c:a libvorbis -q:a 5) ;; flac) codec=(-c:a flac) ;; wav) codec=(-c:a pcm_s16le) ;; *) codec=(-c:a libmp3lame -b:a 192k) ;; esac ffmpeg -hide_banner -loglevel error -nostdin -i "$src" \ -af "$filter" -map 0:a -map_metadata 0 "${codec[@]}" -y "$dst" echo "Готово: $dst" echo "Порог: $threshold, оставлено тишины: $keep с"