Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Renames subtitles files according to tv shows names found in a directory
  4. # Acceped syntaxes for season/episode are: 304, s3e04, s03e04, 3x04, Season 03 Episode 04 (case insensitive)
  5. #
  6. # Usage:
  7. # Put this gist somewhere in your $PATH, like /usr/local/bin/subtitles-renamer
  8. # Chmod +x it
  9. # cd ~/YourHolidaysTvShowsWithSubtitles
  10. # subtitles-renamer
  11. #
  12. # Note: zipfiles will be unzipped and .zip will be removed
  13. #
  14. # There are bashisms to work with regular expressions,
  15. # so you really need bash or a shell compatible
  16.  
  17.  
  18. # unzip files, maybe there are subtitles in it...
  19. for f in *.zip; do
  20. if [ -e "$f" ]; then
  21. unzip "$f"
  22. rm "$f"
  23. fi
  24. done
  25.  
  26. # switch into case insensitive
  27. shopt -s nocasematch
  28.  
  29. # search subtitles
  30. for f in *.{srt,ssa,sub} ; do
  31. if [ -e "$f" ]; then
  32. if [[ "$f" =~ s([0-9]+)e([0-9]+) || "$f" =~ ([0-9]+)x([0-9]+) || "$f" =~ ([0-9]+)([0-9][0-9]) ]]; then
  33. echo "Found '$f'"
  34. let SEASON="10#${BASH_REMATCH[1]}" # eventually delete leading 0
  35. EPISODE=${BASH_REMATCH[2]}
  36.  
  37.  
  38. # search for a matching film
  39. for movie in *.{avi,mkv} ; do
  40. if [ -e "$movie" ]; then
  41. if [[ "$movie" =~ ${SEASON}${EPISODE} || "$movie" =~ s0?${SEASON}e${EPISODE} || "$movie" =~ ${SEASON}x${EPISODE} || "$movie" =~ (Season 0?${SEASON} Episode ${EPISODE}) ]]; then
  42.  
  43. NEW_NAME=`echo "${movie%.*}.${f##*.}"`
  44. if [ "$f" = "${NEW_NAME}" ]; then
  45. echo " Already ok"
  46. elif [ -e "${NEW_NAME}" ]; then
  47. echo " A file named '${NEW_NAME}' already exist, skipping"
  48. else
  49. mv "$f" "${NEW_NAME}"
  50. echo " Renamed '$f' in '${NEW_NAME}'"
  51. fi
  52. break;
  53. fi
  54. fi
  55. done
  56. fi
  57. fi
  58. done
  59.  
  60.  
  61. # reswitch into case sensitive
  62. shopt -u nocasematch
  63.  
  64. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement