Advertisement
Guest User

remove-ads

a guest
Feb 9th, 2024
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.07 KB | None | 0 0
  1. #!/bin/sh
  2. # cleans srt formatted subtitles of common blocks that may be considered unwanted, works well as a post-process script for software such as Bazarr or Sub-Zero
  3. # please consider leaving or modifying this regex to properly credit the hard work that is put into providing these subtitles
  4.  
  5. ### usage:
  6. ## Download this file from the command line to your current directory:
  7. # curl https://raw.githubusercontent.com/brianspilner01/media-server-scripts/master/sub-clean.sh > sub-clean.sh && chmod +x sub-clean.sh
  8. ## Run this script across your whole media library:
  9. # find /path/to/library -name '*.srt' -exec /path/to/sub-clean.sh "{}" \;
  10. ## Add to Bazarr (Settings > Subtitles > Use Custom Post-Processing > Post-processing command):
  11. # /path/to/sub-clean.sh "{{subtitles}}" --
  12. ## Add to Sub-Zero (in Plex > Settings > under Manage > Plugins > Sub-Zero Subtitles > Call this executable upon successful subtitle download (near the bottom):
  13. # /path/to/sub-clean.sh %(subtitle_path)s
  14. ## Test out what lines this script would remove:
  15. # REGEX_TO_REMOVE='(br|dvd|web).?(rip|scr)|english (- )?us|sdh|srt|(yahoo|mail|book|fb|4m|hd)\. ?com|(sub(title)?(bed)?(s)?(fix)?|encode(d)?|correct(ed|ion(s)?)|caption(s|ed)|sync(ed|hroniz(ation|ed))?|english)(.pr(esented|oduced))?.?(by|&)|[^a-z]www\.|http|\. ?(co|pl|link|org|net|mp4|mkv|avi|pdf)([^a-z]|$)|©|™'
  16. # REGEX_TO_REMOVE2='opensubtitles|sub(scene|rip)|podnapisi|addic7ed|titlovi|bozxphd|sazu489|psagmeno|normita|anoxmous|isubdb|americascardroom'
  17. # awk 'tolower($0) ~ '"/$REGEX_TO_REMOVE/" RS='' ORS='\n\n' "/path/to/sub.srt"
  18. # awk 'tolower($0) ~ '"/$REGEX_TO_REMOVE2/" RS='' ORS='\n\n' "/path/to/sub.srt"
  19.  
  20. # specify file ownership
  21. CHMOD=666
  22.  
  23. SUB_FILEPATH="$1"
  24.  
  25. # check usage
  26. [ ! -f "$SUB_FILEPATH" ] && { echo "usage: sub-clean.sh [FILE]" ; echo "Warning: subtitle file does not exist" ; exit 1 ; }
  27.  
  28. # lowercase list of regex (gore/magic?) that will be removed from srt
  29. REGEX_TO_REMOVE='(br|dvd|web).?(rip|scr)|english (- )?us|sdh|srt|(yahoo|mail|facebook|fb|4m|hd)\. ?com|(sub(title)?(bed)?(s)?(fix)?|encode(d)?|correct(ed|ion(s)?)|caption(s|ed)|sync(ed|hroniz(ation|ed))?|english)(.pr(esented|oduced))?.?(by|&)|[^a-z]http|\. ?(co|pl|link|mp4|mkv|avi|pdf)([^a-z]|$)|©|™'
  30. # regex lists seperated for compatibility with old implementations of awk that require <400 characters
  31. REGEX_TO_REMOVE2='opensubtitles|sub(scene|rip)|podnapisi|addic7ed|titlovi|bozxphd|sazu489|psagmeno|normita|anoxmous|isubdb|americascardroom'
  32.  
  33. if [ "$(echo "$SUB_FILEPATH" | grep '\.srt$')" ] # only operate on srt files
  34. then
  35.     cp "$SUB_FILEPATH" "$SUB_FILEPATH.original"
  36.     # convert any DOS formatted files to UNIX (remove carriage return line endings)
  37.     awk '{ sub("\r$", ""); print }' "$SUB_FILEPATH" > "${SUB_FILEPATH}.bak" && mv "${SUB_FILEPATH}.bak" "$SUB_FILEPATH"
  38.  
  39.     ### each record (in awk) is defined as a block of srt formatted subs (record seperator RS is essentially \n\n+, see docs), with each line of the block a seperate field .i.e.:
  40.     # LINE NUMBER
  41.     # TIMESTAMP --> TIMESTAMP
  42.     # SUB LINE 1
  43.     # SUB LINE 2
  44.     # ...
  45.     #
  46.  
  47.     awk 'tolower($0) !~ /'"$REGEX_TO_REMOVE"'/ { $1 = VAR++ ; print ; next } { print >> TRASH }' RS='' FS='\n' OFS='\n' ORS='\n\n' VAR=1 TRASH="$SUB_FILEPATH.trash.tmp" "$SUB_FILEPATH" > "$SUB_FILEPATH.tmp" && \
  48.     mv "$SUB_FILEPATH.tmp" "$SUB_FILEPATH" && \
  49.     awk 'tolower($0) !~ /'"$REGEX_TO_REMOVE2"'/ { $1 = VAR++ ; print ; next } { print >> TRASH }' RS='' FS='\n' OFS='\n' ORS='\n\n' VAR=1 TRASH="$SUB_FILEPATH.trash.tmp" "$SUB_FILEPATH" > "$SUB_FILEPATH.tmp" && \
  50.     mv "$SUB_FILEPATH.tmp" "$SUB_FILEPATH" && \
  51.     chmod $CHMOD "$SUB_FILEPATH" && \
  52.     echo "sub-clean.sh succesfully processed $SUB_FILEPATH"
  53.  
  54.     if [ -f "$SUB_FILEPATH.trash.tmp" ]
  55.     then
  56.         REMOVED_LINES=$(cat "$SUB_FILEPATH.trash.tmp")
  57.         # rm "$SUB_FILEPATH.trash.tmp"
  58.  
  59.         if [ "$REMOVED_LINES" ]
  60.         then
  61.             echo "The following lines were removed:"
  62.             echo "$REMOVED_LINES"
  63.         fi
  64.     else
  65.         rm "$SUB_FILEPATH.original"
  66.     fi
  67.  
  68. else
  69.     echo "Provided file must be .srt"
  70.     exit 1
  71. fi
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement