# !/bin/bash # srt2sami # command-line utility to convert srt subtitle files to sami subtitles. # count_zero 2007 # Distributed under the GPLv2 license inputfile=${1/\ /_} inputfile_temp=${inputfile}-temp inputfile2=${inputfile}2 outputfile=${inputfile/.srt/.smi} cat "$1" | sed 's/[[:space:]]*$//g' > $inputfile_temp mv $inputfile_temp $inputfile2 time_conversion () { # function to convert timeframes from srt to smi # 00:00:00,000 (srt timeframe)--> milliseconds (sami timeframe) unset time hours minutes seconds milliseconds total_milliseconds time=$1 hours=`echo $time | cut -c -2 | sed 's/^0*//'` hours=$(( hours * 60 * 60 * 1000 )) minutes=`echo $time | cut -c -5 | cut -c 4- | sed 's/^0*//'` minutes=$(( minutes * 60 * 1000 )) seconds=`echo $time | cut -c -8 | cut -c 7- | sed 's/^0*//'` seconds=$(( seconds * 1000 )) milliseconds=`echo $time | cut -c 10- | sed 's/^0*//'` total_milliseconds=$(( hours + minutes + seconds + milliseconds )) echo $total_milliseconds } # Create outputfile with sami header information echo ' ' > $outputfile echo -n 'Converting file' # Read each line of srt file and convert to sami format while read line do echo -n . if [[ -n `echo "$line" | sed -n 's/-->/&/p'` ]] then starttime=`echo $line | sed 's/\ -->.*//'` starttime=$(time_conversion $starttime) endtime=`echo $line | sed 's/^.*-->\ //' | sed 's/[^0-9:, ]//g'` endtime=$(time_conversion $endtime) echo " " >> $outputfile check=1 elif [[ -z "$starttime" ]] then : elif [[ -z `echo "$line" | sed '/^[[:space:]]*$/d'` ]] || [[ -z `echo "$line" | sed '/^$/d'` ]] then echo >> $outputfile elif [[ -z `echo "$line" | sed '/^[0-9]*[[:space:]]*$/d'` ]] then echo " " >> $outputfile echo "

 " >> $outputfile else if [[ "$check" = 1 ]] then echo -n "

$line
" >> $outputfile check=0 else echo -n "$line" >> $outputfile fi fi unset line done < $inputfile2 echo -e ''\\n'' >> $outputfile cat $outputfile | sed 's/
$//' > ${outputfile}temp mv ${outputfile}temp $outputfile rm -f $inputfile2 echo $outputfile created. echo Done!