tvdup.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. set failglob
  3. BASE="/mnt/media/TV"
  4. NOW="`date '+%s'`"
  5. THEN=$(( $NOW - 3600 ))
  6. function updateCount() {
  7. count=0
  8. for i in "${dir}/${num} - "*; do
  9. if echo "${i}" | grep -q '.nfo$'; then
  10. continue
  11. fi
  12. if echo "${i}" | grep -q '.srt$'; then
  13. continue
  14. fi
  15. count=$(( $count + 1 ))
  16. done
  17. }
  18. declare -A seen
  19. while IFS= read -r -d '' lpath; do
  20. # Reset the loop
  21. count=0
  22. file="`basename "${lpath}"`"
  23. dir="`dirname "${lpath}"`"
  24. num="`echo "${file}" | sed 's%\([0-9]*\).*%\1%'`"
  25. # Only process things in a format we understand
  26. if [ -z "${num}" ]; then
  27. echo "Error on: ${lpath}" 1>&2
  28. continue
  29. fi
  30. # Skip if we're down to one
  31. updateCount
  32. if [ $count -lt 2 ]; then
  33. continue
  34. fi
  35. # Build a list of checked files and skip dups
  36. id="${dir}/${num} - "
  37. if [ -n "${seen["${id}"]}" ]; then
  38. continue
  39. fi
  40. seen["${id}"]=1
  41. # Skip things modified since $THEN
  42. if [ "`uname`" == "Darwin" ]; then
  43. MTIME="`stat -f '%m' "${id}"*`"
  44. else
  45. MTIME="`stat -c '%Y' "${id}"*`"
  46. fi
  47. MTIME="`echo "${MTIME}" | sort | head -n 1`"
  48. if [ $MTIME -gt $THEN ]; then
  49. echo "Skipping recent file: ${lpath}" 1>&2
  50. continue
  51. fi
  52. # Use mkvRename (which protects against small files internally)
  53. sudo chattr -i "${id}"*
  54. QUIET=1 ~/bin/video/mkvRename "${lpath}"
  55. # Skip if we're down to one
  56. updateCount
  57. if [ $count -lt 2 ]; then
  58. continue
  59. fi
  60. # Human intervention
  61. sudo chattr -i "${id}"*
  62. touch "${id}"*
  63. echo "Multiple matches for: ${id}" 1>&2
  64. short="`echo "${id}" | sed "s%^${BASE}/%%"`"
  65. (cd "${BASE}" && ls -lhtQ "${short}"*)
  66. done< <(find "${BASE}" -type f -path '*Season*' -name '[0-9]*.*' -print0)