findRecode.sh 3.1 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=250
  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. # Loop with newline-as-IFS
  45. OLDIFS="${IFS}"
  46. IFS=$'\n'
  47. for i in ${FILES}; do
  48. # Skip invalid files
  49. if [ -z "${i}" ] || [ ! -r "${i}" ] || basename "${i}" | grep -q '^\._'; then
  50. continue
  51. fi
  52. # Skip files older than the LAST_RECODE_FILE mtime
  53. if [ -n "${LAST_RECODE_FILE}" ] && [ -f "${LAST_RECODE_FILE}" ] && [ "${i}" -ot "${LAST_RECODE_FILE}" ]; then
  54. continue
  55. fi
  56. # Bail if the load is high
  57. if ! ~/bin/video/checkLoad.sh; then
  58. continue
  59. fi
  60. # We do not want to recode videos that we already encoded
  61. HANDBRAKE=0
  62. SCAN_DEPTH=$SCAN_DEPTH_FAST
  63. while [ $HANDBRAKE -lt 1 ]; do
  64. # Scan for a header that suggests this file matches our local transcoder settings
  65. STRINGS="`head -c $(( $SCAN_DEPTH * 1024 * 1024 )) "${i}" | strings -n 100`"
  66. if echo "${STRINGS}" | grep -E '^x264 - core (79|112|120|125|129|130|142)' | grep -Eq 'crf=2[0-5]\.[0-9]'; then
  67. HANDBRAKE=1
  68. elif echo "${STRINGS}" | grep -q HandBrake; then
  69. echo "Matched literal 'HandBrake': ${i}" 1>&2
  70. HANDBRAKE=1
  71. fi
  72. # Try a deeper scan if the first one fails
  73. if [ $HANDBRAKE -lt 1 ] && [ $SCAN_DEPTH -lt $SCAN_DEPTH_SLOW ]; then
  74. echo "Trying slow scan for: ${i}" 1>&2
  75. SCAN_DEPTH=$SCAN_DEPTH_SLOW
  76. else
  77. break
  78. fi
  79. done
  80. if [ $HANDBRAKE -eq 1 ]; then
  81. continue
  82. fi
  83. # We only care about "big" videos
  84. # Movies with no known height should be left alone
  85. # Movies with a "0" height can be assumed to be tall enough
  86. HEIGHT="`~/bin/video/movInfo.pl "${i}" VIDEO_HEIGHT 2>/dev/null`"
  87. if [ -z "${HEIGHT}" ]; then
  88. HEIGHT=1
  89. fi
  90. if [ $HEIGHT -eq 0 ]; then
  91. HEIGHT=$MIN_HEIGHT
  92. fi
  93. if [ $HEIGHT -lt $MIN_HEIGHT ]; then
  94. continue
  95. fi
  96. # Skip files is low bitrates
  97. SIZE="`stat -f %z "${i}" 2>/dev/null`"
  98. if [ -z "${SIZE}" ]; then
  99. SIZE=1
  100. fi
  101. LENGTH="`~/bin/video/movInfo.pl "${i}" LENGTH 2>/dev/null | sed 's%\..*$%%'`"
  102. if [ -z "${LENGTH}" ] || [ $LENGTH -eq 0 ]; then
  103. LENGTH=1
  104. fi
  105. RATE=$(( $SIZE / $LENGTH ))
  106. if [ $RATE -le $MIN_RATE ]; then
  107. continue
  108. fi
  109. # If we're still around, the file matched all criteria
  110. if [ $ZERO -gt 0 ]; then
  111. echo -en "${i}\0"
  112. else
  113. echo "${i}"
  114. fi
  115. done
  116. IFS="${OLDIFS}"
  117. # Record the last scan time
  118. if [ -n "${LAST_RECODE_FILE}" ]; then
  119. touch "${LAST_RECODE_FILE}" >/dev/null 2>&1
  120. fi