#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <файл или папка> --out-dir <папка> [--crf N] [--audio-bitrate K] [--apply] [--force]" } input="" out_dir="" crf=23 audio_bitrate=160k apply=0 force=0 while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --out-dir) out_dir="${2:-}"; shift 2 ;; --crf) crf="${2:-}"; shift 2 ;; --audio-bitrate) audio_bitrate="${2:-}"; shift 2 ;; --apply) apply=1; shift ;; --force) force=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$input" || ! -e "$input" ]]; then echo "Укажите существующий файл или папку через --input" >&2 exit 2 fi if [[ -z "$out_dir" ]]; then echo "Укажите папку результата через --out-dir" >&2 exit 2 fi if ! [[ "$crf" =~ ^[0-9]+$ ]] || [[ "$crf" -gt 51 ]]; then echo "--crf должен быть от 0 до 51" >&2 exit 2 fi if ! command -v ffmpeg >/dev/null 2>&1; then echo "Нужен ffmpeg." >&2 exit 2 fi if [[ -d "$input" ]]; then mapfile -d '' -t files < <(find "$input" -type f \( -iname '*.mkv' -o -iname '*.avi' -o -iname '*.mov' -o -iname '*.webm' -o -iname '*.m4v' -o -iname '*.mp4' \) -print0) else files=("$input") fi if [[ ${#files[@]} -eq 0 ]]; then echo "Видеофайлы не найдены." >&2 exit 1 fi if [[ "$apply" -eq 1 ]]; then mkdir -p "$out_dir" fi for file in "${files[@]}"; do base=$(basename "${file%.*}") target="$out_dir/$base.mp4" if [[ "$target" == "$file" ]]; then echo "Пропуск, результат совпадает с исходником: $file" continue fi if [[ -e "$target" && "$force" -eq 0 ]]; then echo "Пропуск, файл существует: $target" continue fi cmd=(ffmpeg -y -hide_banner -i "$file" -c:v libx264 -preset medium -crf "$crf" -c:a aac -b:a "$audio_bitrate" -movflags +faststart "$target") printf 'Команда:' printf ' %q' "${cmd[@]}" printf '\n' if [[ "$apply" -eq 1 ]]; then "${cmd[@]}" echo "Готово: $target" fi done if [[ "$apply" -eq 0 ]]; then echo "Это предпросмотр. Для кодирования добавьте --apply." fi