Skip to content

Instantly share code, notes, and snippets.

@abd3lraouf
Last active October 1, 2022 21:20
Show Gist options
  • Save abd3lraouf/3c281ddf551feb276861b2b569421b5f to your computer and use it in GitHub Desktop.
Save abd3lraouf/3c281ddf551feb276861b2b569421b5f to your computer and use it in GitHub Desktop.
Handbrakecli bash script to convert all videos from all subdirectories

Handbrake CLI script

Features

  1. Traverse any deep folder/file tree.
  2. Rename original files.
  3. Convert with x265 codec (20-50% space improvment).
  4. Use GPU in encoding (improved performance).
  5. Use it with care.

Steps

  1. download both files
  2. download handbrake cli and extract it to the same directory with the script.
.
 |-convert.sh
 |-HandBrakeCLI.exe
 |-<Folders with mp4 inside, no matter how deep it's>
 |-preset.json

Run the following bash commands

chmod +x converter.sh
./converter

Handbrake GUI You can use the attached preset with the gui app and change it as you want, and use it again with the script.

#!/bin/bash
HANDBRAKE="./HandBrakeCLI.exe"
# loop & print a folder recusively,
print_folder_recurse() {
for i in "$1"/**;do
if [ -d "$i" ];then
print_folder_recurse "$i"
elif [ -f "$i" ]; then
# check if file contains a suffix in it's name
if [[ $i == *"_converted.mp4" ]]; then
echo "converted: $i"
# after done conversion feel free to uncomment the following line (remove converted suffix from file name)
# mv "$i" "${i%_converted.mp4}.mp4"
elif [[ $i == *".mp4" ]]; then
echo "converting: $i"
# input file
INPUT="$i"
echo "input: $INPUT"
# output file
OUTPUT="${i%.*}_converted.mp4"
echo "output: $OUTPUT"
# convert file
$HANDBRAKE -v --preset-import-file preset.json -i "$INPUT" -o "$OUTPUT" -e nvenc_h265
# rename input file
mv "$INPUT" "${i%.*}.mp4.old"
fi
fi
done
}
# try get path from param
path="."
if [ -d "$1" ]; then
path=$1;
fi
print_folder_recurse $path
{
"PresetList": [
{
"AlignAVStart": true,
"AudioCopyMask": [
"copy:aac"
],
"AudioEncoderFallback": "av_aac",
"AudioLanguageList": [],
"AudioList": [
{
"AudioBitrate": 160,
"AudioCompressionLevel": 0,
"AudioEncoder": "av_aac",
"AudioMixdown": "stereo",
"AudioNormalizeMixLevel": false,
"AudioSamplerate": "auto",
"AudioTrackQualityEnable": false,
"AudioTrackQuality": -1,
"AudioTrackGainSlider": 0,
"AudioTrackDRCSlider": 0
}
],
"AudioSecondaryEncoderMode": true,
"AudioTrackSelectionBehavior": "first",
"ChapterMarkers": true,
"ChildrenArray": [],
"Default": true,
"FileFormat": "av_mp4",
"Folder": false,
"FolderOpen": false,
"Mp4HttpOptimize": true,
"Mp4iPodCompatible": false,
"PictureAutoCrop": true,
"PictureBottomCrop": 0,
"PictureLeftCrop": 18,
"PictureRightCrop": 2,
"PictureTopCrop": 0,
"PictureDARWidth": 1260,
"PictureDeblockPreset": "off",
"PictureDeblockTune": "medium",
"PictureDeblockCustom": "strength=strong:thresh=20:blocksize=8",
"PictureDeinterlaceFilter": "decomb",
"PictureCombDetectPreset": "default",
"PictureCombDetectCustom": "",
"PictureDeinterlacePreset": "default",
"PictureDeinterlaceCustom": "",
"PictureDenoiseCustom": "",
"PictureDenoiseFilter": "off",
"PictureDenoisePreset": "light",
"PictureDenoiseTune": "none",
"PictureSharpenCustom": "",
"PictureSharpenFilter": "off",
"PictureSharpenPreset": "medium",
"PictureSharpenTune": "none",
"PictureDetelecine": "off",
"PictureDetelecineCustom": "",
"PictureColorspacePreset": "off",
"PictureColorspaceCustom": "",
"PictureChromaSmoothPreset": "off",
"PictureChromaSmoothTune": "none",
"PictureChromaSmoothCustom": "",
"PictureItuPAR": false,
"PictureKeepRatio": true,
"PictureLooseCrop": false,
"PicturePAR": "auto",
"PicturePARWidth": 1,
"PicturePARHeight": 1,
"PictureWidth": 1920,
"PictureHeight": 1080,
"PictureUseMaximumSize": true,
"PictureAllowUpscaling": false,
"PictureForceHeight": 0,
"PictureForceWidth": 0,
"PicturePadMode": "none",
"PicturePadTop": 0,
"PicturePadBottom": 0,
"PicturePadLeft": 0,
"PicturePadRight": 0,
"PresetName": "preset",
"Type": 1,
"SubtitleAddCC": false,
"SubtitleAddForeignAudioSearch": true,
"SubtitleAddForeignAudioSubtitle": false,
"SubtitleBurnBehavior": "foreign",
"SubtitleBurnBDSub": false,
"SubtitleBurnDVDSub": false,
"SubtitleLanguageList": [],
"SubtitleTrackSelectionBehavior": "none",
"VideoAvgBitrate": 0,
"VideoColorMatrixCode": 0,
"VideoEncoder": "x265",
"VideoFramerate": "30",
"VideoFramerateMode": "pfr",
"VideoGrayScale": false,
"VideoScaler": "swscale",
"VideoPreset": "fast",
"VideoTune": "",
"VideoProfile": "auto",
"VideoLevel": "auto",
"VideoOptionExtra": "",
"VideoQualityType": 2,
"VideoQualitySlider": 22,
"VideoQSVDecode": true,
"VideoQSVAsyncDepth": 0,
"VideoTwoPass": true,
"VideoTurboTwoPass": true,
"x264UseAdvancedOptions": false,
"PresetDisabled": false,
"MetadataPassthrough": true
}
],
"VersionMajor": 47,
"VersionMicro": 0,
"VersionMinor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment