scripts/srtadrm

52 lines
1011 B
Bash
Executable File

#!/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"
"CARDROOM.COM"
"[Cc]ardroom.com"
"PROJECT WARLOCK"
)
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}