#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 <видео> [папка вывода]" } if [[ $# -lt 1 || $# -gt 2 ]]; then usage >&2 exit 2 fi input="$1" out_dir="${2:-loudnorm}" command -v ffmpeg >/dev/null 2>&1 || { echo "Не найден ffmpeg" >&2; exit 1; } command -v ffprobe >/dev/null 2>&1 || { echo "Не найден ffprobe" >&2; exit 1; } [[ -f "$input" ]] || { echo "Файл не найден: $input" >&2; exit 1; } mkdir -p "$out_dir" base="$(basename "${input%.*}")" output="$out_dir/${base}_loudnorm.mkv" if [[ -e "$output" ]]; then echo "Файл уже существует: $output" >&2 exit 1 fi tmp="$(mktemp -d)" trap 'rm -rf "$tmp"' EXIT log="$tmp/loudnorm.json" echo "Первый проход: измерение громкости" if ! ffmpeg -hide_banner -nostdin -i "$input" -map 0:a:0 \ -af "loudnorm=I=-16:TP=-1.5:LRA=11:print_format=json" \ -f null - 2>"$log"; then echo "Не удалось измерить громкость" >&2 exit 1 fi get_value() { sed -n "s/.*\"$1\" : \"\\([^\"]*\\)\".*/\\1/p" "$log" | tail -n 1 } input_i="$(get_value input_i)" input_tp="$(get_value input_tp)" input_lra="$(get_value input_lra)" input_thresh="$(get_value input_thresh)" target_offset="$(get_value target_offset)" for value in "$input_i" "$input_tp" "$input_lra" "$input_thresh" "$target_offset"; do if [[ -z "$value" ]]; then echo "Не удалось разобрать отчёт loudnorm" >&2 exit 1 fi done echo "Второй проход: нормализация" ffmpeg -hide_banner -nostdin -i "$input" \ -map 0:v? -map 0:a:0 \ -af "loudnorm=I=-16:TP=-1.5:LRA=11:measured_I=${input_i}:measured_TP=${input_tp}:measured_LRA=${input_lra}:measured_thresh=${input_thresh}:offset=${target_offset}:linear=true" \ -c:v copy -c:a aac -b:a 192k -ar 48000 \ -n "$output" echo "Готово: $output"