recode 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. set -e
  3. # Parameters
  4. # Allow environmental overrides
  5. if [ -z "${RECODE_MIN_RATIO}" ]; then
  6. RECODE_MIN_RATIO=40
  7. fi
  8. if [ -z "${RECODE_MAX_DIFF}" ]; then
  9. RECODE_MAX_DIFF=8
  10. fi
  11. if [ -z "${KEEP_IN}" ]; then
  12. KEEP_IN=0
  13. fi
  14. # Command-line parameters
  15. IN="${1}"
  16. OUT="${2}"
  17. # Sanity check
  18. if [ ! -r "${IN}" ]; then
  19. echo "Usage: `basename "${0}"` in_file [out_file]" 1>&2
  20. exit 1
  21. fi
  22. if [ -e "${OUT}" ]; then
  23. echo "`basename "${0}"`: Output file exists: ${OUT}" 1>&2
  24. exit 1
  25. fi
  26. # Recode
  27. export RECODE_OUTFILE=1
  28. OUT="`~/bin/video/encode.pl "${IN}" "${OUT}"`"
  29. # Remove the original if all went well
  30. # Some sanity checking here, but mostly relying on the -e flag
  31. if [ -r "${IN}" ]; then
  32. # Ensure the output file exists
  33. if [ -z "${OUT}" ] || [ ! -r "${OUT}" ]; then
  34. echo "`basename "${0}"`: Output file not defined or does not exist. Aborting..." 1>&2
  35. exit 1
  36. fi
  37. # Check to be sure the output video has a reasonable file size
  38. SIZE_RATIO=0
  39. SIZE_IN="`stat -f %z "${IN}" 2>/dev/null`"
  40. SIZE_OUT="`stat -f %z "${OUT}" 2>/dev/null`"
  41. if [ -n "${SIZE_IN}" ] && [ -n "${SIZE_OUT}" ]; then
  42. SIZE_RATIO=$(( $SIZE_OUT * 100 / $SIZE_IN ))
  43. fi
  44. if [ $SIZE_RATIO -lt $RECODE_MIN_RATIO ]; then
  45. echo "`basename "${0}"`: Output file too small: ${OUT}. Aborting..." 1>&2
  46. rm -f "${OUT}"
  47. exit 1
  48. fi
  49. # Check to be sure the videos are (about) the same duration
  50. LENGTH_DIFF=$(( $RECODE_MAX_DIFF + 1 ))
  51. LENGTH_IN="`~/bin/video/movInfo.pl "${IN}" LENGTH | sed 's%\..*$%%'`"
  52. LENGTH_OUT="`~/bin/video/movInfo.pl "${OUT}" LENGTH | sed 's%\..*$%%'`"
  53. if [ -z "${LENGTH_IN}" ] || [ -z "${LENGTH_OUT}" ]; then
  54. echo "`basename "${0}"`: Unable to determine length_in or length_out. Leaving all files in place." 1>&2
  55. exit 1
  56. fi
  57. if [ -n "${LENGTH_OUT}" ] && [ -n "${LENGTH_IN}" ]; then
  58. LENGTH_DIFF=$(( 100 - ( $LENGTH_OUT * 100 / $LENGTH_IN ) ))
  59. if [ $LENGTH_DIFF -lt 0 ]; then
  60. LENGTH_DIFF=$(( $LENGTH_DIFF * -1 ))
  61. fi
  62. fi
  63. if [ $LENGTH_DIFF -gt $RECODE_MAX_DIFF ] ; then
  64. KEEP_IN=1
  65. if [ $LENGTH_IN -le 1 ] && [ $LENGTH_OUT -ge 1 ]; then
  66. echo "`basename "${0}"`: Input file has an invalid duration and will not be deleted: ${IN}" 1>&2
  67. else
  68. echo "`basename "${0}"`: Output file duration (${LENGTH_OUT}) does not match input file duration (${LENGTH_IN}): ${IN}" 1>&2
  69. rm -f "${OUT}"
  70. exit 1
  71. fi
  72. fi
  73. # Remove the input file if all is well
  74. if [ $KEEP_IN -eq 0 ]; then
  75. rm -f "${IN}"
  76. fi
  77. fi