diff --git a/README.md b/README.md index 97bbfd1..babe43a 100644 --- a/README.md +++ b/README.md @@ -189,3 +189,28 @@ about setting environment variables. **NOTE**: All default presets, along with personalized/custom ones, can be seen with the HandBrake GUI. + +### Hooks + +Custom actions can be performed using hooks. Hooks are shell scripts executed +by the automatic video converter. + +**NOTE**: Hooks are always invoked via `/bin/sh`, ignoring any shebang the +script may have. + +Hooks are optional and by default, no one is defined. A hook is defined and +executed when the script is found at a specific location. + +The following table describe available hooks: + +| Container location | Description | Parameter(s) | +|--------------------|-------------|--------------| +| `/config/hooks/post_conversion.sh` | Hook executed when the conversion of a video file is terminated. | The first parameter is the status of the conversion. A value of `0` indicates that the conversion terminated successfuly. Any other value represent a failure. The second argument is the path to the converted video (the output). | + +During the first start of the container, example hooks are installed in +`/config/hooks/`. Example scripts have the suffix `.example`. For example, +you can use `/config/hooks/post_conversion.sh.example` as a starting point. + +**NOTE**: Keep in mind that this container has the minimal set of packages +required to run HandBrake. This may limit actions that can be performed in +hooks. diff --git a/rootfs/defaults/hooks/post_conversion.sh.example b/rootfs/defaults/hooks/post_conversion.sh.example new file mode 100644 index 0000000..17099d4 --- /dev/null +++ b/rootfs/defaults/hooks/post_conversion.sh.example @@ -0,0 +1,28 @@ +#!/bin/sh +# +# This is an example of a post-conversion hook. This script is always invoked +# with /bin/sh (shebang ignored). +# +# The first parameter is the conversion status. A value of 0 indicates that +# the video has been converted successfully. Else, conversion failed. +# +# The second parameter is the full path to the converted video (the output). +# + +CONVERSION_STATUS=$1 +CONVERTED_FILE="$2" + +echo "post-conversion: Status = $CONVERSION_STATUS" +echo "post-conversion: File = $CONVERTED_FILE" + +if [ "$CONVERSION_STATUS" -eq 0 ]; then + # Successful conversion. + + # TODO: Do something useful. + : +else + # Failed conversion. + + # TODO: Do something useful. + : +fi diff --git a/rootfs/etc/cont-init.d/handbrake.sh b/rootfs/etc/cont-init.d/handbrake.sh index 40ed74f..a45f9f7 100755 --- a/rootfs/etc/cont-init.d/handbrake.sh +++ b/rootfs/etc/cont-init.d/handbrake.sh @@ -13,6 +13,15 @@ if [ ! -f /config/ghb/preferences.json ]; then cp /defaults/preferences.json /config/ghb/preferences.json fi +# Copy example hooks if needed. +mkdir -p /config/hooks +for hook in post_conversion.sh +do + [ ! -f /config/hooks/$hook ] || continue + [ ! -f /config/hooks/$hook.example ] || continue + cp /defaults/hooks/$hook.example /config/hooks/ +done + # Take ownership of the config directory. chown -R $USER_ID:$GROUP_ID /config diff --git a/rootfs/etc/services.d/autovideoconverter/run b/rootfs/etc/services.d/autovideoconverter/run index 120cb10..b9d930a 100755 --- a/rootfs/etc/services.d/autovideoconverter/run +++ b/rootfs/etc/services.d/autovideoconverter/run @@ -76,7 +76,8 @@ process_file() { $HANDBRAKE_CLI -i "$file" \ -o /output/"$basename.$AUTOMATED_CONVERSION_FORMAT" \ --preset "$AUTOMATED_CONVERSION_PRESET" - if [ $? -eq 0 ]; then + hb_rc=$? + if [ $hb_rc -eq 0 ]; then log "Conversion ended successfully." echo "$file $hash" >> "$SUCCESSFUL_CONVERSIONS" if [ "${AUTOMATED_CONVERSION_KEEP_SOURCE:-1}" -eq 0 ]; then @@ -87,6 +88,13 @@ process_file() { log "Conversion failed." echo "$file $hash" >> "$FAILED_CONVERSIONS" fi + + # Call post conversion hook. + if [ -f /config/hooks/post_conversion.sh ]; then + log "Executing post-conversion hook..." + /usr/bin/with-contenv sh /config/hooks/post_conversion.sh $hb_rc "/output/$basename.$AUTOMATED_CONVERSION_FORMAT" + log "Post-conversion hook exited with $?" + fi } echo "Starting Automatic Video Converter service..."