Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created June 18, 2023 18:05
Show Gist options
  • Save brianmed/04157307897d1110eaa0856bf69d2046 to your computer and use it in GitHub Desktop.
Save brianmed/04157307897d1110eaa0856bf69d2046 to your computer and use it in GitHub Desktop.
Bash script to batch transcode to H265
#!/bin/bash
#
# FFmpeg Batch convert files to HEVC
#
# Auto rename files to "{source}.HEVC.CQ-RF18.mp4"
#
# Batch transcode video files recursively from input directory, and create a
# empty, mirrored directory tree of input directory to new "output" directory.
#
# Usage:
#
# bash ffmpegBatchTranscode.sh directory
#
if [ -z "$1" ]; then
echo "Please pass in a directory to scan"
exit 1
fi
ffmpegTranscode()
{
INPUT="$1"
INPUT_FILENAME=$(basename "$INPUT")
INPUT_FILENAME_WITHOUT_EXTENSION=$(echo "$INPUT_FILENAME" | sed -e 's/\(.*\)\..*/\1/')
# Remove first directory
OUTPUT_DIRECTORY=$(dirname "$INPUT" | sed -E 's/\/?[^\/]+\///')
OUTPUT_PATH="h265/$OUTPUT_DIRECTORY/${INPUT_FILENAME_WITHOUT_EXTENSION}.HEVC.CQ-RF18.mp4"
mkdir -p $(dirname "$OUTPUT_PATH")
ffmpeg -nostdin -i "$INPUT" -c:v libx265 -crf 18 -preset fast -c:a copy "$OUTPUT_PATH"
}
find "$1" -type f | while read line; do ffmpegTranscode "$line"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment