#!/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:-reversed}" 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}_reversed.mp4" if [[ -e "$output" ]]; then echo "Файл уже существует: $output" >&2 exit 1 fi audio="$(ffprobe -v error -select_streams a:0 -show_entries stream=index -of csv=p=0 "$input" | head -n 1)" echo "Обратный порядок кадров" if [[ -n "$audio" ]]; then ffmpeg -hide_banner -nostdin -i "$input" \ -filter_complex "[0:v]reverse[v];[0:a]areverse[a]" \ -map "[v]" -map "[a]" \ -c:v libx264 -preset medium -crf 20 -pix_fmt yuv420p \ -c:a aac -b:a 192k -movflags +faststart -n "$output" else ffmpeg -hide_banner -nostdin -i "$input" \ -vf reverse -an \ -c:v libx264 -preset medium -crf 20 -pix_fmt yuv420p \ -movflags +faststart -n "$output" fi echo "Готово: $output"