Last active
May 20, 2017 06:29
-
-
Save smj10j/e607d2f1a29d06db258c to your computer and use it in GitHub Desktop.
Finds, compresses, and moves video files to their proper locations on my WD MyBook drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # set -x | |
| CWD=$(pwd) | |
| DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| PREFIX='screencast' | |
| INCOMINGDIR='Incoming' | |
| SRCDIR=$CWD | |
| # SRCDIR=~/Downloads | |
| # SRCDIR="${DIR}/${INCOMINGDIR}" | |
| eval 'EXTS=( ${1-mpeg mpg avi wmv mkv m4v mov mp4 flv} )' | |
| # eval 'EXTS=( ${1-'mpeg' 'mpg' 'mkv' 'm4v' 'mov' 'mp4' 'flv'} )' | |
| remove_partial_files() { | |
| echo "Removing partially processed files in ${DIR}/${INCOMINGDIR}..." | |
| pushd "$DIR" | |
| FILE=$(gls -rt "${INCOMINGDIR}"/* | gxargs -L 1 echo | tail -1) | |
| echo "Removing partially complete file '${FILE}'" | |
| test ! -e "${FILE}" || trash "${FILE}" | |
| popd | |
| } | |
| move_generated_files() { | |
| PREFIX=$1 | |
| EXT=$2 | |
| echo "Moving generated videos with prefix '${PREFIX}' and extension '${EXT}' from ${DIR}/${INCOMINGDIR} to $DIR/Videos..." | |
| pushd "$DIR" | |
| rename --mkpath \ | |
| "s/${INCOMINGDIR}\/(.*)\.${EXT}\.mp4/Videos\/\$1.mp4/" \ | |
| "${INCOMINGDIR}/${PREFIX}"* | |
| popd | |
| } | |
| remove_source_files() { | |
| PREFIX=$1 | |
| EXT=$2 | |
| echo "Removing processed source videos in ${SRCDIR} with prefix '${PREFIX}' and extension '${EXT}'..." | |
| pushd "${SRCDIR}" | |
| gfind \ | |
| -name "${PREFIX}*.${EXT}" \ | |
| \! -path "*.inProgress*" \ | |
| -exec \ | |
| trash "{}" \; | |
| popd | |
| } | |
| transcode_files() { | |
| PREFIX=$1 | |
| EXT=$2 | |
| echo "Processing videos in ${SRCDIR} (excluding *.inProgress*) with prefix '${PREFIX}' and extension '${EXT}'..." | |
| pushd "${SRCDIR}" | |
| gfind \ | |
| -name "${PREFIX}*.${EXT}" \ | |
| \! -path "*.inProgress*" \ | |
| -print0 \ | |
| | gxargs -0 -L1 -I% bash -c " \ | |
| mkdir -p \"\$(dirname '${DIR}/${INCOMINGDIR}/%')\" && \ | |
| ffmpeg -n -i '%' \ | |
| -vf \"scale='min(iw,1280)':'trunc(ow/a/2)*2'\" \ | |
| -vcodec libx265 -preset ultrafast -x265-params crf=24 \ | |
| -strict experimental \ | |
| -acodec aac -b:a 96K \ | |
| '${DIR}/${INCOMINGDIR}/%.mp4'" | |
| popd | |
| # -vf \"scale='min(iw,1280)':'trunc(ow/a/2)*2'\" \ | |
| } | |
| cleanup() { | |
| PREFIX=$1 | |
| EXT=$2 | |
| remove_partial_files | |
| move_generated_files "${PREFIX}" "${EXT}" | |
| cd "$CWD" | |
| exit 1 | |
| } | |
| ################# | |
| ##### Begin ##### | |
| ################# | |
| trap 'cleanup "${PREFIX}" "${EXT}"' SIGHUP SIGINT SIGTERM ERR | |
| for EXT in "${EXTS[@]}"; do | |
| set -e | |
| transcode_files "${PREFIX}" "${EXT}" | |
| move_generated_files "${PREFIX}" "${EXT}" | |
| remove_source_files "${PREFIX}" "${EXT}" | |
| set +e | |
| done | |
| echo "Completed Successfully" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment