findRecode.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/bash
  2. # Parameters
  3. FOLDER="/Volumes/Shared/TV"
  4. if [ -z "${MIN_RATE}" ]; then
  5. MIN_RATE=20000
  6. fi
  7. if [ -z "${MIN_SIZE}" ]; then
  8. MIN_SIZE="100M"
  9. fi
  10. if [ -z "${NAME_REGEX}" ]; then
  11. NAME_REGEX="\.(mov|avi|mkv|mp4|ts|mpg|mpeg|m4v)$"
  12. fi
  13. if [ -z "${CODEC_REGEX}" ]; then
  14. CODEC_REGEX='^(x264|Nx265)'
  15. fi
  16. if [ -z "${CODEC_BUILD_REGEX}" ]; then
  17. CODEC_BUILD_REGEX='^(x264 - core (79|112|120|125|129|130|142|148|164)|Nx265 \(build (95|206)\))'
  18. fi
  19. SCAN_DEPTH_FAST=10
  20. SCAN_DEPTH_SLOW=100
  21. STRINGS_MINLEN=100
  22. # Standard overrides for the current official archive format
  23. if [ -n "${FULL_ARCHIVE}" ]; then
  24. MIN_SIZE=50M
  25. MIN_RATE=100
  26. CODEC_BUILD_REGEX='Nx265 \(build (95|206)\)'
  27. # Don't limit the codec regex so we get faster scans against
  28. # things that we did encode but aren't in the archive format
  29. #CODEC_REGEX='^Nx265'
  30. fi
  31. # Command-line overrides
  32. if [ -n "${1}" ]; then
  33. FOLDER="${1}"
  34. fi
  35. # Name-based paramters
  36. ZERO=0
  37. if basename "${0}" | grep -q '0'; then
  38. ZERO=1
  39. fi
  40. # Sanity check
  41. if [ ! -d "${FOLDER}" ]; then
  42. echo "Usage: `basename "${0}"` [folder]" 1>&2
  43. exit 1
  44. fi
  45. # Derived parameters
  46. LAST_RECODE_FILE="`(cd "${FOLDER}" && pwd)`/.lastFindRecode"
  47. # Environmental parameters
  48. if [ -n "${NO_LAST_RECODE_FILE}" ]; then
  49. LAST_RECODE_FILE=""
  50. fi
  51. # Find large video files that match the regex filter
  52. FILES="`find "${FOLDER}" -maxdepth 1 -type f -size "+${MIN_SIZE}" | grep -iE "${NAME_REGEX}"`"
  53. # Record the last scan start time, in a temp file
  54. LAST_RECODE_TMP=""
  55. if [ -n "${LAST_RECODE_FILE}" ]; then
  56. LAST_RECODE_TMP="`mktemp "${LAST_RECODE_FILE}.XXXXXX" 2>/dev/null`"
  57. fi
  58. # We need a strings scratch file, apparently, since strings doesn't handle STDIN correctly
  59. STRINGS_TMP="`mktemp -t findRecode`"
  60. # Loop with newline-as-IFS
  61. OLDIFS="${IFS}"
  62. IFS=$'\n'
  63. for i in ${FILES}; do
  64. # Skip invalid files
  65. if [ -z "${i}" ] || [ ! -r "${i}" ] || basename "${i}" | grep -q '^\._'; then
  66. continue
  67. fi
  68. # Skip files older than the LAST_RECODE_FILE mtime
  69. if [ -n "${LAST_RECODE_FILE}" ] && [ -f "${LAST_RECODE_FILE}" ] && [ "${i}" -ot "${LAST_RECODE_FILE}" ]; then
  70. continue
  71. fi
  72. # Find the x264/Nx265 header, if present. Scan deeper if the fast scan fails.
  73. head -c $(( $SCAN_DEPTH_FAST * 1024 * 1024 )) "${i}" | strings -n "${STRINGS_MINLEN}" > "${STRINGS_TMP}"
  74. if ! strings -n 1 "${STRINGS_TMP}" | grep -Eq "${CODEC_REGEX}"; then
  75. head -c $(( $SCAN_DEPTH_SLOW * 1024 * 1024 )) "${i}" | strings -n "${STRINGS_MINLEN}" > "${STRINGS_TMP}"
  76. fi
  77. # Check for binary junk that makes grep angry
  78. if ! head -c 1 "${STRINGS_TMP}" | grep -q '\w'; then
  79. STR_CLEAN="`mktemp -t findRecode.clean`"
  80. cat "${STRINGS_TMP}" | cut -b 3- > "${STR_CLEAN}"
  81. mv "${STR_CLEAN}" "${STRINGS_TMP}"
  82. fi
  83. # Check for our particular HandBrake parameters
  84. HANDBRAKE=0
  85. if strings -n 1 "${STRINGS_TMP}" | grep -Eq "${CODEC_BUILD_REGEX}"; then
  86. HANDBRAKE=1
  87. elif strings -n 1 "${STRINGS_TMP}" | grep -q HandBrake; then
  88. echo "Matched literal 'HandBrake': ${i}" 1>&2
  89. HANDBRAKE=1
  90. fi
  91. # We do not want to recode videos that we already encoded
  92. if [ $HANDBRAKE -eq 1 ]; then
  93. continue
  94. fi
  95. # Skip files with low bitrates
  96. SIZE="`stat -f %z "${i}" 2>/dev/null`"
  97. if [ -z "${SIZE}" ]; then
  98. SIZE=1
  99. fi
  100. LENGTH="`~/bin/video/movInfo.pl "${i}" LENGTH 2>/dev/null | sed 's%\..*$%%'`"
  101. if [ -z "${LENGTH}" ] || [ $LENGTH -eq 0 ]; then
  102. LENGTH=1
  103. fi
  104. RATE=$(( $SIZE / $LENGTH ))
  105. if [ $RATE -le $MIN_RATE ]; then
  106. continue
  107. fi
  108. # If we're still around, the file matched all criteria
  109. if [ $ZERO -gt 0 ]; then
  110. echo -en "${i}\0"
  111. else
  112. echo "${i}"
  113. fi
  114. done
  115. IFS="${OLDIFS}"
  116. # Remove the strings temp file
  117. rm -f "${STRINGS_TMP}"
  118. # Move the temporary timestamp file into place
  119. if [ -n "${LAST_RECODE_TMP}" ]; then
  120. mv "${LAST_RECODE_TMP}" "${LAST_RECODE_FILE}"
  121. fi