Srt2vtt in Shell Script (Beta)

Publicado por Perfil removido (última atualização em 17/12/2018)

[ Hits: 1.360 ]

Download srt2vtt.sh




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.

  



Esconder código-fonte

#/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

Scripts recomendados

Maiusculas / Minusculas

Renomear Arquivos

Renomear Extensão de Arquivos

Localizar arquivos por extensão e mostrar relatório

Configurando interfaces de rede


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts