Skip to content

Instantly share code, notes, and snippets.

@xgvargas
Last active August 13, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xgvargas/4767a3578002b8fbc0fa3b6e9b4b6baa to your computer and use it in GitHub Desktop.
Save xgvargas/4767a3578002b8fbc0fa3b6e9b4b6baa to your computer and use it in GitHub Desktop.
Bash script to download subtitles
#!/bin/bash
agent="SubDB/1.0 (autoSubtitle/0.1; http://github.com)"
process_file_f() {
ffull="$1"
# fpath=${ffull%/*}
ffn=${ffull##*/}
fext=${ffn##*.}
fname=${ffn%.*}
printf "\n---\nProcessando: $ffull ...\n"
tmp=$(mktemp)
dd if="$ffull" bs=1 count=65536 of=$tmp >& /dev/null
# size=$(ls -l "$ffull" | awk '{print $5}')
size=$(wc -c < "$ffull")
skip=$((size-65536))
dd if="$ffull" bs=1 count=65536 of=$tmp skip=$skip oflag=append conv=notrunc >& /dev/null
hash=$(md5sum -b $tmp | awk '{print $1}')
rm $tmp
printf "\nHash: $hash\n"
langs=$(curl -X GET -s -H "User-Agent: $agent" "http://api.thesubdb.com/?action=search&hash=$hash&versions")
printf "Linguagens: $langs\n"
if [[ $langs = *"pt"* ]]; then
printf "Baixando portugues..."
tmp=$(mktemp)
curl -X GET -is -H "User-Agent: $agent" -o $tmp "http://api.thesubdb.com/?action=download&hash=$hash&language=pt"
ext=$(cat $tmp | dos2unix | awk -F. '/filename=/{print $NF}')
# outfn="$fname-pt.$ext"
outfn="$fname.$ext"
if [ -f "$outfn" ]; then cp -f "$outfn"{,.bak}; fi
cat $tmp | dos2unix | sed '1,/^$/d' > "$outfn"
rm $tmp
printf " ok --> $outfn\n\n"
elif [[ $langs = *"en"* ]]; then
printf "Baixando ingles..."
tmp=$(mktemp)
curl -X GET -is -H "User-Agent: $agent" -o $tmp "http://api.thesubdb.com/?action=download&hash=$hash&language=en"
ext=$(cat $tmp | dos2unix | awk -F. '/filename=/{print $NF}')
outfn="$fname-en.$ext"
if [ -f "$outfn" ]; then cp -f "$outfn"{,.bak}; fi
cat $tmp | dos2unix | sed '1,/^$/d' > "$outfn"
rm $tmp
printf " ok --> $outfn\n\n"
else
printf "\nNada pra baixar...\n\n"
fi
}
if [[ -d $1 ]]; then
cd "$1"
shopt -s nullglob nocaseglob
for video in *.{avi,mkv,mp4,mov,mpg,wmv}; do
size=$(wc -c < "$video")
if [ $size -ge 20000000 ]; then # muito pequeno, deve ser uma amostra... vamos ignorar...
process_file_f "$video"
fi
done
elif [[ -f "$1" ]]; then
file=$(realpath $1);
cd ${file%/*} # isso espande para o path do arquivo
process_file_f "$1"
else
printf "\nErro: Sem argumentos!\n\n\tUso: $0 <arquivo de video | diretorio com multiplos videos>\n\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment