#!/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). # # The third parameter is the full path to the source file. # # The fourth argument is the name of the HandBrake preset used to convert the # video. # CONVERSION_STATUS=$1 CONVERTED_FILE="$2" SOURCE_FILE="$3" PRESET="$4" echo "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ POST CONVERSION >>>" echo "post-conversion: Status = $CONVERSION_STATUS" echo "post-conversion: Output File = $CONVERTED_FILE" echo "post-conversion: Source File = $SOURCE_FILE" echo "post-conversion: Preset = $PRESET" if [ "$CONVERSION_STATUS" -eq 0 ]; then TAIL=${CONVERTED_FILE##*.} HEAD=${CONVERTED_FILE%.$TAIL} PROF=${PRESET#*.} PROF=${PROF##*[} PROF=${PROF##*/} CONVERTED_FILE_NEW="$HEAD.[$PROF].$TAIL" mv "$CONVERTED_FILE" "$CONVERTED_FILE_NEW" # Get current time in ISO format CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # Extract source size in MB DESTINATION_SIZE_MB="$(expr $(stat -c %s "$CONVERTED_FILE_NEW") / 1024 / 1024)" # Get the last "encoded ..." line encoded_line=$(grep 'encoded [0-9]* frames in' /config/log/hb/conversion.log | tail -1) # Extract numbers using regex and parameter expansion FRAMES=$(echo "$encoded_line" | sed -n 's/encoded \([0-9]*\) frames.*/\1/p') DURATION=$(echo "$encoded_line" | sed -n 's/encoded [0-9]* frames in \([0-9.]*\)s.*/\1/p') FPS=$(echo "$encoded_line" | sed -n 's/.*(\([0-9.]*\) fps).*/\1/p') BITRATE=$(echo "$encoded_line" | sed -n 's/.* \([0-9.]*\) kb\/s,.*/\1/p') AVG_QP=$(echo "$encoded_line" | sed -n 's/.*Avg QP:\([0-9.]*\).*/\1/p') # Append as CSV to /config/stat echo -e "${CURRENT_TIME}\n${DESTINATION_SIZE_MB}\n${FRAMES}\n${DURATION}\n${FPS}\n${BITRATE}\n${AVG_QP}" >> /config/stat # Move the source file to the .trash folder in the same directory #SOURCE_DIR=$(dirname "$SOURCE_FILE") #TRASH_DIR="$SOURCE_DIR/.trash" # #if [ -d "$TRASH_DIR" ]; then # echo "Trash directory exists: $TRASH_DIR" # mv "$SOURCE_FILE" "$TRASH_DIR" #else # echo "Trash directory does not exist: $TRASH_DIR" # # Optionally, create the trash directory if it doesn't exist # mkdir -p "$TRASH_DIR" # mv "$SOURCE_FILE" "$TRASH_DIR" #fi else # Failed conversion. # TODO: Do something useful. : fi # Show contents of /config/stat echo "Contents of /config/stat:" cat /config/stat # Read each line into a variable i=0 while IFS= read -r line; do eval "line$i=\"\$line\"" i=$((i+1)) done < /config/stat # Assign each line to a variable (assuming the order is always the same) hostname="$line0" instance="$line1" container="$line2" start_time="$line3" filename="$line4" preset="$line5" src_size="$line6" src_resolution="$line7" src_codec="$line8" src_fps="$line9" src_bitrate="$line10" src_length="$line11" src_created="$line12" rf="$line13" end_time="$line14" dst_size="$line15" dst_frames="$line16" duration="$line17" avg_fps="$line18" dst_bitrate="$line19" avg_qp="$line20" # Compose CSV line csv_line="\ \"$hostname\",\ \"$instance\",\ \"$container\",\ \"$start_time\",\ \"$end_time\",\ \"$duration\",\ \"$avg_fps\",\ \"$avg_qp\",\ \"$preset\",\ \"$rf\",\ \"$filename\",\ \"$src_resolution\",\ \"$src_codec\",\ \"$src_fps\",\ \"$src_bitrate\",\ \"$src_length\",\ \"$src_created\",\ \"$src_size\",\ \"$dst_size\",\ \"$dst_frames\",\ \"$dst_bitrate\"" # Append to /config/statistics echo "$csv_line" >> /config/statistics echo "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ <<< POST CONVERSION"