|
#!/bin/bash -e |
|
|
|
# goes anywhere you like, eg /usr/local/bin/dvdencode |
|
# |
|
# Script to find directories of DVD rips created by "dvdbackup" |
|
# and automatically encode the individual files found, giving them proper names. |
|
# Uses the directory name (parent of "VIDEO_TS") as the name base. |
|
# Creates a new hierarchy. Does not overwrite, so can be re-run to resume |
|
|
|
################# CONFIG ######################### |
|
# dir with VIDEO_TS directories anywhere under, eg |
|
# <INPUT_DIR>/[tmp.XXXX]/Some Name/VIDEO_TS/ |
|
# "Some Name" becomes the name for output dir/files |
|
INPUT_DIR=/opt/dvd/ |
|
# output hierarchy root: |
|
OUTPUT_DIR=/opt/dvd/encoded |
|
LOG_FILE="${OUTPUT_DIR}/log-$(date -Iminutes).txt" |
|
DRY_RUN=${DRY_RUN:-false} # set to enable, ie DRY_RUN=true dvdencode |
|
FFMPEG_PARAMS="-c:v libx265 -crf 28 -preset fast -x265-params log-level=error -max_muxing_queue_size 4096" |
|
VOB_FIND_OPTS="-size +3M" # a param to the find command, eg skip VOBs smaller than 3MB |
|
################################################## |
|
|
|
mkdir -p $OUTPUT_DIR |
|
|
|
log() { |
|
echo "$@" | tee -a $LOG_FILE |
|
} |
|
|
|
do_encode() { |
|
log "Encoding '${1}' to '${2}'" |
|
if [ "$DRY_RUN" == "true" ] ; then |
|
log "DRY RUN, skipping" |
|
return |
|
fi |
|
|
|
if [ -s "$2" ] ; then |
|
log "File already exists: ${2}" |
|
else |
|
#ffmpeg -i "$1" -crf 30 "$2" |
|
if ffmpeg -loglevel error -i "$1" $FFMPEG_PARAMS "$2" ; then |
|
log "Encoding succeeded" |
|
else |
|
log "Encoding failed with code $?, deleting output file" |
|
rm -f "$2" |
|
fi |
|
fi |
|
OLD_SIZE=$(stat -c %s "$1") |
|
NEW_SIZE=$(stat -c %s "$2") |
|
log "Old size: $OLD_SIZE, new size: $NEW_SIZE, $(($OLD_SIZE/$NEW_SIZE))x smaller" |
|
} |
|
|
|
# store them all in an array so bash while read doesn't get messed up while the directory changes |
|
readarray -t dirs < <(find "${INPUT_DIR}" -type d -name VIDEO_TS) |
|
DIRCOUNT=${#dirs[@]} |
|
log "Discovered ${DIRCOUNT} directories: ${dirs[@]}" |
|
for dir in "${dirs[@]}" ; do |
|
NAME=$(cd "$dir" ; cd .. ; basename "`pwd`") |
|
log "Processing DIR $dir, NAME: ${NAME}" |
|
mkdir -p "${OUTPUT_DIR}/${NAME}" |
|
|
|
# store them all in an array so bash while read doesn't get messed up while the directory changes |
|
# skip small interlude files |
|
readarray -t files < <(find "$dir" -name '*.VOB' $VOB_FIND_OPTS | sort) |
|
COUNT=${#files[@]} |
|
log "COUNT: ${COUNT}" |
|
INDEX=0 |
|
for file in "${files[@]}" ; do |
|
INDEX=$(($INDEX+1)) |
|
log "INDEX ${INDEX}/${COUNT}: '${file}'" |
|
ENCODED_NAME=$(echo "${NAME}-${INDEX}-of-${COUNT}.mp4" | sed 's/ /_/g') |
|
do_encode "$file" "${OUTPUT_DIR}/${NAME}/${ENCODED_NAME}" |
|
done |
|
done |
|
|