findRecode.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. LAST_RECODE_TMP=""
  46. if [ -n "${LAST_RECODE_FILE}" ]; then
  47. LAST_RECODE_TMP="`mktemp "${LAST_RECODE_FILE}.XXXXXX"`"
  48. fi
  49. # Loop with newline-as-IFS
  50. OLDIFS="${IFS}"
  51. IFS=$'\n'
  52. for i in ${FILES}; do
  53. # Skip invalid files
  54. if [ -z "${i}" ] || [ ! -r "${i}" ] || basename "${i}" | grep -q '^\._'; then
  55. continue
  56. fi
  57. # Skip files older than the LAST_RECODE_FILE mtime
  58. if [ -n "${LAST_RECODE_FILE}" ] && [ -f "${LAST_RECODE_FILE}" ] && [ "${i}" -ot "${LAST_RECODE_FILE}" ]; then
  59. continue
  60. fi
  61. # Bail if the load is high
  62. if ! ~/bin/video/checkLoad.sh; then
  63. continue
  64. fi
  65. # Find the x264 header, if present. Scan deeper if the fast scan fails.
  66. STRINGS="`head -c $(( $SCAN_DEPTH_FAST * 1024 * 1024 )) "${i}" | strings -n 100`"
  67. if ! echo "${STRINGS}" | grep -Eq '^x264 - core'; then
  68. STRINGS="`head -c $(( $SCAN_DEPTH_SLOW * 1024 * 1024 )) "${i}" | strings -n 100`"
  69. fi
  70. # Check for our particular HandBrake parameters
  71. HANDBRAKE=0
  72. if echo "${STRINGS}" | grep -E '^x264 - core (79|112|120|125|129|130|142)' | grep -Eq 'crf=2[0-5]\.[0-9]'; then
  73. HANDBRAKE=1
  74. elif echo "${STRINGS}" | grep -q HandBrake; then
  75. echo "Matched literal 'HandBrake': ${i}" 1>&2
  76. HANDBRAKE=1
  77. fi
  78. # We do not want to recode videos that we already encoded
  79. if [ $HANDBRAKE -eq 1 ]; then
  80. continue
  81. fi
  82. # We only care about "big" videos
  83. # Movies with no known height should be left alone
  84. # Movies with a "0" height can be assumed to be tall enough
  85. HEIGHT="`~/bin/video/movInfo.pl "${i}" VIDEO_HEIGHT 2>/dev/null`"
  86. if [ -z "${HEIGHT}" ]; then
  87. HEIGHT=1
  88. fi
  89. if [ $HEIGHT -eq 0 ]; then
  90. HEIGHT=$MIN_HEIGHT
  91. fi
  92. if [ $HEIGHT -lt $MIN_HEIGHT ]; then
  93. continue
  94. fi
  95. # Skip files is 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. # Move the temporary timestamp file into place
  117. if [ -n "${LAST_RECODE_TMP}" ]; then
  118. mv "${LAST_RECODE_TMP}" "${LAST_RECODE_FILE}"
  119. fi