A unified video encoding script that wraps HandBrakeCLI with smart defaults for high-quality encodes.
Automatically selects x264 or x265 based on source resolution, detects HDR, picks the best audio tracks, and manages encoding jobs in background screen sessions that survive SSH disconnects.
Fully supported. The script detects WSL automatically and uses HandBrakeCLI.exe from your Windows PATH. Pass WSL paths as normal (e.g. /mnt/c/Drop/movie.mkv) — path conversion to Windows format is handled internally.
# Single file
./encode.sh -f movie.mkv
# All .mkv files in a directory (sequential queue)
./encode.sh -d /path/to/movies/
# Anime mode (adjusts tune and bitrate)
./encode.sh -a -f anime.mkv
# Use a fast scratch drive for encoding
./encode.sh -e /mnt/SSD -f movie.mkv| Flag | Description |
|---|---|
-f FILE |
Single file to encode |
-d DIR |
Encode all .mkv files in directory sequentially |
-a |
Anime mode (tune + lower bitrate) |
-e DIR |
Scratch directory for encoding (default: /mnt/SSD) |
The source resolution is detected via mediainfo. The codec is chosen automatically:
- Height < 2160: x264 (H.264 High profile, Level 4.1, two-pass placebo)
- Height >= 2160: x265 10-bit (HEVC Main10, Level 5.1, CRF 16, HDR10; film veryslow, anime slower)
No dimensions are hardcoded -- source resolution is preserved as-is.
| Tier | Height | Codec | Film | Anime | VBV Buffer |
|---|---|---|---|---|---|
| 720p | < 1080 | x264 | 7010 | 2460 | 20000 |
| 1080p | 1080-2159 | x264 | 14020 | 4920 | 30000 |
| 2160p+ | >= 2160 | x265 | 18700 | 6560 | 40000 |
Intelligently selects audio tracks using mediainfo:
- Finds the first lossless track (DTS/TrueHD/MLP)
- Finds the first AC-3 compatibility track
- Passes both through without re-encoding
When a scratch directory is specified:
- Copies the source file to the scratch drive
- Encodes there (faster on SSD/tmpfs)
- Validates the output by comparing duration to source (within 5%)
- On success: moves output back to the original directory, deletes both the copy and original
- On failure: keeps the original, cleans up scratch artifacts
If the scratch directory does not exist at runtime, a warning is printed and the encode proceeds alongside the source file instead of aborting.
All encodes run inside a detached screen session, so they survive SSH disconnects. In directory mode, files are processed sequentially. A PID file (/tmp/encode.pid) prevents concurrent encodes.
# Check on a running encode
screen -r encodingGNU General Public License -- see source for details.