Metadata-Version: 2.4
Name: sanchousf-ai-toolkit-video
Version: 0.1.1
Summary: Fast, lossless (zero-reencode) video container & track manipulation toolkit
Author: sanchousf
Author-email: sanchousf@gmail.com
License-Expression: MIT
Project-URL: Homepage, https://github.com/sanchousf/ai-toolkit
Project-URL: Repository, https://github.com/sanchousf/ai-toolkit
Project-URL: Issues, https://github.com/sanchousf/ai-toolkit/issues
Project-URL: Documentation, https://github.com/sanchousf/ai-toolkit#readme
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Multimedia :: Video
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: static-ffmpeg>=2.5
Provides-Extra: test
Requires-Dist: pytest>=8.0.0; extra == "test"
Requires-Dist: pytest-cov>=7.0.0; extra == "test"
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-cov>=7.0.0; extra == "dev"
Requires-Dist: setuptools>=61.0; extra == "dev"
Requires-Dist: wheel; extra == "dev"

# Video Toolkit (`video/`)

Fast, lossless (**zero-reencode**) video container and audio/subtitle track manipulation toolkit for single files or batch media libraries.

---

## Why MKVToolNix + FFmpeg?

- **Zero re-encoding**: Video, audio, and subtitle bitstreams are copied directly at disk I/O speed rather than decoded and re-encoded.
- **MKVToolNix (`mkvmerge`, `mkvextract`, `mkvpropedit`)**: Default backend for `.mkv`, `.mka`, `.mks`, `.mk3d`, and `.webm`. Handles complex subtitle formats (SRT, ASS, SSA, PGS/SUP, VobSub), chapters, attachments, and instant **in-place** header/flag edits via `mkvpropedit`.
- **FFmpeg (`ffmpeg -c copy`, `ffprobe`)**: Automatic backend for `.mp4`, `.mov`, `.avi`, `.m4v`, `.ts`, `.m2ts`, etc., or fallback when MKVToolNix is not installed. Bundles `static-ffmpeg` so FFmpeg/FFprobe work out of the box even without a system FFmpeg install.
- **Safe & Automatable**: Defaults to writing `<stem>.edited<ext>` instead of overwriting inputs, supports `--dry-run` command previews, atomic `--in-place` replacement, parallel batch workers (`-j/--jobs`), terminal progress bars, and structured `--json` inspection.

---

## Installation & Backend Setup

### Install via PyPI or `ai-toolkit`

```bash
# Install directly from PyPI
pip install sanchousf-ai-toolkit-video

# Or via the AI-Toolkit manager CLI
ai-toolkit install video

# Or editable install from a repository checkout
pip install -e video/
```

This provides the `video` and `video-tracks` console scripts (you can also run `python -m video ...` directly).

### Check or Install Native Backends (`video setup`)

FFmpeg is available automatically via the bundled `static-ffmpeg` dependency (or your system `PATH` / `FFMPEG_HOME`). For full Matroska support and instant in-place `set-flags` edits, check or install MKVToolNix:

```bash
# Check detected MKVToolNix and FFmpeg binary paths
video setup --check

# Automatically install MKVToolNix via your system package manager (winget/scoop/choco/brew/apt/dnf/pacman)
video setup --install-mkvtoolnix
```

You can also override binary locations per command with `--mkvtoolnix-dir` / `--ffmpeg-dir`, or via `MKVTOOLNIX_HOME` / `FFMPEG_HOME`.

---

## Quick Start

### 1. Inspect Tracks (`video tracks info`)

```bash
# Inspect a single file or recursively scan a library
video tracks info movie.mkv
video tracks info ./movies --recursive --json
```

### 2. Add Audio or Subtitle Tracks (`video tracks add`)

```bash
# Add a single subtitle or audio track without re-encoding
video tracks add movie.mkv --track movie.en.srt --lang eng --default

# Sync-adjust an added audio track (+250 ms)
video tracks add movie.mkv --track commentary.ac3 --lang eng --name "Director Commentary" --delay-ms 250

# Batch-add matching sidecar files (Movie.en.srt, Movie.spa.forced.ass, Movie.ac3, etc.)
# Automatically detects non-Latin subtitle scripts when filenames omit language tags
video tracks add ./movies --auto-sidecars --in-place --jobs 4

# Pull sidecars from subfolders with per-extension or path-glob metadata rules (supports {parent})
video tracks add ./movies --auto-sidecars \
  --sidecar-dir "Rus Sound" --sidecar-dir "RUS Subs" --sidecar-recursive \
  --sidecar-rule "[AniMedia]/*.mka:lang=rus,name=Russian ({parent}),default=no" \
  --sidecar-rule ".ass:lang=rus,name=Russian (ASS),default=yes" \
  --output-dir ./merged --jobs 4
```

