Advertisement
roderickvd

RenameMKV.sh

Dec 5th, 2013
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.31 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. ##############################################################################
  4. ### NZBGET POST-PROCESSING SCRIPT                                          ###
  5.  
  6. # Rename garbled MKV files to season and episode numbering.
  7. # Useful for QoQ releases.
  8.  
  9. # Directory allowed to rename files in (should be equal to tv category DestDir)
  10. #AllowedDir=/volume1/video/tv
  11.  
  12. ### NZBGET POST-PROCESSING SCRIPT                                          ###
  13. ##############################################################################
  14.  
  15. # Tested on Synology DSM 4.3-3810 Update 2 (BusyBox v1.16.1)
  16.  
  17. # INSTALLATION INSTRUCTIONS:
  18. #
  19. #  1. Place script in ppscripts directory,
  20. #       e.g. /usr/local/ppscripts/RenameMKV.sh
  21. #
  22. #  2. Make script executable:
  23. #       chmod a+x /usr/local/ppscripts/RenameMKV.sh
  24. #
  25. #  3. Restart NZBGet
  26. #
  27. #  4. NZBGet Settings > RenameMKV: set AllowedDir to tv category DestDir,
  28. #       e.g. /volume1/video/tv
  29. #
  30. #  5. NZBGet Settings > Post-Processing Scripts: move RenameMKV.sh after DeleteSamples.py,
  31. #       e.g. DeleteSamples.py, RenameMKV.sh, nzbToMedia*.py, Email.py, Logger.py
  32. #
  33. #  6. NZBGet Settings > Categories: set RenameMKV.sh as DefScript for tv category,
  34. #       e.g. RenameMKV.sh, nzbToMedia/nzbToSickBeard.py
  35. #
  36. #  NZBGet will now execute RenameMKV.sh for tv NZBs added from this point onwards.
  37. #  Manually change the PP-Parameters for any tv NZBs that were already queued.
  38.  
  39. # CHANGELOG:
  40. #
  41. #  2013/12/08
  42. #   * [FIX] Typo prevented AllowedDir to work with non-default settings
  43. #   * [ADD] Strip year from NZB name
  44. #   * [ADD] AllowedDir error message now includes directories for debugging
  45.  
  46. ALLOWED_DIR=$NZBPP_ALLOWEDDIR
  47. if [ "$ALLOWED_DIR" = "" ]; then
  48.         ALLOWED_DIR="/volume1/video/tv"
  49. fi
  50.  
  51. # Sanity checks: require NZB post-processing directory and name from NZBGet
  52. if [ "$NZBPP_DIRECTORY" == "" ]; then
  53.         echo "[ERROR] Empty directory name, skipping."
  54.         exit 95
  55. fi
  56. if [ "$NZBPP_NZBNAME" == "" ]; then
  57.         echo "[ERROR] Empty NZB name, skipping."
  58.         exit 95
  59. fi
  60.  
  61. echo "[INFO] Fixing MKV filenames in $NZBPP_DIRECTORY for $NZBPP_NZBNAME"
  62.  
  63. # Sanity check: disallow post-processing directory to be root or the working directory
  64. if [ "$NZBPP_DIRECTORY" == "." -o "$NZBPP_DIRECTORY" == "/" ]; then
  65.         echo "[ERROR] Illegal directory name, skipping."
  66.         exit 95
  67. fi
  68.  
  69. # Sanity check: require post-processing directory to contain AllowedDir
  70. if [ `echo "$NZBPP_DIRECTORY" | grep "$ALLOWED_DIR" | wc -l` == "0" ]; then
  71.         echo "[ERROR] Directory $NZBPP_DIRECTORY not in allowed $ALLOWED_DIR, skipping."
  72.         exit 95
  73. fi
  74.  
  75. # Find files which already have season and episode numbering
  76. if [ `find "$NZBPP_DIRECTORY" -maxdepth 1 -type f -regex '.*[sS][0-9][0-9]*[eE][0-9][0-9]*.*\.[mM][kK][vV]$' | wc -l` == "0" ]; then
  77.         # No parsed files found, try to find MKV files now
  78.         NZB_NAME=`echo $NZBPP_NZBNAME | sed 's/([0-9][0-9][0-9][0-9])//g' | sed 's/  / /g' | sed 's/\.\./\./g'`
  79.         echo "[DETAIL] Renaming MKV files to $NZB_NAME.mkv"
  80.         find "$NZBPP_DIRECTORY" -name *.mkv -exec mv {} "$NZBPP_DIRECTORY/$NZB_NAME.mkv" \;
  81.         RESULT=$?
  82.         if ! [ "$RESULT" == 0 ]; then
  83.                 echo "[ERROR] Error $RESULT"
  84.                 exit 94
  85.         fi
  86. else
  87.         echo "[DETAIL] No MKV files to rename."
  88. fi
  89.  
  90. exit 93
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement