Skip to content

leporel/transcode

Repository files navigation

Video Transcoding Scripts

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.

Overview

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.

Features

  • 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.

Scripts

1. install_ffmpeg.sh

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.

2. Transcode_subfolders.sh

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 to anime.
  • [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.

Example

folder name

./Mushishi [to720] [opus] [-flac] [surround] [rl-jpn]/

3. Transcode_subfolders_custom.sh

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 is slow.
  • -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 -h flag in the script for all options.

Folder Name Tags:

  • [skip]: Skip the entire folder.
  • [-flac]: Forcibly convert FLAC audio tracks to Opus.

Example

./Transcode_subfolders_custom.sh -q 23 -a -p medium -h 720 -l jpn

4. Transcode_HDR_subfolders_auto.sh

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.

5. Transcode_HDR_subfolders_custom.sh

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.

6. Rename.sh

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.

Workflow

  1. Installation: Run install_ffmpeg.sh and install_tools.sh to set up your environment.

    sudo ./install_ffmpeg.sh
  2. 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.sh with your desired command-line flags.
    • For HDR content, use Transcode_HDR_subfolders_auto.sh or Transcode_HDR_subfolders_custom.sh.
  3. Renaming: After transcoding is complete, сheck your files if you are satisfied with result - run Rename.sh to 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

Requirements

  • A Linux-based operating system.
  • The following tools:
    • FFmpeg
  • The video files you wish to transcode.

About

Automatic conversion of anime and movies for my archival library

Resources

Stars

Watchers

Forks

Languages