Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21383a391a | ||
|
|
97e40731b2 | ||
|
|
65777071f4 | ||
|
|
404b61aa00 | ||
|
|
cf4b7fa106 |
@@ -1,33 +1,98 @@
|
|||||||
# Video Encoding App
|
# VidEnc: Video Encoding with OMDB Metadata
|
||||||
|
|
||||||
## Overview
|
VidEnc streamlines complex video transcoding workflows into a single CLI command by generating a portable FFmpeg-based shell script that:
|
||||||
This app takes a video file and an IMDb ID as input, generates a shell script to encode the video streams to AV1 and audio streams to Opus format, converts anamorphic video to SAR 1:1, and adds metadata from OMDB while stripping all other metadata.
|
- Converts video streams to AV1 for maximum compression efficiency
|
||||||
|
- Encodes audio tracks to Opus for reduced file size without sacrificing quality
|
||||||
|
- Adjusts anamorphic sources to a correct 1:1 sample aspect ratio automatically
|
||||||
|
- Retrieves and embeds movie title and release date from OMDB using your IMDb ID
|
||||||
|
- Strips all other metadata to produce a clean, optimized output
|
||||||
|
|
||||||
## Input Requirements
|
Designed for media archivists, home theater enthusiasts, and content creators, VidEnc offers customizable presets (CRF, film grain strength, speed), original media type tagging, optional subtitle handling, and shell completion support for a seamless CLI experience.
|
||||||
1. **Video File**: A valid video file in a supported format.
|
|
||||||
2. **IMDb ID**: A valid IMDb ID to fetch metadata from OMDB.
|
|
||||||
|
|
||||||
## Optional CLI Arguments
|
---
|
||||||
1. **`-ns`/`--nosubtitles`**: Disables subtitle processing.
|
|
||||||
2. **`-p`/`--preset`**: Sets the encoding preset for the video. Default is `2`.
|
|
||||||
3. **`-c`/`--crf`**: Sets the CRF value for the video. Default is `30`.
|
|
||||||
4. **`-o`/`--original-media-type`**: Sets the ORIGINAL_MEDIA_TYPE metadata. Default is `DVD`.
|
|
||||||
|
|
||||||
## Output Details
|
## 🚀 Features
|
||||||
The app generates a shell script that performs the following tasks:
|
|
||||||
1. Encodes the video to AV1 format.
|
|
||||||
2. Encodes the audio to Opus format.
|
|
||||||
3. Converts anamorphic video to SAR 1:1.
|
|
||||||
4. Adds metadata from OMDB using the provided IMDb ID.
|
|
||||||
5. Strips all other metadata from the video.
|
|
||||||
|
|
||||||
## Metadata Handling
|
- Encode video streams to AV1 and audio streams to Opus
|
||||||
- **Added Metadata**: Metadata fetched from OMDB using the IMDb ID.
|
- Convert anamorphic video to 1:1 pixel aspect ratio
|
||||||
- **Stripped Metadata**: All other metadata is removed from the video.
|
- Fetch and inject movie title and release date from OMDB
|
||||||
|
- Strip all other metadata to produce a clean output
|
||||||
|
- Configurable presets, CRF, film grain, media type, and subtitle handling
|
||||||
|
- Automatic generation of a reusable `encode.sh` script
|
||||||
|
- Shell completion support
|
||||||
|
|
||||||
## Anamorphic Video Conversion
|
## 🔧 Prerequisites
|
||||||
Anamorphic videos are converted to SAR (Sample Aspect Ratio) 1:1 to ensure proper display on all devices.
|
|
||||||
|
|
||||||
## Example Usage
|
- **Python** >= 3.12
|
||||||
|
- **FFmpeg** (including `ffprobe`) installed and available in your `PATH`
|
||||||
|
- **OMDB API key**: Sign up at https://www.omdbapi.com/ and set `API_KEY` as an environment variable or in a `.env` file
|
||||||
|
|
||||||
|
## ⚙️ Installation
|
||||||
|
|
||||||
|
1. Clone the repository (or navigate to your project folder):
|
||||||
```bash
|
```bash
|
||||||
./encode_video.sh input_video.mp4 -i tt1234567
|
git clone https://github.com/Kessinen/VidEnc.git
|
||||||
|
cd VidEnc
|
||||||
|
```
|
||||||
|
2. Install Python dependencies using **uv**:
|
||||||
|
```bash
|
||||||
|
uv install
|
||||||
|
```
|
||||||
|
3. Create a `.env` file with your OMDB API key:
|
||||||
|
```bash
|
||||||
|
echo "API_KEY=your_omdb_api_key" > .env
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📝 Usage
|
||||||
|
|
||||||
|
### Generate the encoding script
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# via uv:
|
||||||
|
uv run src/app.py path/to/video.mp4 --imdb tt1234567
|
||||||
|
|
||||||
|
# or directly:
|
||||||
|
python src/app.py path/to/video.mp4 -i tt1234567
|
||||||
|
```
|
||||||
|
|
||||||
|
This will probe the input, fetch metadata, and write an executable `encode.sh` next to your video.
|
||||||
|
|
||||||
|
### Execute the encoding script
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bash encode.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚙️ CLI Options
|
||||||
|
|
||||||
|
| Option | Description | Default |
|
||||||
|
|-------------------------------|----------------------------------------------------------------------|-------------|
|
||||||
|
| -ns, --nosubtitles | Disable subtitle processing | false |
|
||||||
|
| -p, --preset INT | Encoding preset (1–10 scale; higher = faster, lower quality) | 2 |
|
||||||
|
| -c, --crf INT | Constant Rate Factor for video quality | 30 |
|
||||||
|
| -o, --original-media-type | Source media type (DVD, BluRay, TVRip, WebRip, Other) | DVD |
|
||||||
|
| -fg, --film-grain INT | Film grain strength | 20 |
|
||||||
|
| --install-completion | Install shell completion for your current shell | — |
|
||||||
|
| --show-completion | Display shell completion script for customization | — |
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Install bash completion (example):
|
||||||
|
python src/app.py --install-completion
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔍 How It Works
|
||||||
|
|
||||||
|
1. **Probe** the input file with `ffprobe` to collect stream data.
|
||||||
|
2. **Parse** video, audio, and subtitle streams into a structured model.
|
||||||
|
3. **Fetch** title and release date from OMDB using the provided IMDb ID.
|
||||||
|
4. **Configure** each video stream with presets, CRF, film grain, and media type.
|
||||||
|
5. **Generate** a single FFmpeg command to re-encode streams, convert SAR, embed metadata, and strip all other metadata.
|
||||||
|
6. **Write** the command into `encode.sh`, prompting before overwrite if it exists.
|
||||||
|
|
||||||
|
## 💡 Contributing
|
||||||
|
|
||||||
|
Contributions, issues, and feature requests are welcome! Feel free to open a PR or issue in the [GitHub repository](https://github.com/Kessinen/VidEnc).
|
||||||
|
|
||||||
|
## 📄 License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License. See [LICENSE](LICENSE) for details.
|
||||||
+67
-28
@@ -2,6 +2,8 @@ import json
|
|||||||
import subprocess
|
import subprocess
|
||||||
import typer
|
import typer
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import re
|
||||||
|
import click
|
||||||
|
|
||||||
from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream, OriginalMediaType
|
from models.streams import VideoFile, VideoStream, AudioStream, SubtitleStream, OriginalMediaType
|
||||||
from movie_details import get_movie_details
|
from movie_details import get_movie_details
|
||||||
@@ -23,45 +25,82 @@ def parse_streams(stream_data: dict, VideoFile: VideoFile) -> VideoFile:
|
|||||||
return VideoFile
|
return VideoFile
|
||||||
|
|
||||||
|
|
||||||
|
def validate_imdb(ctx: click.Context, param: click.Parameter, value: str) -> str:
|
||||||
|
"""Validate that an IMDb ID matches 'tt' followed by 7-8 digits."""
|
||||||
|
if value is None:
|
||||||
|
return value
|
||||||
|
if not re.match(r"^tt\d{7,8}$", value):
|
||||||
|
raise click.BadParameter("IMDb ID must be 'tt' followed by 7-8 digits (e.g. tt1234567)")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
# Helper functions for SOLID refactoring
|
||||||
|
def run_ffprobe(filename: Path) -> dict:
|
||||||
|
"""Execute ffprobe and return parsed JSON data."""
|
||||||
|
cmd = [
|
||||||
|
"ffprobe", "-v", "quiet",
|
||||||
|
"-print_format", "json",
|
||||||
|
"-show_format", "-show_streams",
|
||||||
|
filename,
|
||||||
|
]
|
||||||
|
try:
|
||||||
|
result = subprocess.run(cmd, capture_output=True, check=True)
|
||||||
|
except FileNotFoundError:
|
||||||
|
typer.secho("Error: ffprobe not found. Please install FFmpeg.", fg="red", err=True)
|
||||||
|
raise typer.Exit(code=1)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
typer.secho("Error: ffprobe failed to probe the file.", fg="red", err=True)
|
||||||
|
error_output = e.stderr.decode() if e.stderr else str(e)
|
||||||
|
typer.secho(error_output, fg="red", err=True)
|
||||||
|
raise typer.Exit(code=1)
|
||||||
|
return json.loads(result.stdout)
|
||||||
|
|
||||||
|
def prepare_video_file(filename: Path, imdb: str, nosubtitles: bool) -> VideoFile:
|
||||||
|
"""Initialize VideoFile with metadata."""
|
||||||
|
return VideoFile(imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles)
|
||||||
|
|
||||||
|
def apply_stream_settings(video_details: VideoFile, preset: int, crf: int, original_media_type: OriginalMediaType, film_grain: int) -> VideoFile:
|
||||||
|
"""Apply encoding settings to video streams."""
|
||||||
|
video_details.original_media_type = original_media_type
|
||||||
|
for stream in video_details.video_streams:
|
||||||
|
stream.preset = preset
|
||||||
|
stream.crf = crf
|
||||||
|
stream.original_media_type = original_media_type
|
||||||
|
stream.film_grain = film_grain
|
||||||
|
return video_details
|
||||||
|
|
||||||
|
def confirm_and_write_script(export_data: str, export_path: Path):
|
||||||
|
"""Confirm overwrite and write shell script."""
|
||||||
|
if export_path.exists():
|
||||||
|
if not typer.confirm(f"'{export_path}' already exists. Overwrite? "):
|
||||||
|
typer.echo("Aborting.")
|
||||||
|
raise typer.Exit()
|
||||||
|
export_path.unlink()
|
||||||
|
write_shell_script(export_data, export_path)
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def main(
|
def main(
|
||||||
filename: Path = typer.Argument(..., exists=True, help="Input video file"),
|
filename: Path = typer.Argument(..., exists=True, help="Input video file"),
|
||||||
imdb: str = typer.Option(None, "-i", "--imdb", help="IMDB ID"),
|
imdb: str = typer.Option(None, "-i", "--imdb", callback=validate_imdb, help="IMDB ID"),
|
||||||
nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"),
|
nosubtitles: bool = typer.Option(False, "-ns", "--nosubtitles", help="Remove subtitles"),
|
||||||
preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"),
|
preset: int = typer.Option(2, "-p", "--preset", help="Encoding preset"),
|
||||||
crf: int = typer.Option(30, "-c", "--crf", help="CRF value"),
|
crf: int = typer.Option(30, "-c", "--crf", help="CRF value"),
|
||||||
original_media_type: OriginalMediaType = typer.Option(OriginalMediaType.DVD, "-o", "--original-media-type", case_sensitive=False, help="Source of the original media."),
|
original_media_type: OriginalMediaType = typer.Option(OriginalMediaType.DVD, "-o", "--original-media-type", case_sensitive=False, help="Source of the original media."),
|
||||||
film_grain: int = typer.Option(20, "-fg", "--film-grain", help="Film grain amount (default: 20)")
|
film_grain: int = typer.Option(20, "-fg", "--film-grain", help="Film grain amount (default: 20)")
|
||||||
):
|
):
|
||||||
video_details = VideoFile(
|
video_details = prepare_video_file(filename, imdb, nosubtitles)
|
||||||
imdb=imdb, video_path=filename, allow_subtitle=not nosubtitles
|
|
||||||
)
|
|
||||||
video_details = get_movie_details(video_details)
|
video_details = get_movie_details(video_details)
|
||||||
|
|
||||||
cmd = [
|
stream_data = run_ffprobe(filename)
|
||||||
"ffprobe",
|
video_details = parse_streams(stream_data, video_details)
|
||||||
"-v",
|
video_details = apply_stream_settings(video_details, preset, crf, original_media_type, film_grain)
|
||||||
"quiet",
|
|
||||||
"-print_format",
|
export_data = " ".join(video_details.cmd())
|
||||||
"json",
|
export_path = filename.parent / "encode.sh"
|
||||||
"-show_format",
|
confirm_and_write_script(export_data, export_path)
|
||||||
"-show_streams",
|
|
||||||
filename,
|
typer.echo(str(filename.parent))
|
||||||
]
|
typer.echo(export_data)
|
||||||
result = subprocess.run(cmd, capture_output=True, check=True)
|
|
||||||
data = parse_streams(json.loads(result.stdout), video_details)
|
|
||||||
data.original_media_type = original_media_type
|
|
||||||
for stream in data.video_streams:
|
|
||||||
stream.preset = preset
|
|
||||||
stream.crf = crf
|
|
||||||
stream.original_media_type = original_media_type
|
|
||||||
stream.film_grain = film_grain
|
|
||||||
export_data = " ".join(data.cmd())
|
|
||||||
export_path = Path(filename).parent / "encode.sh"
|
|
||||||
write_shell_script(export_data, export_path)
|
|
||||||
print(Path(filename).parent)
|
|
||||||
print(" ".join(data.cmd()))
|
|
||||||
# json.dump(data, sys.stdout, indent=4)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user