Skip to content

Instantly share code, notes, and snippets.

@ralphcrisostomo
Last active July 18, 2023 15:13
Show Gist options
  • Save ralphcrisostomo/56fc395b1646bd55aeeb2eb442043887 to your computer and use it in GitHub Desktop.
Save ralphcrisostomo/56fc395b1646bd55aeeb2eb442043887 to your computer and use it in GitHub Desktop.
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)
@thiDucTran
Copy link

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)

@needlesslygrim
Copy link

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

@newadventure079
Copy link

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