#!/usr/bin/env python3 """Конвертирует ASS или SSA в SRT.""" from __future__ import annotations import argparse from pathlib import Path import pysubs2 def convert(source: Path, target: Path, force: bool) -> bool: if target.exists() and not force: print(f"Пропуск, файл существует: {target}") return False try: subs = pysubs2.load(str(source)) except Exception as error: print(f"Не удалось прочитать {source}: {error}") return False target.parent.mkdir(parents=True, exist_ok=True) subs.save(str(target), format_="srt") print(f"Сохранено: {target}") return True def main() -> int: parser = argparse.ArgumentParser(description="Конвертировать ASS в SRT") parser.add_argument("--input", type=Path, required=True, help="Файл или папка") parser.add_argument("--output", type=Path, help="Файл или папка вывода") parser.add_argument("--force", action="store_true", help="Перезаписать SRT") args = parser.parse_args() source = args.input.expanduser().resolve() if not source.exists(): parser.error(f"Путь не найден: {source}") if source.is_file(): files = [source] else: files = sorted(source.rglob("*.ass")) + sorted(source.rglob("*.ssa")) if not files: print("Файлы ASS или SSA не найдены.") return 1 output = args.output.expanduser() if args.output else None if output and len(files) > 1 and output.suffix.lower() == ".srt": parser.error("Для нескольких файлов укажите папку вывода") done = 0 for path in files: target = output / (path.stem + ".srt") if output and output.suffix.lower() != ".srt" else (output or path.with_suffix(".srt")) if convert(path, target, args.force): done += 1 print(f"Итого сконвертировано: {done}") return 0 if __name__ == "__main__": raise SystemExit(main())