# Syncronize it
# Version 0.1
# Author : kyzh@snibbits.net
# Syncronize your subtitle file (.str at least...)
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
# Copyright (C) 2004 Sam Hocevar
# 14 rue de Plaisance, 75014 Paris, France
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
# 0. You just DO WHAT THE FUCK YOU WANT TO.
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
@time = {"secondes",0,"minutes",0,"hours",0}
@shift = 20
@source = "test.srt"
@destination = "retest.srt"
def file_test(file)
if File.file?(file)
if File::exists?(file)
return true
else
#error => don't exist
return false
end
#erreur => not a file
return false
end
end
def str_to_time(str)
time = @time
#strings look like 00:00:09,100 --> 00:00:11,800
time["hours"], time["minutes"],time["secondes"]= str.to_s.split(":")[0], str.to_s.split(":")[1], str.to_s.split(":")[2]
return time
end
def sec_to_time(sec)
time = @time
a_sec = sec.divmod(60)
s = a_sec[1]
a_min = a_sec[0].divmod(60)
m = a_min[1]
a_h = a_min[0].divmod(24)
h = a_h[1]
time["secondes"],time["minutes"],time["hours"]= s,m,h
return time
end
def time_to_sec(time)
sec = time["secondes"].to_i
sec = sec + time["minutes"].to_i * 60
sec = sec + time["hours"].to_i * 60 * 60
return sec
end
def time_to_str(time)
#strings look like 00:00:19
str = time["hours"].to_s + ":" + time["minutes"].to_s + ":" + time["secondes"].to_s + ":"
return str
end
def save_it(file,ligne)
svg = File.new(fichier, "a")
svg << ligne
svg.close
end
def parse_it(a_ligne)
# 00:00:09,100 --> 00:00:11,800
#time1 time2
time1 = str_to_time(a_ligne[0].split(",")[0])
sec1 = time_to_sec(time1)
shifted_time1 = sec_to_time(sec1.to_i + @shift)
ligne = time_to_str(shifted_time1) + "," + a_ligne[0].split(",")[1]
time2 = str_to_time(a_ligne[2].split(",")[0])
sec2 = time_to_sec(time2)
shifted_time2 = sec_to_time(sec2.to_i + @shift)
ligne = ligne + " --> " + time_to_str(shifted_time2) + "," + a_ligne[2].split(",")[1] + "\n"
return ligne
end
if file_test(@source)
ARGV << @source
while ligne = gets
if ligne =~ /\d\d.\d\d.\d\d/ # ligne is like 00:00:00 in fact like that 00:00:09,100 --> 00:00:11,800
a_ligne = ligne.split
new_ligne=parse_it(a_ligne)
save_it(@destination, new_ligne)
else
save_it(@destination, ligne)
end
end
else
#we do something
end
Refactorings
No refactoring yet !
Magnus Holm
December 22, 2008, December 22, 2008 01:19, permalink
Well, at least I got rid of some bytes!
# Takes two arguments: filename and seconds to shift
# `ruby srt.rb quantum_of_solace.srt 10.67 > better_quantum_of_solace.srt`
require 'time'
puts File.read(ARGV[0]).gsub(/^(.*) --> (.*)$/){[
$1,$2].map{|x|(t=Time.parse(x.strip)+ARGV[1].to_f
).strftime("%H:%M:%S,#{t.usec}00")[0,12]}*' --> '}
mischamolhoek.myopenid.com
December 23, 2008, December 23, 2008 08:24, permalink
that's about as short as it gets :)
sick of seeking into the subtitle?
I made this code because I was...
I'd like to cleanning up this code to go further.