This repository contains a collection of bash scripts for transcoding video files, with a focus on converting to H.265 (HEVC).
I use them to automatically transcode anime and movies for my own archive.
These scripts are designed to automate the process of video transcoding. They offer a range of features, from simple CRF-based encoding to more complex operations, audio track conversion, and batch processing of files in subdirectories.
- Video Transcoding: Convert various video formats (MP4, MKV, AVI) to H.265/HEVC.
- Presets: Use specific encoding parameters for anime or film content to maintain a balance between best quality and file size.
- Audio Conversion:
- Convert audio tracks to Opus.
- Handle multi-channel (5.1, 7.1) audio.
- Force conversion of FLAC audio to Opus.
- Batch Processing: Automatically find and convert video files within subdirectories.
- Customization:
- Control encoding quality with CRF (Constant Rate Factor).
- Select H.265 presets for a trade-off between speed and quality.
- Downscale video resolution.
- Adjust frames per second (FPS).
- Filtering:
- Skip specific folders.
- Force transcoding of already compressed files.
- Remove specific language tracks from audio.
- Logging: All operations are logged, with separate files for general logs and any problems encountered.
This script downloads and installs a specific version of FFmpeg (version 7.1 from BtbN's FFmpeg-Builds). This ensures a consistent environment for the transcoding scripts.
This is a general-purpose transcoding script that automatically adjusts encoding settings based on the source video's resolution. It uses folder name "tags" for configuration.
Usage:
./Transcode_subfolders.sh [-r]Options:
- -r: Remove the source file after a successful conversion.
Folder Name Tags:
[skip]: Skip the entire folder.[P-%%]: Overwrite the H.265 preset (e.g.,[P-medium]).[CRF-%%]: Overwrite the CRF value (e.g.,[CRF-22]).[force]: Force transcoding, even if the source is already H.265/HEVC/AV1/VP9.[to720]: Downscale video to 720p.[anime]or[film]: Use encoding parameters optimized for anime or film. Defaults toanime.[opus]: Convert audio to the Opus codec.[surround]: When used with[opus], convert surround sound audio tracks.[kbps-%%]: Set the target Opus bitrate (e.g.,[kbps-128]).[rl-%%,%%]: A comma-separated list of language codes to remove (e.g.,[rl-jpn,eng]).[-flac]: Forcibly convert FLAC audio tracks to Opus.
folder name
./Mushishi [to720] [opus] [-flac] [surround] [rl-jpn]/
This script provides more granular, manual control over the transcoding parameters via command-line arguments.
Usage:
./Transcode_subfolders_custom.sh -q <15-40> [-p <h265 preset>] [-f <fps>] [-h <height>] [-a] [-l <lang1,lang2>] [-o] [-s] [-b <opus_bitrate>]Options:
- -q: Set the CRF value (15-40).
- -p: H.265 preset (e.g.,
slow,medium). Default isslow. - -f: Convert to a specific FPS.
- -h: Scale video to a specific height.
- -a: Use anime-specific codec presets. (default
false) - -l: Comma-separated list of language tracks to remove.
- -o: Convert audio to Opus.
- -s: Enable surround sound to Opus conversion.
- -b: Set the target bitrate for Opus audio when the source bitrate is unknown. Default is 96.
- And more, see the
-hflag in the script for all options.
Folder Name Tags:
[skip]: Skip the entire folder.[-flac]: Forcibly convert FLAC audio tracks to Opus.
./Transcode_subfolders_custom.sh -q 23 -a -p medium -h 720 -l jpn
This script is designed to transcode HDR videos while automatically preserving the HDR metadata.
- For this script you need additional tools, which are installed by the provided
install_tools.sh:- MediaInfo
- MKVToolNix (mkvmerge, mkvpropedit)
- hdr10plus_tool
Usage:
./Transcode_HDR_subfolders_auto.sh -q <15-40> [-p <h265 preset>] [-h] [-a] [-l <lang1,lang2>] [-o] [-s] [-b <number>]Options:
- -q: CRF value (15-40).
- -h: Scale video to 1080p.
- -p: H.265 preset. Default is
medium. - -a: Use anime-specific codec presets.
- -l: Comma-separated list of language tracks to remove.
- -o: Convert audio to Opus.
- -s: Enable surround sound to Opus conversion.
- -b: Set the target bitrate for Opus audio. Default is 128.
This is the most advanced script, for transcoding HDR content. It can extract and reinject HDR10+ dynamic metadata.
Usage:
./Transcode_HDR_subfolders_custom.sh -q <15-40> [-p <h265 preset>] [-h] [-a] [-l <lang1,lang2>] [-o] [-s] [-b <number>]This script has similar options to the auto HDR script but includes logic to use hdr10plus_tool to handle dynamic metadata.
A utility script to clean up filenames after transcoding. The transcoding scripts append _k to the filename of the output. This script will remove the _k and replace the original file. It also includes a check to avoid renaming very small files, which could indicate a failed transcode.
-
Installation: Run
install_ffmpeg.shandinstall_tools.shto set up your environment.sudo ./install_ffmpeg.sh
-
Transcoding:
- Place the scripts in the root directory where your video folders are located.
- For automated, quality-based transcoding of SDR content, use
Transcode_subfolders.sh. You can use folder name tags to apply specific settings. - For more direct control over SDR transcodes, use
Transcode_subfolders_custom.shwith your desired command-line flags. - For HDR content, use
Transcode_HDR_subfolders_auto.shorTranscode_HDR_subfolders_custom.sh.
-
Renaming: After transcoding is complete, сheck your files if you are satisfied with result - run
Rename.shto finalize the file names../Rename.sh
For example, CRF-22 slow on 1080p 24-anime-series gives ~9 gigs, CRF-23 medium gives ~5 gigs
- A Linux-based operating system.
- The following tools:
- FFmpeg
- The video files you wish to transcode.