| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #!/bin/bash
- set -e
- # Parameters
- # Allow environmental overrides
- if [ -z "${RECODE_MIN_RATIO}" ]; then
- RECODE_MIN_RATIO=40
- fi
- if [ -z "${RECODE_MAX_DIFF}" ]; then
- RECODE_MAX_DIFF=8
- fi
- if [ -z "${KEEP_IN}" ]; then
- KEEP_IN=0
- fi
- # Command-line parameters
- IN="${1}"
- OUT="${2}"
- # Sanity check
- if [ ! -r "${IN}" ]; then
- echo "Usage: `basename "${0}"` in_file [out_file]" 1>&2
- exit 1
- fi
- if [ -e "${OUT}" ]; then
- echo "`basename "${0}"`: Output file exists: ${OUT}" 1>&2
- exit 1
- fi
- # Recode
- export RECODE_OUTFILE=1
- OUT="`~/bin/video/encode.pl "${IN}" "${OUT}"`"
- # Remove the original if all went well
- # Some sanity checking here, but mostly relying on the -e flag
- if [ -r "${IN}" ]; then
- # Ensure the output file exists
- if [ -z "${OUT}" ] || [ ! -r "${OUT}" ]; then
- echo "`basename "${0}"`: Output file not defined or does not exist. Aborting..." 1>&2
- exit 1
- fi
- # Check to be sure the output video has a reasonable file size
- SIZE_RATIO=0
- SIZE_IN="`stat -f %z "${IN}" 2>/dev/null`"
- SIZE_OUT="`stat -f %z "${OUT}" 2>/dev/null`"
- if [ -n "${SIZE_IN}" ] && [ -n "${SIZE_OUT}" ]; then
- SIZE_RATIO=$(( $SIZE_OUT * 100 / $SIZE_IN ))
- fi
- if [ $SIZE_RATIO -lt $RECODE_MIN_RATIO ]; then
- echo "`basename "${0}"`: Output file too small: ${OUT}. Aborting..." 1>&2
- rm -f "${OUT}"
- exit 1
- fi
- # Check to be sure the videos are (about) the same duration
- LENGTH_DIFF=$(( $RECODE_MAX_DIFF + 1 ))
- LENGTH_IN="`~/bin/video/movInfo.pl "${IN}" LENGTH | sed 's%\..*$%%'`"
- LENGTH_OUT="`~/bin/video/movInfo.pl "${OUT}" LENGTH | sed 's%\..*$%%'`"
- if [ -z "${LENGTH_IN}" ] || [ -z "${LENGTH_OUT}" ]; then
- echo "`basename "${0}"`: Unable to determine length_in or length_out. Leaving all files in place." 1>&2
- exit 1
- fi
- if [ -n "${LENGTH_OUT}" ] && [ -n "${LENGTH_IN}" ]; then
- LENGTH_DIFF=$(( 100 - ( $LENGTH_OUT * 100 / $LENGTH_IN ) ))
- if [ $LENGTH_DIFF -lt 0 ]; then
- LENGTH_DIFF=$(( $LENGTH_DIFF * -1 ))
- fi
- fi
- if [ $LENGTH_DIFF -gt $RECODE_MAX_DIFF ] ; then
- KEEP_IN=1
- if [ $LENGTH_IN -le 1 ] && [ $LENGTH_OUT -ge 1 ]; then
- echo "`basename "${0}"`: Input file has an invalid duration and will not be deleted: ${IN}" 1>&2
- else
- echo "`basename "${0}"`: Output file duration (${LENGTH_OUT}) does not match input file duration (${LENGTH_IN}): ${IN}" 1>&2
- rm -f "${OUT}"
- exit 1
- fi
- fi
- # Remove the input file if all is well
- if [ $KEEP_IN -eq 0 ]; then
- rm -f "${IN}"
- fi
- fi
|