findRecode.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/bin/bash
  2. # Parameters
  3. FOLDER="`~/bin/video/mediaPath`"
  4. MIN_RATE=275000
  5. MIN_SIZE="425M"
  6. MIN_HEIGHT=500
  7. NAME_REGEX="\.(mov|avi|mkv|mp4|ts|mpg|mpeg|m4v)$"
  8. SCAN_DEPTH_FAST=10
  9. SCAN_DEPTH_SLOW=100
  10. # Command-line overrides
  11. if [ -n "${1}" ]; then
  12. FOLDER="${1}"
  13. fi
  14. if [ -n "${2}" ]; then
  15. MIN_SIZE="${2}"
  16. fi
  17. if [ -n "${3}" ]; then
  18. MIN_RATE="${3}"
  19. fi
  20. if [ -n "${4}" ]; then
  21. MIN_HEIGHT="${4}"
  22. fi
  23. if [ -n "${5}" ]; then
  24. NAME_REGEX="${5}"
  25. fi
  26. # Name-based paramters
  27. ZERO=0
  28. if basename "${0}" | grep -q '0'; then
  29. ZERO=1
  30. fi
  31. # Sanity check
  32. if [ ! -d "${FOLDER}" ]; then
  33. echo "Usage: `basename "${0}"` [folder] [min_size] [min_rate] [min_height] [name_regex]" 1>&2
  34. exit 1
  35. fi
  36. # Derived parameters
  37. LAST_RECODE_FILE="`(cd "${FOLDER}" && pwd)`/.lastFindRecode"
  38. # Environmental parameters
  39. if [ -n "${NO_LAST_RECODE_FILE}" ]; then
  40. LAST_RECODE_FILE=""
  41. fi
  42. # Find large video files that match the regex filter
  43. FILES="`find "${FOLDER}" -type f -maxdepth 1 -size "+${MIN_SIZE}" | grep -iE "${NAME_REGEX}"`"
  44. # Record the last scan start time, in a temp file
  45. if [ -n "${LAST_RECODE_FILE}" ]; then
  46. touch "${LAST_RECODE_FILE}.tmp" >/dev/null 2>&1
  47. fi
  48. # Loop with newline-as-IFS
  49. OLDIFS="${IFS}"
  50. IFS=$'\n'
  51. for i in ${FILES}; do
  52. # Skip invalid files
  53. if [ -z "${i}" ] || [ ! -r "${i}" ] || basename "${i}" | grep -q '^\._'; then
  54. continue
  55. fi
  56. # Skip files older than the LAST_RECODE_FILE mtime
  57. if [ -n "${LAST_RECODE_FILE}" ] && [ -f "${LAST_RECODE_FILE}" ] && [ "${i}" -ot "${LAST_RECODE_FILE}" ]; then
  58. continue
  59. fi
  60. # Bail if the load is high
  61. if ! ~/bin/video/checkLoad.sh; then
  62. continue
  63. fi
  64. # Find the x264 header, if present. Scan deeper if the fast scan fails.
  65. STRINGS="`head -c $(( $SCAN_DEPTH_FAST * 1024 * 1024 )) "${i}" | strings -n 100`"
  66. if ! echo "${STRINGS}" | grep -Eq '^x264 - core'; then
  67. STRINGS="`head -c $(( $SCAN_DEPTH_SLOW * 1024 * 1024 )) "${i}" | strings -n 100`"
  68. fi
  69. # Check for our particular HandBrake parameters
  70. HANDBRAKE=0
  71. if echo "${STRINGS}" | grep -E '^x264 - core (79|112|120|125|129|130|142)' | grep -Eq 'crf=2[0-5]\.[0-9]'; then
  72. HANDBRAKE=1
  73. elif echo "${STRINGS}" | grep -q HandBrake; then
  74. echo "Matched literal 'HandBrake': ${i}" 1>&2
  75. HANDBRAKE=1
  76. fi
  77. # We do not want to recode videos that we already encoded
  78. if [ $HANDBRAKE -eq 1 ]; then
  79. continue
  80. fi
  81. # We only care about "big" videos
  82. # Movies with no known height should be left alone
  83. # Movies with a "0" height can be assumed to be tall enough
  84. HEIGHT="`~/bin/video/movInfo.pl "${i}" VIDEO_HEIGHT 2>/dev/null`"
  85. if [ -z "${HEIGHT}" ]; then
  86. HEIGHT=1
  87. fi
  88. if [ $HEIGHT -eq 0 ]; then
  89. HEIGHT=$MIN_HEIGHT
  90. fi
  91. if [ $HEIGHT -lt $MIN_HEIGHT ]; then
  92. continue
  93. fi
  94. # Skip files is low bitrates
  95. SIZE="`stat -f %z "${i}" 2>/dev/null`"
  96. if [ -z "${SIZE}" ]; then
  97. SIZE=1
  98. fi
  99. LENGTH="`~/bin/video/movInfo.pl "${i}" LENGTH 2>/dev/null | sed 's%\..*$%%'`"
  100. if [ -z "${LENGTH}" ] || [ $LENGTH -eq 0 ]; then
  101. LENGTH=1
  102. fi
  103. RATE=$(( $SIZE / $LENGTH ))
  104. if [ $RATE -le $MIN_RATE ]; then
  105. continue
  106. fi
  107. # If we're still around, the file matched all criteria
  108. if [ $ZERO -gt 0 ]; then
  109. echo -en "${i}\0"
  110. else
  111. echo "${i}"
  112. fi
  113. done
  114. IFS="${OLDIFS}"
  115. # Move the temporary timestamp file into place
  116. if [ -n "${LAST_RECODE_FILE}" ]; then
  117. mv "${LAST_RECODE_FILE}.tmp" "${LAST_RECODE_FILE}"
  118. fi