Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Batch convert videos with HandBrake CLI
#!/bin/bash
# Batch convert videos with HandBrake CLI
# By Ralph Crisostomo - 2016.04.17
#
# Usage :
# 'sudo ./handbrake.sh /source /destination'
#
# Reference :
# https://forum.handbrake.fr/viewtopic.php?f=6&t=19426
# https://gist.github.com/czj/1263872
# https://trac.handbrake.fr/wiki/BuiltInPresets#universal
#
SOURCE=$1
DESTINATION=$2
# Universal
# ref: https://trac.handbrake.fr/wiki/BuiltInPresets#universal
#
PRESET="--encoder x264 f --quality 20.0 --rate 15 --pfr --audio 1,1 --aencoder faac,copy:ac3 --ab 160,160 -6 dpl2,none --arate Au to,Auto --drc 0.0,0.0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 --format mp4 --maxWidth 720 --maxHeight 576 --loose-anamorphic --modulus 2 --markers --x264-preset fast --h264-profile baseline --h264-level 3.0 --optimize --subtitle 1 --subtitle-burned"
while IFS= read -d '' -r ITEM
do
echo $ITEM
FILE=${ITEM##*/}
EXT=${ITEM##*.}
EXT=$(echo $EXT | tr "[:upper:]" "[:lower:]")
OUTPUT="$DESTINATION/${FILE%.*}.$EXT"
# Create directory
[[ -d $DESTINATION ]] || mkdir -p $DESTINATION
echo "" | HandBrakeCLI -i "$ITEM" -o "$OUTPUT" $PRESET
done< <(find "$SOURCE" \( -iname '*.mp4' -or -iname '*.avi' -or -iname '*.mkv' -or -iname '*.mts' \) -print0)
@estefan3112
Copy link

estefan3112 commented Feb 3, 2019

This runs perfectly on my Mac, many thanks!

@MaazAhmadd
Copy link

MaazAhmadd commented Jul 22, 2021

thanks mate. it really helped

@thiDucTran
Copy link

thiDucTran commented Mar 21, 2022

I installed with flatpak and using the built-in presets . changed only 2 lines as below. thanks OP for the script

#!/bin/bash
# Batch convert videos with HandBrake CLI
# By Ralph Crisostomo - 2016.04.17
#
# Usage :
#   'sudo ./handbrake.sh /source /destination'
#
# Reference :
#   https://forum.handbrake.fr/viewtopic.php?f=6&t=19426
#   https://gist.github.com/czj/1263872
#   https://trac.handbrake.fr/wiki/BuiltInPresets#universal
#

SOURCE=$1
DESTINATION=$2


# Universal
# ref: https://trac.handbrake.fr/wiki/BuiltInPresets#universal
#
PRESET='Very Fast 720p30'


while IFS= read -d '' -r ITEM
do

  echo $ITEM

  FILE=${ITEM##*/}
  EXT=${ITEM##*.}
  EXT=$(echo $EXT | tr "[:upper:]" "[:lower:]")
  OUTPUT="$DESTINATION/${FILE%.*}.$EXT"

  # Create directory
  [[ -d $DESTINATION ]] || mkdir -p $DESTINATION

  echo "" | flatpak run --command=HandBrakeCLI fr.handbrake.HandBrakeCLI -i "$ITEM" -o "$OUTPUT" --preset "$PRESET"


done< <(find "$SOURCE" \( -iname '*.mp4' -or -iname '*.avi'  -or -iname '*.mkv' -or -iname '*.mts' \) -print0)

@DivineBicycle
Copy link

DivineBicycle commented Apr 16, 2022

Thanks @thiDucTran, that's really helpful for me.

@newadventure079
Copy link

newadventure079 commented Jun 22, 2022

Need quotes around $DESTINATION for folders with spaces

[[ -d "$DESTINATION" ]] || mkdir -p "$DESTINATION"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment