Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # REDDIT EDITION
- # 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
- # please consider leaving or modifying this regex to properly credit the hard work that is put into providing these subtitles
- ### usage:
- ## Download this file from the command line to your current directory:
- # curl https://raw.githubusercontent.com/brianspilner01/media-server-scripts/master/sub-clean.sh > sub-clean.sh && chmod +x sub-clean.sh
- ## Run this script across your whole media library:
- # find /path/to/library -name '*.srt' -exec /path/to/sub-clean.sh "{}" \;
- ## Add to Bazarr (Settings > Subtitles > Use Custom Post-Processing > Post-processing command):
- # /path/to/sub-clean.sh "{{subtitles}}" --
- ## Add to Sub-Zero (in Plex > Settings > under Manage > Plugins > Sub-Zero Subtitles > Call this executable upon successful subtitle download (near the bottom):
- # /path/to/sub-clean.sh %(subtitle_path)s
- ## Test out what lines this script would remove:
- # 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]|$)|©|™'
- # REGEX_TO_REMOVE2='opensubtitles|sub(scene|rip)|podnapisi|addic7ed|titlovi|bozxphd|sazu489|psagmeno|normita|anoxmous|isubdb|americascardroom'
- # awk 'tolower($0) ~ '"/$REGEX_TO_REMOVE/" RS='' ORS='\n\n' "/path/to/sub.srt"
- # awk 'tolower($0) ~ '"/$REGEX_TO_REMOVE2/" RS='' ORS='\n\n' "/path/to/sub.srt"
- # specify file ownership
- CHMOD=666
- SUB_FILEPATH="$1"
- # check usage
- [ ! -f "$SUB_FILEPATH" ] && { echo "usage: sub-clean.sh [FILE]" ; echo "Warning: subtitle file does not exist" ; exit 1 ; }
- # RUN on Synology:
- # find /<PATH>/Movies/ -not -path "*/@eaDir/*" -not -path "*/#snapshot/*" -not -path "*/#recycle/*" -name "*.srt" -exec /<PATH>/sub-clean.sh "{}" \;
- # lowercase list of regex (gore/magic?) that will be removed from srt
- REGEX_TO_REMOVE='(br|dvd|web).?(rip|scr)|english (- )?us|(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|\.(com|pl|link|org|net|mp4|mkv|avi|pdf)([^a-z]|$)'
- # regex lists seperated for compatibility with old implementations of awk that require <400 characters
- REGEX_TO_REMOVE2='opensubtitles|sub(scene|rip)|podnapisi|addic7ed|titlovi|bozxphd|sazu489|psagmeno|normita|anoxmous|isubdb|americascardroom'
- if [ "$(echo "$SUB_FILEPATH" | grep '\.srt$')" ] # only operate on srt files
- then
- # convert any DOS formatted files to UNIX (remove carriage return line endings)
- awk '{ sub("\r$", ""); print }' "$SUB_FILEPATH" > "/tmp/sub-clean3.tmp" # && mv "${SUB_FILEPATH}.bak" "$SUB_FILEPATH"
- ### 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.:
- # LINE NUMBER
- # TIMESTAMP --> TIMESTAMP
- # SUB LINE 1
- # SUB LINE 2
- # ...
- #
- awk 'tolower($0) !~ /'"$REGEX_TO_REMOVE"'/ { $1 = VAR++ ; print ; next } { print >> TRASH }' RS='' FS='\n' OFS='\n' ORS='\n\n' VAR=1 TRASH="/tmp/sub-clean.trash.tmp" "/tmp/sub-clean3.tmp" > "/tmp/sub-clean2.tmp" && \
- # mv "$SUB_FILEPATH.tmp" "$SUB_FILEPATH" && \
- awk 'tolower($0) !~ /'"$REGEX_TO_REMOVE2"'/ { $1 = VAR++ ; print ; next } { print >> TRASH }' RS='' FS='\n' OFS='\n' ORS='\n\n' VAR=1 TRASH="/tmp/sub-clean.trash.tmp" "/tmp/sub-clean2.tmp" > "/tmp/sub-clean.tmp" && \
- # ls -al "$SUB_FILEPATH" && \
- # ls -al "/tmp/sub-clean.tmp" && \
- [[ $(stat -c %s /tmp/sub-clean.tmp) != $(stat -c %s "$SUB_FILEPATH") ]] && mv "/tmp/sub-clean.tmp" "$SUB_FILEPATH"
- # chmod $CHMOD "$SUB_FILEPATH" && \
- echo "sub-clean.sh succesfully processed $SUB_FILEPATH"
- if [ -f "/tmp/sub-clean.trash.tmp" ]
- then
- REMOVED_LINES=$(cat "/tmp/sub-clean.trash.tmp")
- rm "/tmp/sub-clean.trash.tmp"
- if [ "$REMOVED_LINES" ]
- then
- echo "The following lines were removed:"
- echo "$REMOVED_LINES"
- fi
- fi
- [ -f "/tmp/sub-clean3.tmp" ] && rm "/tmp/sub-clean3.tmp"
- [ -f "/tmp/sub-clean2.tmp" ] && rm "/tmp/sub-clean2.tmp"
- [ -f "/tmp/sub-clean.tmp" ] && rm "/tmp/sub-clean.tmp"
- else
- echo "Provided file must be .srt"
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement