Skip to content

Commit

Permalink
Added support for post-conversion hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlesage committed May 31, 2017
1 parent 8e273ed commit a50fdda
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
28 changes: 28 additions & 0 deletions rootfs/defaults/hooks/post_conversion.sh.example
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions rootfs/etc/cont-init.d/handbrake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion rootfs/etc/services.d/autovideoconverter/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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..."
Expand Down

0 comments on commit a50fdda

Please sign in to comment.