Saturday, September 1, 2007

On watching movies

Yesternight I was trying to watch this movie when I realised that the subtitle is way out of sync. I tried to use VLC's Subtitles advanced setting but turns out that it is unable to sync it. I tried hunting for a few more subtitles track but after finding two of them I realised that both of them were identical, I am sure that there would be many souls out there who dont have easy net access or there are many movies with not many different subtitles track. Being a geek that I am, at 1:00 in the night I decided it is time to do something permanent :) and I came up with this script that syncs up the subtitles track for me. It takes 4 inputs, a time (T1) towards the start of the movie and the corresponding time (t1) in the subtitles file [If you didnt know, subtitles file can be edited (.srt)] when the same dialog is being played and two similiar times towards the end of the movie.

Run the script like this:
awk -f lipsync.awk Subtitles.srt > ModifiedSubtitles.srt

Play the movie with the new file.

The script: [Save it as lipsync.awk]
# --------------------------------------- Start of lipsync.awk
#
# Author: Fasihullah Askiri
#
# Movie: ----T1---------------------------------------T2---
# File: --t1---------------------------------t2--
#
# T1 - T2
# -------
# t1 - t2
#

BEGIN {
T1 = 745
t1 = 721

T2 = 7530
t2 = 7227

scale = (T1 - T2) / (t1 - t2)
delaySec = T2 - t2 * scale
# print "Scale: " scale
# print "DelaySec: " delaySec
}

{ processed = 0 }

function formatTime (timeInSecs)
{
hr = int (timeInSecs/3600)
min = int (timeInSecs/60) - hr * 60
sec = int (timeInSecs) - min * 60 - hr * 3600
msecs = int (1000 * (timeInSecs - int (timeInSecs)))
return hr ":" min ":" sec "," msecs
}

function getTimeInSecs (str)
{
split (str, time, ":")
split (time [3], timeLast, ",")
return time [1] * 3600 + time [2] * 60 + timeLast [1] + timeLast [2] / 1000
}

/ --> / {
newStartTime = formatTime(getTimeInSecs($1) * scale + delaySec)
newEndTime = formatTime(getTimeInSecs($3) * scale + delaySec)
print newStartTime, $2, newEndTime
processed = 1
}

!processed {
print $0
}

# --------------------------------------- End of lipsync.awk



I assume that you have a unix based system or a cygwin on your windoze or at least a running version of awk on windoze

Hope it helps

No comments: