webvtt-ruby 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +6 -0
- data/Gemfile.lock +19 -0
- data/lib/parser.rb +58 -8
- data/tests/parser.rb +56 -23
- data/tests/segmenter.rb +3 -3
- data/tests/subtitles/test.vtt +1 -1
- data/{webvtt-ruby.gemspecs → webvtt-ruby.gemspec} +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 750601959beae93666af734645757e22a9f8fe2d
|
4
|
+
data.tar.gz: ce85964a436d8459dc409503ff61e8ae12ba33b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d4c6238c8c843bb83955db872f2b7ad51281e64074e8c1beecb30e9fb1ef930e4f0344d1db7e4243c63f5625b6c575d3d55947d8c8555887a2fcf19ea380081
|
7
|
+
data.tar.gz: f250cac70bf7c1a534f518ca013fe04c8a7d422686d8cb7439ff3621b4410d35b2aab69f7ed3456c2ed7a8db25c8ca2c0e0dd4ce032297e2eedfeddde48680f4
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
data/lib/parser.rb
CHANGED
@@ -17,7 +17,7 @@ module WebVTT
|
|
17
17
|
# normalize new line character
|
18
18
|
srt.gsub!("\r\n", "\n")
|
19
19
|
|
20
|
-
srt = "WEBVTT\n\n#{srt}"
|
20
|
+
srt = "WEBVTT\n\n#{srt}".strip
|
21
21
|
::File.open(output, "w") {|f| f.write(srt)}
|
22
22
|
|
23
23
|
return File.new(output)
|
@@ -53,7 +53,9 @@ module WebVTT
|
|
53
53
|
def save(output=nil)
|
54
54
|
output ||= @path.gsub(".srt", ".vtt")
|
55
55
|
|
56
|
-
File.open(output, "w")
|
56
|
+
::File.open(output, "w") do |f|
|
57
|
+
f.write(to_webvtt)
|
58
|
+
end
|
57
59
|
return output
|
58
60
|
end
|
59
61
|
|
@@ -107,15 +109,20 @@ module WebVTT
|
|
107
109
|
end
|
108
110
|
|
109
111
|
def start_in_sec
|
110
|
-
|
112
|
+
@start.to_f
|
111
113
|
end
|
112
114
|
|
113
115
|
def end_in_sec
|
114
|
-
|
116
|
+
@end.to_f
|
115
117
|
end
|
116
118
|
|
117
119
|
def length
|
118
|
-
|
120
|
+
@end.to_f - @start.to_f
|
121
|
+
end
|
122
|
+
|
123
|
+
def offset_by( offset_secs )
|
124
|
+
@start += offset_secs
|
125
|
+
@end += offset_secs
|
119
126
|
end
|
120
127
|
|
121
128
|
def parse
|
@@ -130,11 +137,54 @@ module WebVTT
|
|
130
137
|
end
|
131
138
|
|
132
139
|
if lines[0].match(/([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}) -+> ([0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3})(.*)/)
|
133
|
-
@start = $1
|
134
|
-
@end = $2
|
140
|
+
@start = Timestamp.new $1
|
141
|
+
@end = Timestamp.new $2
|
135
142
|
@style = Hash[$3.strip.split(" ").map{|s| s.split(":").map(&:strip) }]
|
136
143
|
end
|
137
144
|
@text = lines[1..-1].join("\n")
|
138
145
|
end
|
139
146
|
end
|
140
|
-
|
147
|
+
|
148
|
+
class Timestamp
|
149
|
+
def self.parse_seconds( timestamp )
|
150
|
+
if mres = timestamp.match(/\A([0-9]{2}):([0-9]{2}):([0-9]{2}\.[0-9]{3})\z/)
|
151
|
+
sec = mres[3].to_f # seconds and subseconds
|
152
|
+
sec += mres[2].to_f * 60 # minutes
|
153
|
+
sec += mres[1].to_f * 60 * 60 # hours
|
154
|
+
elsif mres = timestamp.match(/\A([0-9]{2}):([0-9]{2}\.[0-9]{3})\z/)
|
155
|
+
sec = mres[2].to_f # seconds and subseconds
|
156
|
+
sec += mres[1].to_f * 60 # minutes
|
157
|
+
else
|
158
|
+
raise ArgumentError.new("Invalid WebVTT timestamp format: #{timestamp.inspect}")
|
159
|
+
end
|
160
|
+
|
161
|
+
return sec
|
162
|
+
end
|
163
|
+
|
164
|
+
def initialize( time )
|
165
|
+
if time.is_a? Numeric
|
166
|
+
@timestamp = time
|
167
|
+
elsif time.is_a? String
|
168
|
+
@timestamp = Timestamp.parse_seconds( time )
|
169
|
+
else
|
170
|
+
raise ArgumentError.new("time not numeric nor a string")
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def to_s
|
175
|
+
hms = [60,60].reduce( [ @timestamp ] ) { |m,o| m.unshift(m.shift.divmod(o)).flatten }
|
176
|
+
hms << (@timestamp.divmod(1).last * 1000).round
|
177
|
+
|
178
|
+
sprintf("%02d:%02d:%02d.%03d", *hms)
|
179
|
+
end
|
180
|
+
|
181
|
+
def to_f
|
182
|
+
@timestamp.to_f
|
183
|
+
end
|
184
|
+
|
185
|
+
def +(other)
|
186
|
+
Timestamp.new self.to_f + other.to_f
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
end
|
data/tests/parser.rb
CHANGED
@@ -1,29 +1,21 @@
|
|
1
1
|
$LOAD_PATH << "lib/"
|
2
|
-
require "
|
2
|
+
require "minitest/autorun"
|
3
3
|
require "webvtt"
|
4
4
|
|
5
|
-
class ParserTest < Test
|
5
|
+
class ParserTest < Minitest::Test
|
6
6
|
def test_can_read_webvtt
|
7
|
-
|
8
|
-
|
9
|
-
assert_equal "test.vtt", webvtt.filename
|
10
|
-
}
|
7
|
+
webvtt = WebVTT.read("tests/subtitles/test.vtt")
|
8
|
+
assert_equal "test.vtt", webvtt.filename
|
11
9
|
end
|
12
10
|
|
13
11
|
def test_cant_read_webvtt
|
14
|
-
|
12
|
+
assert_raises(WebVTT::InputError) {
|
15
13
|
webvtt = WebVTT.read("tests/subtitles/test_.vtt")
|
16
14
|
}
|
17
15
|
end
|
18
16
|
|
19
|
-
def test_is_valid_webvtt
|
20
|
-
assert_nothing_raised(WebVTT::MalformedFile) {
|
21
|
-
webvtt = WebVTT.read("tests/subtitles/test.vtt")
|
22
|
-
}
|
23
|
-
end
|
24
|
-
|
25
17
|
def test_is_not_valid_webvtt
|
26
|
-
|
18
|
+
assert_raises(WebVTT::MalformedFile) {
|
27
19
|
webvtt = WebVTT.read("tests/subtitles/notvalid.vtt")
|
28
20
|
}
|
29
21
|
end
|
@@ -44,8 +36,8 @@ class ParserTest < Test::Unit::TestCase
|
|
44
36
|
def test_cue
|
45
37
|
webvtt = WebVTT.read("tests/subtitles/test.vtt")
|
46
38
|
cue = webvtt.cues[0]
|
47
|
-
assert_equal "00:00:29.000", cue.start
|
48
|
-
assert_equal "00:00:31.000", cue.end
|
39
|
+
assert_equal "00:00:29.000", cue.start.to_s
|
40
|
+
assert_equal "00:00:31.000", cue.end.to_s
|
49
41
|
assert_instance_of Hash, cue.style
|
50
42
|
assert_equal "75%", cue.style["line"]
|
51
43
|
assert_equal "English subtitle 15 -Forced- (00:00:27.000)\nline:75%", cue.text
|
@@ -55,8 +47,8 @@ class ParserTest < Test::Unit::TestCase
|
|
55
47
|
webvtt = WebVTT.read("tests/subtitles/test.vtt")
|
56
48
|
cue = webvtt.cues[1]
|
57
49
|
assert_equal "2", cue.identifier
|
58
|
-
assert_equal "00:00:31.000", cue.start
|
59
|
-
assert_equal "00:00:33.000", cue.end
|
50
|
+
assert_equal "00:00:31.000", cue.start.to_s
|
51
|
+
assert_equal "00:00:33.000", cue.end.to_s
|
60
52
|
assert_equal ["align", "line"].sort, cue.style.keys.sort
|
61
53
|
assert_equal ["start", "0%"].sort, cue.style.values.sort
|
62
54
|
assert_equal "English subtitle 16 -Unforced- (00:00:31.000)\nalign:start line:0%", cue.text
|
@@ -154,12 +146,53 @@ The text should change)
|
|
154
146
|
webvtt = WebVTT.read("tests/subtitles/no_text.vtt")
|
155
147
|
assert_equal 2, webvtt.cues.size
|
156
148
|
assert_equal "265", webvtt.cues[0].identifier
|
157
|
-
assert_equal "00:08:57.409", webvtt.cues[0].start
|
158
|
-
assert_equal "00:09:00.592", webvtt.cues[0].end
|
149
|
+
assert_equal "00:08:57.409", webvtt.cues[0].start.to_s
|
150
|
+
assert_equal "00:09:00.592", webvtt.cues[0].end.to_s
|
159
151
|
assert_equal "", webvtt.cues[0].text
|
160
152
|
assert_equal "266", webvtt.cues[1].identifier
|
161
|
-
assert_equal "00:09:00.593", webvtt.cues[1].start
|
162
|
-
assert_equal "00:09:02.373", webvtt.cues[1].end
|
153
|
+
assert_equal "00:09:00.593", webvtt.cues[1].start.to_s
|
154
|
+
assert_equal "00:09:02.373", webvtt.cues[1].end.to_s
|
163
155
|
assert_equal "", webvtt.cues[1].text
|
164
156
|
end
|
165
|
-
|
157
|
+
|
158
|
+
def test_cue_offset_by
|
159
|
+
cue = WebVTT::Cue.new <<-CUE
|
160
|
+
00:00:01.000 --> 00:00:25.432
|
161
|
+
Test Cue
|
162
|
+
CUE
|
163
|
+
assert_equal 1.0, cue.start.to_f
|
164
|
+
assert_equal 25.432, cue.end.to_f
|
165
|
+
cue.offset_by( 12.0 )
|
166
|
+
assert_equal 13.0, cue.start.to_f
|
167
|
+
assert_equal 37.432, cue.end.to_f
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_timestamp_from_string
|
171
|
+
ts_str = "00:05:31.522"
|
172
|
+
ts = WebVTT::Timestamp.new( ts_str )
|
173
|
+
assert_equal ts_str, ts.to_s
|
174
|
+
assert_equal (5*60 + 31.522), ts.to_f
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_timestamp_from_number
|
178
|
+
ts_f = (7*60 + 12.111)
|
179
|
+
ts = WebVTT::Timestamp.new( ts_f )
|
180
|
+
assert_equal "00:07:12.111", ts.to_s
|
181
|
+
assert_equal ts_f, ts.to_f
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_timestamp_errors_from_unknown_type
|
185
|
+
assert_raises ArgumentError do
|
186
|
+
WebVTT::Timestamp.new( nil )
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_timestamp_addition
|
191
|
+
ts = WebVTT::Timestamp.new( "01:47:32.004" )
|
192
|
+
ts2 = ts + (4*60 + 30)
|
193
|
+
assert_equal "01:52:02.004", ts2.to_s
|
194
|
+
ts3 = ts + ts2
|
195
|
+
assert_equal "03:39:34.008", ts3.to_s
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
data/tests/segmenter.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
$LOAD_PATH << "lib/"
|
2
|
-
require "
|
2
|
+
require "minitest/autorun"
|
3
3
|
require "webvtt"
|
4
4
|
require "fileutils"
|
5
5
|
|
6
|
-
class ParserTest < Test
|
6
|
+
class ParserTest < Minitest::Test
|
7
7
|
|
8
8
|
def test_segment_of_a_given_cue
|
9
9
|
webvtt = WebVTT.read("tests/subtitles/test.vtt")
|
@@ -73,4 +73,4 @@ class ParserTest < Test::Unit::TestCase
|
|
73
73
|
puts "--"
|
74
74
|
end
|
75
75
|
end
|
76
|
-
end
|
76
|
+
end
|
data/tests/subtitles/test.vtt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'webvtt-ruby'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.3.0'
|
4
4
|
s.summary = "WebVTT parser and segmenter in ruby"
|
5
5
|
s.description = "WebVTT parser and segmenter in ruby for HTML5 and HTTP Live Streaming (HLS)."
|
6
6
|
s.authors = ["Bruno Celeste"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webvtt-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Celeste
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: WebVTT parser and segmenter in ruby for HTML5 and HTTP Live Streaming
|
14
14
|
(HLS).
|
@@ -20,6 +20,7 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- ".gitignore"
|
22
22
|
- Gemfile
|
23
|
+
- Gemfile.lock
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
25
26
|
- bin/webvtt-segmenter
|
@@ -36,7 +37,7 @@ files:
|
|
36
37
|
- tests/subtitles/test_from_srt.srt
|
37
38
|
- tests/subtitles/test_from_srt.vtt
|
38
39
|
- tests/subtitles/withnote.vtt
|
39
|
-
- webvtt-ruby.
|
40
|
+
- webvtt-ruby.gemspec
|
40
41
|
homepage: https://github.com/HeyWatch/webvtt-ruby
|
41
42
|
licenses:
|
42
43
|
- MIT
|