Skip to content

Instantly share code, notes, and snippets.

@tamirko
Created May 11, 2017 07:38
Show Gist options
  • Save tamirko/c92e6e5c2a80914358ffd8f925a5f334 to your computer and use it in GitHub Desktop.
Save tamirko/c92e6e5c2a80914358ffd8f925a5f334 to your computer and use it in GitHub Desktop.
Concatenate several srt file into one file with renumbering all the entries and shifting all the timestamps
#!/bin/bash
clear
# Install this 1st :
# sudo apt install subtitleripper
cd ~/tmp/test1005
export rawTotalSrt="rawTotal.srt"
export totalSrt="total.srt"
rm -f ${rawTotalSrt} ${totalSrt}
EPOCH='jan 1 1970'
sum=0
first_srt=`ls $1/*.srt | grep "1_"`
prevVid=`ls $1/*.mp4 | grep "1_"`
prevVidDuration=`ffmpeg -i ${prevVid} |& grep -i "Duration" | awk -F " " '{ print $2 }' | sed 's/,//g' | sed 's/\./,/g'`
cp $first_srt .
previous_srt=$first_srt
echo "${prevVid} ${prevVidDuration}"
echo "${first_srt}"
sum="$(date -u -d "$EPOCH $prevVidDuration" +%s) + $sum"
sumBc=`echo ${sum}|bc`
currHostName=`hostname`
currFilename=$(basename "$0")
for currVideo in `ls $1/*.mp4 | grep -v "1_"`; do
echo "Current total sum is ${sum}"
echo "Current sumBc is ${sumBc}"
echo "------------------------"
#echo "${currVideo} ..."
vidDuration=`ffmpeg -i ${currVideo} |& grep -i "Duration" | awk -F " " '{ print $2 }' | sed 's/,//g' | sed 's/\./,/g'`
echo "${currVideo} ${vidDuration}"
currSrt="${currVideo%.*}.srt"
currStrFilename=$(basename "$currSrt")
#cp $currSrt $currStrFilename
srttool -d ${sumBc} -i ${currSrt}>${currStrFilename}
#echo "prev ${previous_srt}..."
#echo "curr ${currSrt}..."
echo "${currSrt} should start at ${sumBc}"
sum="$(date -u -d "$EPOCH $vidDuration" +%s) + $sum"
sumBc=`echo ${sum}|bc`
previous_srt=$currSrt
prevVidDuration=$vidDuration
done
echo "------------------------"
#echo "Total sum is ${sum}"
#sumBc=`echo ${sum}|bc`
#echo "Total sumBc is ${sumBc}"
echo "Creating to full srt file ... "
cat *.srt > ${rawTotalSrt}
srttool -r -i ${rawTotalSrt} -o ${totalSrt}
# Look at this for info, if something doesn't work for you :
# https://unix.stackexchange.com/questions/2283/how-do-i-merge-two-srt-files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment