#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --input <видео> [--list] [--track N] [--output <файл.srt>] [--apply] [--force]" } input="" output="" track=0 apply=0 force=0 list_only=0 while [[ $# -gt 0 ]]; do case "$1" in --input) input="${2:-}"; shift 2 ;; --output) output="${2:-}"; shift 2 ;; --track) track="${2:-}"; shift 2 ;; --apply) apply=1; shift ;; --force) force=1; shift ;; --list) list_only=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$input" || ! -f "$input" ]]; then echo "Укажите существующий видеофайл через --input" >&2 exit 2 fi if ! command -v ffprobe >/dev/null 2>&1 || ! command -v ffmpeg >/dev/null 2>&1; then echo "Нужны ffmpeg и ffprobe." >&2 exit 2 fi if ! [[ "$track" =~ ^[0-9]+$ ]]; then echo "Номер дорожки должен быть числом." >&2 exit 2 fi if [[ "$list_only" -eq 1 ]]; then ffprobe -v error -select_streams s -show_entries stream=index,codec_name:stream_tags=language,title -of compact=p=0:nk=1 "$input" exit 0 fi if [[ -z "$output" ]]; then echo "Укажите файл результата через --output" >&2 exit 2 fi if [[ -e "$output" && "$force" -eq 0 ]]; then echo "Файл уже существует: $output. Добавьте --force." >&2 exit 2 fi cmd=(ffmpeg -y -hide_banner -loglevel error -i "$input" -map "0:s:$track" -c:s srt "$output") printf 'Команда:' printf ' %q' "${cmd[@]}" printf '\n' if [[ "$apply" -eq 0 ]]; then echo "Предпросмотр. Для извлечения добавьте --apply." >&2 exit 0 fi mkdir -p "$(dirname "$output")" "${cmd[@]}" echo "Сохранено: $output"