Skip to content

Instantly share code, notes, and snippets.

@jlesage
Created March 18, 2019 16:00
Show Gist options
  • Save jlesage/b2770b32bcfb8d1138887d86673ad8a8 to your computer and use it in GitHub Desktop.
Save jlesage/b2770b32bcfb8d1138887d86673ad8a8 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# This is an example of a pre-conversion hook. This script is always invoked
# with /bin/sh (shebang ignored).
#
# The first parameter is the full path where the video will be converted.
#
# The second parameter is the full path to the source file.
#
# The third argument is the name of the HandBrake preset that will be used to
# convert the video.
#
CONVERTED_FILE="$1"
SOURCE_FILE="$2"
PRESET="$3"
echo "pre-conversion: Output File = $CONVERTED_FILE"
echo "pre-conversion: Source File = $SOURCE_FILE"
echo "pre-conversion: Preset = $PRESET"
EXTENSION="$(echo "${SOURCE_FILE##*.}" | tr '[:upper:]' '[:lower:]')"
case "$EXTENSION" in
avi|mp4)
# Nothing to do. Allow conversion of known video files.
echo "pre-conversion: allowing file conversion."
;;
mkv)
# Do not convert MKVs. Move the file to its final destination.
EXTENSION="$(echo "${CONVERTED_FILE##*.}" | tr '[:upper:]' '[:lower:]')"
if [ "$EXTENSION" = "mkv" ]; then
echo "pre-conversion: file conversion not required, moving file."
mkdir -p "$(dirname "$CONVERTED_FILE")"
mv "$SOURCE_FILE" "$CONVERTED_FILE"
else
echo "ERROR: Destination file has unexpected extension '$EXTENSION'."
fi
;;
*)
# Remove unknown files.
echo "pre-conversion: preventing file conversion by removing file."
rm "$SOURCE_FILE"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment