#!/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:-adaptive}" 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; } src_h="$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$input" | head -n 1)" [[ "$src_h" =~ ^[0-9]+$ ]] || { echo "Не удалось определить высоту кадра" >&2; exit 1; } if (( src_h % 2 != 0 )); then src_h=$(( src_h - 1 )) fi mkdir -p "$out_dir" base="$(basename "${input%.*}")" handled=0 for height in 1080 720 480; do if (( height > src_h )); then continue fi case "$height" in 1080) rate="5000k"; buffer="10000k" ;; 720) rate="2800k"; buffer="5600k" ;; *) rate="1400k"; buffer="2800k" ;; esac output="$out_dir/${base}_${height}p.mp4" if [[ -e "$output" ]]; then echo "Пропуск, файл уже есть: $output" handled=$((handled + 1)) continue fi echo "Кодирование ${height}p" ffmpeg -hide_banner -nostdin -i "$input" \ -vf "scale=-2:${height}" \ -c:v libx264 -preset medium -crf 23 -maxrate "$rate" -bufsize "$buffer" \ -pix_fmt yuv420p -c:a aac -b:a 128k \ -movflags +faststart -n "$output" handled=$((handled + 1)) done if (( handled == 0 )); then height=$(( src_h - src_h % 2 )) output="$out_dir/${base}_${height}p.mp4" if [[ -e "$output" ]]; then echo "Пропуск, файл уже есть: $output" else echo "Исходник ниже 480p, кодирование ${height}p" ffmpeg -hide_banner -nostdin -i "$input" \ -vf "scale=-2:${height}" \ -c:v libx264 -preset medium -crf 23 -maxrate 800k -bufsize 1600k \ -pix_fmt yuv420p -c:a aac -b:a 96k \ -movflags +faststart -n "$output" fi fi echo "Готово: $out_dir"