Srt2vtt in Shell Script (Beta)
Publicado por Perfil removido (última atualização em 17/12/2018)
[ Hits: 1.735 ]
Este script converte legendas.srt para webvtt utilizando ffmpeg, um programa em linha de comando que é composto de uma coleção de software livre e bibliotecas de código aberto.
Para converter a legenda.srt você precisa informar a codificação (encode) da legenda.srt, por exemplo: ASCII text
Uma forma simples de descobrir qual é a codificação do arquivo é utilizando o comando file. Você também pode baixar o encode.txt ou copiar e colar a lista de codificações: https://trac.ffmpeg.org/attachment/ticket/2431/sub_charenc_parameters.txt
Renomeie o download acima para encode.txt. O arquivo encode.txt deve estar no seu diretorio $HOME.
#/usr/bin/env bash
# PATH
log_path=/tmp/srt2vtt.log # the output of ffmpeg
list_path=$(find $HOME -name enccode.txt) # ffmpeg sub charenc parameters (2006)
if [ -z $list_path ];then list_not_found=true ;else list_not_found=false ;fi
# COLORS
red="\033[1;31m"
green="\033[1;32m"
end="\033[0m"
srt=$2 # subtile.srt
check=$(file asd.srt | cut -d':' -f2 | tr -d [:blank:])
# verifies that ffmpeg is installed
if [[ ! -e $(which ffmpeg) ]];then echo -e "${red}ffmpeg isn't installed ${end}" ; exit 1 ;fi
_whitespaces(){
if [ -n "$srt" -a -e "$srt" ];then
file $srt
if [ $? -ne 0 ];then
echo -e "${red}\nError: whitespaces "
echo -e "Please run srt2vtt -r to Rename the subtitle${end}\n"
exit 1
fi
fi
}
_fileExist(){
if ! [ -e "$srt" ];then
echo -e "${red}\nError: no such file or directory${end}"
exit 1
fi
}
_HELP_(){
echo """
srt2vtt version 1.0
Usage: srt2vtt <option> <subtitle.srt>
-c ) Convert srt to vtt whitout verbose
-h ) show this Help message
-v ) enable Verbose mode and convert
-r ) Rename the subtitle (use in case of the sub has whitespaces)
-l ) shows the last sub log
by erickDarko 2018
"""
exit 0
}
_rename(){
if [[ ! -e $(which python3) ]];then echo -e "${red}python3 isn't installed ${end}" ; exit 1 ;fi
echo """
from os import listdir
ls = listdir()
for c,v in enumerate(ls):
if 'srt' in v:
print('mv',end=' ')
print('\"{}\" \"{}\"'.format(ls[c],v.replace(' ','_')))
print()
""" > rename.py
python3 rename.py > rename
chmod +x rename ; ./rename 2> /dev/null ; sleep 0.5s ; rm rename*
echo ; exit 0
}
_convert(){
if [ "$check" == "empty" ];then echo -e "${red}Error: empty file${end}"; exit 2 ;fi
_fileExist # verifies if the file exist
_whitespaces # verifies whitespaces
len=$(expr ${#srt} - 4) # length of the subtile without the extension ".srt"
vtt=$(echo $srt | cut -c-$len)
if [ -e $vtt.vtt ];then rm -iv $vtt.vtt ;fi # delete the subtitle if it already exists
encode=$(file $srt | cut -d":" -f2 | cut -d" " -f2) # get the file encode
if [ $list_not_found == false ];then
# verifies if the encode is on the $list_path
list=$(cat $list_path | grep "$encode")
if [ $1 == false ];then
for enc in $list;do
ffmpeg -fix_sub_duration -sub_charenc $enc -i $srt $vtt.vtt &> $log_path
done
else
for enc in $list;do
ffmpeg -fix_sub_duration -sub_charenc $enc -i $srt $vtt.vtt 0> $log_path
done
fi
if [ $? -ne 0 ];then
echo -ne "${red}Error: ${end}"
echo -e "${red}$(cat $log_path | tail -n1)${end}"
rm $vtt.vtt &> /dev/null
exit 2
fi
else
while true;do
echo "File information: $(file $srt)"
read -p "File encode: " encode
if [ $1 == false ];then
ffmpeg -fix_sub_duration -sub_charenc $encode -i $srt $vtt.vtt &> $log_path
else
ffmpeg -fix_sub_duration -sub_charenc $encode -i $srt $vtt.vtt 0> $log_path
fi
if [ $? -ne 0 ];then
echo -ne "${red}Error: ${end}"
echo -e "${red}$(cat $log_path | tail -n1)${end}"
rm $vtt.vtt &> /dev/null
exit 2
fi
break
done
fi
}
# srt2vtt parameters
case $1 in
-h) _HELP_ ;;
--help) __HELP__ ;;
-c) _convert false ;;
-v) _convert true ;;
-r) _rename ;;
-l) less $log_path ;;
*) echo -e "${red}Error: no option was given${end}" ; _HELP_ ;;
esac
if [ -e $vtt.vtt ];then
echo -e "\nENCODE: $encode"
l=$(wc -l $vtt.vtt | cut -d" " -f1) # lines in the subtitle
if [ $l -eq 0 ];then
echo -e "[ ${red}${l}${end} ]"
else
echo -e "[ ${green}${l}${end} ]"
rm -iv $srt # remove the original subtitle
fi
fi
vcut - Recortar vídeos usando ffmpeg
Checar a carga da bateria (modo texto) com barra de status versão 1.0
Nenhum comentário foi encontrado.
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
IA chega ao desktop e impulsiona produtividade no mundo Linux
Novos apps de produtividade, avanços em IA e distros em ebulição agitam o universo Linux
Como instalar o repositório do DBeaver no Ubuntu
Como instalar o Plex Media Server no Ubuntu
Digitando underscore com "shift" + "barra de espaços"
Como ativar a lixeira e recuperar aquivos deletados em um servidor Linux
Como mudar o nome de dispositivos Bluetooth via linha de comando
O programa assinador digital (0)
dpkg: erro: gatilho de arquivo duplicado chamado pelo arquivo de nome (6)
Instalação não está resolvendo as dependencias (2)
Captação de áudio no zorin linux começa a diminuir com o tempo (5)









