findRecode 2.7 KB

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