/
subtitle_timing.rb
66 lines (53 loc) · 1.21 KB
/
subtitle_timing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# encoding : utf-8
class SubtitleTiming
# Accept a timing string or a timestamp for init
def initialize(timing)
if timing.instance_of?(String) && timing[':']
@value = self.timing_to_timestamp(timing)
else
@value = timing.to_i
end
end
def to_s
to_timing
end
# Return the timing as a timestamp
def to_timestamp
@value
end
# Return the timing
def to_timing
self.timestamp_to_timing(@value)
end
# Convert a timestamp in ms to a timing
def timestamp_to_timing(timestamp)
h = "%02d" % (timestamp / 3600000)
timestamp = timestamp % 3600000
m = "%02d" % (timestamp / 60000)
timestamp = timestamp % 60000
s = "%02d" % (timestamp / 1000)
timestamp = timestamp % 1000
ms = "%03d" % (timestamp)
return "#{h}:#{m}:#{s},#{ms}"
end
# Convert a timing to a timestamp
def timing_to_timestamp(timing)
split = timing.split(/:/)
h = split[0].to_i
m = split[1].to_i
s = split[2]
if s =~ /,/
ssplit = s.split(',')
s = ssplit[0]
ms = ssplit[1].to_i
end
s = s.to_i
return h*3600000 + m*60000 + s*1000 + ms
end
# Change a timing according to new fps
def change_speed(old_fps, new_fps)
ratio = old_fps.to_f / new_fps.to_f
@value = @value * ratio
return self
end
end