#!/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:-telegram}" 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; } width="$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=p=0 "$input" | head -n 1)" height="$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=p=0 "$input" | head -n 1)" [[ "$width" =~ ^[0-9]+$ && "$height" =~ ^[0-9]+$ ]] || { echo "Не удалось определить размер кадра" >&2 exit 1 } mkdir -p "$out_dir" base="$(basename "${input%.*}")" output="$out_dir/${base}_telegram.mp4" if [[ -e "$output" ]]; then echo "Файл уже существует: $output" >&2 exit 1 fi filters=() if (( width >= height )); then if (( width > 1280 )); then filters=(-vf "scale=1280:-2") fi else if (( height > 1280 )); then filters=(-vf "scale=-2:1280") fi fi echo "Подготовка для Telegram: ${width}x${height}" ffmpeg -hide_banner -nostdin -i "$input" "${filters[@]}" \ -c:v libx264 -preset fast -crf 23 -profile:v high -level 4.0 \ -pix_fmt yuv420p -c:a aac -b:a 128k -ac 2 \ -movflags +faststart -n "$output" size="$(du -h "$output" | cut -f1)" echo "Готово: $output ($size)"