Last active
January 21, 2020 02:55
-
-
Save tastytea/c30faf0c5609b08f3099c943a2acb5ba to your computer and use it in GitHub Desktop.
Remove ads from SRT files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Remove ads from SRT files with a list of POSIX extended regular expressions | |
zmodload zsh/regex | |
file="${1}" | |
newfile="${1}.tmp" | |
ad_re=( | |
"Advertise your product or brand here" | |
"OpenSubtitles.org" | |
"Please rate this subtitle" | |
"[Ff]lix[Tt]or\.to" | |
) | |
match=0 | |
if [ -x "$(which dos2unix)" ]; then | |
filter="dos2unix" | |
else | |
filter="cat" | |
fi | |
if [ -z ${1} ]; then | |
print "usage: $(basename ${0}) FILE" >&2 | |
exit 1 | |
fi | |
if [ -e ${newfile} ]; then | |
rm ${newfile} | |
fi | |
# Read SRT file, convert to UNIX line endings, split to blocks | |
srt=("${(@ps/\n\n/)$(${filter} < ${file})}") | |
for block in ${srt[@]}; do | |
for ad in ${ad_re[@]}; do | |
if [[ "${block}" -regex-match "${ad}" ]]; then | |
match=1 | |
break | |
fi | |
done | |
# Only write block to new file if no ad was found | |
if [ $match -eq 0 ]; then | |
print "${block}\n" >> ${newfile} | |
fi | |
match=0 | |
done | |
mv ${newfile} ${file} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment