Skip to content

Instantly share code, notes, and snippets.

@tastytea
Last active January 21, 2020 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tastytea/c30faf0c5609b08f3099c943a2acb5ba to your computer and use it in GitHub Desktop.
Save tastytea/c30faf0c5609b08f3099c943a2acb5ba to your computer and use it in GitHub Desktop.
Remove ads from SRT files
#!/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