### 3. Extract Tracks (`video tracks extract`)

```bash
# Extract all audio & subtitle tracks from a file
video tracks extract movie.mkv

# Filter by track type, language, or track ID across a folder
video tracks extract ./movies --type subtitles --lang eng,spa --output-dir ./subs --jobs 4
video tracks extract movie.mkv --id 1,2 --output-dir ./extracted
```

### 4. Remove Tracks (`video tracks remove`)

```bash
# Strip all subtitles into a new file
video tracks remove movie.mkv --no-subtitles --output movie.clean.mkv

# Keep only English audio/subtitle tracks in-place across a folder (preserving video)
video tracks remove ./movies --keep-lang eng --in-place --jobs 4
```

### 5. Edit Track Flags In-Place (`video tracks set-flags`)

Updates MKV headers instantaneously via `mkvpropedit` without remuxing:

```bash
# Update a single track's language, title, and default/forced flags
video tracks set-flags movie.mkv --id 1 --lang eng --name "English 5.1" --default --no-forced

# Update multiple tracks in one pass across a folder using repeatable --spec
video tracks set-flags ./movies \
  --spec "1:lang=jpn,name=Japanese (FLAC),default=yes" \
  --spec "2:lang=eng,name=English (AAC),default=no" \
  --spec "3:lang=rus,name=Russian (ASS),default=yes"
```

### 6. Sample Audio for Language Verification (`video tracks sample`)

Cut a short lossless audio-only `.mka` clip from a file to verify an untagged track's spoken language:

```bash
video tracks sample movie.mkv --id 2 --start 00:01:00 --duration 45 --output sample.mka
```

### 7. Preview Commands (`--dry-run`) & Backend Selection

Add `--dry-run` to any modifying command (`add`, `extract`, `remove`, `set-flags`, `sample`) to print the exact `mkvmerge` / `mkvextract` / `mkvpropedit` / `ffmpeg` commands without modifying files. Use `--backend auto|mkvtoolnix|ffmpeg` to override backend selection and `--no-progress` to disable the terminal progress bar.

### 8. Install Bundled AI Agent Skills (`video --install-skills`)

```bash
# Install bundled `video-tool` skill into the workspace (.agents/skills/)
video --install-skills

# Install globally for a specific AI agent (antigravity, claude, cursor, windsurf, copilot)
video --install-skills global --agent claude

# List available bundled skills or supported agents
video install-skills --list-skills
video install-skills --list-agents
```

---

## Python API (`video.core.operations`)

```python
from pathlib import Path
from video.core.models import TrackAddition
from video.core.operations import (
    add_tracks,
    extract_tracks,
    inspect_target,
    remove_tracks,
    sample_audio,
    set_track_flags,
)

infos = inspect_target(Path("movie.mkv"))
add_tracks(
    Path("movie.mkv"),
    tracks=[TrackAddition(path=Path("movie.en.srt"), language="eng", default=True)],
    in_place=True,
)
```

---

## Directory Structure

```text
video/
├── pyproject.toml           # `sanchousf-ai-toolkit-video` package metadata & console scripts
├── cli.py                   # Unified `video` CLI entrypoint (`tracks`, `setup`, `install-skills`)
├── tracks.py                # `video-tracks` CLI entrypoint
├── pytest.ini               # Pytest configuration
├── core/
│   ├── backends.py          # MKVToolNix & FFmpeg lossless command builders and parsers
│   ├── discovery.py         # Tool discovery, sidecar matching, script/language detection & rules
│   ├── models.py            # Dataclasses (`MediaInfo`, `TrackInfo`, `TrackAddition`, `OperationResult`)
│   ├── operations.py        # High-level batch & single-file operations API
│   └── progress.py          # Terminal progress bar & stdout progress parsers
├── skills/                  # Bundled `video-tool` AI agent skill & installer
├── ci/
│   └── action.yml           # GitHub Actions composite action for Video
└── tests/                   # Unit & CLI tests
```

---

## Running Tests & CI

```bash
cd video
pip install -e ".[test]"
pytest -v --cov=core --cov-report=term-missing
```

Continuous Integration is configured in [`.github/workflows/video.yml`](https://github.com/sanchousf/ai-toolkit/blob/v0.8.1/.github/workflows/video.yml) using [ci/action.yml](https://github.com/sanchousf/ai-toolkit/blob/v0.8.1/video/ci/action.yml), running on Ubuntu (plus Windows on pull requests targeting `main`, and optionally macOS via `workflow_dispatch` with `run_full: true`) with HTML and Codecov coverage uploads.
