Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Created June 4, 2016 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbeltagy/d94defbca04cafa6b6b9a59fdd28dcb5 to your computer and use it in GitHub Desktop.
Save mbeltagy/d94defbca04cafa6b6b9a59fdd28dcb5 to your computer and use it in GitHub Desktop.
Reading two subtitle files and harmonizing the times.
# This script fixes subtitle timing your timestamps
# from on file to adjust another
# Opening up the first file in swedish
using StringEncodings
open("The.Seventh.Seal.1957.PROPER.1080p.BluRay.x264-SADPANDA.srt") do io
global swe=readall(io,enc"ISO-8859-15")
end;
swesplit=matchall(r"(\d{1,4}\r\n\d.*?)(?=(\n\d|$))"s,swe);
# The second file in English
open("The\ Seventh\ Seal\ [Det\ Sjunde\ Inseglet].1957.BRRip.XviD.AC3-VLiS.srt") do io
global eng=readall(io,enc"ISO-8859-15")
end;
engsplit=matchall(r"(\d{1,4}\r\n\d.*?)(?=(\n\d|$))"s,eng);
# Now we write the fixed version
open("FixedSweSub.srt",enc"ISO-8859-15","w") do io
# First 9 lines, we use the English
for i=1:9
println(io,engsplit[i])
end
#Getting the English time
m=match(r"\d{2}:\d{2}:\d{2},\d{1,3}",engsplit[10])
timeEng=DateTime(m.match,"H:M:S,s")
#Getting the Swedish time
m=match(r"\d{2}:\d{2}:\d{2},\d{1,3}",swesplit[6])
timeSwe=DateTime(m.match,"H:M:S,s")
delta=timeSwe-timeEng
for i=6:length(swesplit)
println(i)
sweparts=split(swesplit[i],'\n')
engparts=split(engsplit[i+4],'\n')
println(io, engparts[1])
# Getting the begin and end times
m=match(r"(\d{2}:\d{2}:\d{2},\d{1,3}).*(\d{2}:\d{2}:\d{2},\d{1,3})", sweparts[2])
begint=DateTime(m.captures[1],"H:M:S,s")
endt=DateTime(m.captures[2],"H:M:S,s")
println(io,Dates.format(begint-delta,"HH:MM:SS,sss")," --> ",Dates.format(endt-delta,"HH:MM:SS,sss"))
for part in sweparts[3:end]
println(io,part)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment