zaterdag 5 oktober 2013

Parsing subtitle srt files and add seconds Bash OSX

Same as java example writen in bash osx shell.
#!/bin/sh
#
# Check input parameters
#
if [[ -z ${2} ]]; then
 echo "Usage:SubTitle.sh filename.srt +5|-5"
 exit
fi

FILENAME="$1"
DIFFSECONDS="$2"

#
# 00:41:01,345
#
function addTimeSeconds(){
 mMilli=`echo $1|cut -d',' -f2`   # 345
 mSeconds=`echo $1|cut -d',' -f1` # 00:41:01
 # Parse time to seconds
 mSeconds=$(date -j -f "%Y-%m-%d %H:%M:%S" "2000-01-01 ${mSeconds}" "+%s")
 # Add extra seconds
 mSeconds=$((mSeconds + $DIFFSECONDS))
 # Parse time to format HH:MM:SS
 mSeconds=`date -j -f "%s" "$mSeconds" "+%H:%M:%S"`
 printf "${mSeconds},${mMilli}"
}

function addTime(){
 mFromTime=$(addTimeSeconds "$1")
 mToTime=$(addTimeSeconds "$3")
 echo "${mFromTime} --> ${mToTime}"
}

while read line; do
 if [[ $line == *"-->"* ]] ; then
  addTime $line
 else
  echo "$line"
 fi
done < "${FILENAME}"

Geen opmerkingen:

Een reactie posten