srt 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/line_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'srt'
3
+
4
+ describe SRT::Line do
5
+ describe "#new" do
6
+ let(:line) { SRT::Line.new }
7
+
8
+ it "should create an empty subtitle" do
9
+ line.should be_empty
10
+ end
11
+ end
12
+
13
+ describe "#time_str" do
14
+ let(:line) { SRT::Line.new }
15
+
16
+ before do
17
+ line.start_time = 224.2
18
+ line.end_time = 244.578
19
+ end
20
+
21
+ it "should produce timecodes that match the internal float values" do
22
+ line.time_str.should eq("00:03:44,200 --> 00:04:04,578")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'srt'
3
+
4
+ describe SRT::Parser do
5
+ subject { described_class }
6
+
7
+ describe ".id" do
8
+ it "should convert the id string (#[id]) to an int representing the sequence number" do
9
+ subject.id("#317").should eq(317)
10
+ end
11
+ end
12
+
13
+ describe ".timecode" do
14
+ it "should convert the SRT timecode format to a float representing seconds" do
15
+ subject.timecode("01:03:44,200").should eq(3824.2)
16
+ end
17
+ end
18
+
19
+ describe ".timespan" do
20
+ it "should convert a timespan string ([+|-][amount][h|m|s|ms]) to a float representing seconds" do
21
+ subject.timespan("-3.5m").should eq(-210)
22
+ end
23
+
24
+ it "should convert a timespan string ([+|-][amount][h|m|s|ms]) to a float representing seconds" do
25
+ subject.timespan("-1s").should eq(-1)
26
+ end
27
+
28
+ it "should convert a timespan string ([+|-][amount][h|m|s|ms]) to a float representing seconds" do
29
+ subject.timespan("100ms").should eq(0.1)
30
+ end
31
+ end
32
+
33
+ describe ".parse_framerate" do
34
+ it "should convert a framerate string ([number]fps) to a float representing seconds" do
35
+ subject.framerate("23.976fps").should eq(23.976)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,2 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
data/srt.gemspec CHANGED
@@ -7,9 +7,11 @@ Gem::Specification.new do |gem|
7
7
  gem.description = %q{Ruby gem for parsing srt (subtitle) files. SRT stands for SubRip text file format, which is a file for storing subtitles.}
8
8
  gem.summary = %q{Ruby gem for parsing subtitle files.}
9
9
  gem.homepage = "https://github.com/cpetersen/srt"
10
+ gem.license = 'MIT'
10
11
 
11
12
  gem.add_development_dependency('rake')
12
13
  gem.add_development_dependency('rspec')
14
+ gem.add_development_dependency('coveralls')
13
15
 
14
16
  gem.files = `git ls-files`.split($\)
15
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Christopher Petersen
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-24 00:00:00.000000000 Z
11
+ date: 2014-03-10 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,20 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
42
51
  requirements:
43
52
  - - ! '>='
44
53
  - !ruby/object:Gem::Version
@@ -60,42 +69,51 @@ files:
60
69
  - lib/srt.rb
61
70
  - lib/srt/file.rb
62
71
  - lib/srt/line.rb
72
+ - lib/srt/parser.rb
63
73
  - lib/srt/version.rb
64
- - spec/blackswan-part1.srt
65
- - spec/blackswan-part2.srt
66
- - spec/bsg-s01e01.srt
67
- - spec/coordinates-dummy.srt
68
- - spec/srt_spec.rb
69
- - spec/wotw-dubious.srt
74
+ - spec/file_spec.rb
75
+ - spec/fixtures/blackswan-part1.srt
76
+ - spec/fixtures/blackswan-part2.srt
77
+ - spec/fixtures/bsg-s01e01.srt
78
+ - spec/fixtures/coordinates-dummy.srt
79
+ - spec/fixtures/invalid.srt
80
+ - spec/fixtures/wotw-dubious.srt
81
+ - spec/line_spec.rb
82
+ - spec/parser_spec.rb
83
+ - spec/spec_helper.rb
70
84
  - srt.gemspec
71
85
  homepage: https://github.com/cpetersen/srt
72
- licenses: []
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
73
89
  post_install_message:
74
90
  rdoc_options: []
75
91
  require_paths:
76
92
  - lib
77
93
  required_ruby_version: !ruby/object:Gem::Requirement
78
- none: false
79
94
  requirements:
80
95
  - - ! '>='
81
96
  - !ruby/object:Gem::Version
82
97
  version: '0'
83
98
  required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
99
  requirements:
86
100
  - - ! '>='
87
101
  - !ruby/object:Gem::Version
88
102
  version: '0'
89
103
  requirements: []
90
104
  rubyforge_project:
91
- rubygems_version: 1.8.23
105
+ rubygems_version: 2.2.1
92
106
  signing_key:
93
- specification_version: 3
107
+ specification_version: 4
94
108
  summary: Ruby gem for parsing subtitle files.
95
109
  test_files:
96
- - spec/blackswan-part1.srt
97
- - spec/blackswan-part2.srt
98
- - spec/bsg-s01e01.srt
99
- - spec/coordinates-dummy.srt
100
- - spec/srt_spec.rb
101
- - spec/wotw-dubious.srt
110
+ - spec/file_spec.rb
111
+ - spec/fixtures/blackswan-part1.srt
112
+ - spec/fixtures/blackswan-part2.srt
113
+ - spec/fixtures/bsg-s01e01.srt
114
+ - spec/fixtures/coordinates-dummy.srt
115
+ - spec/fixtures/invalid.srt
116
+ - spec/fixtures/wotw-dubious.srt
117
+ - spec/line_spec.rb
118
+ - spec/parser_spec.rb
119
+ - spec/spec_helper.rb
data/spec/srt_spec.rb DELETED
@@ -1,361 +0,0 @@
1
- require 'srt'
2
-
3
- describe SRT do
4
-
5
- describe SRT::Line do
6
- describe "#new" do
7
- let(:line) { SRT::Line.new }
8
-
9
- it "should create an empty subtitle" do
10
- line.should be_empty
11
- end
12
- end
13
-
14
- describe "#time_str" do
15
- let(:line) { SRT::Line.new }
16
-
17
- before do
18
- line.start_time = 224.2
19
- line.end_time = 244.578
20
- end
21
-
22
- it "should produce timecodes that match the internal float values" do
23
- line.time_str.should eq("00:03:44,200 --> 00:04:04,578")
24
- end
25
- end
26
- end
27
-
28
- describe SRT::File do
29
- describe ".parse_timecode" do
30
- it "should convert the SRT timecode format to a float representing seconds" do
31
- SRT::File.parse_timecode("01:03:44,200").should eq(3824.2)
32
- end
33
- end
34
-
35
- describe ".parse_timespan" do
36
- it "should convert a timespan string ([+|-][amount][h|m|s|mil]) to a float representing seconds" do
37
- SRT::File.parse_timespan("-3.5m").should eq(-210)
38
- end
39
- end
40
-
41
- describe ".parse_framerate" do
42
- it "should convert a framerate string ([number]fps) to a float representing seconds" do
43
- SRT::File.parse_framerate("23.976fps").should eq(23.976)
44
- end
45
- end
46
-
47
- shared_examples_for "an SRT file" do
48
- context "when parsing a properly formatted BSG SRT file" do
49
- it "should return an SRT::File" do
50
- subject.class.should eq(SRT::File)
51
- end
52
-
53
- it "should have 600 lines" do
54
- subject.lines.size.should eq(600)
55
- end
56
-
57
- it "should have no errors" do
58
- subject.errors.should be_empty
59
- end
60
-
61
- it "should have the expected sequence number on the first subtitle" do
62
- subject.lines.first.sequence.should eq(1)
63
- end
64
-
65
- it "should have the expected timecodes on the first subtitle" do
66
- subject.lines.first.time_str.should eq("00:00:02,110 --> 00:00:04,578")
67
- end
68
-
69
- it "should have the expected text on the first subtitle" do
70
- subject.lines.first.text.should eq(["<i>(male narrator) Previously", "on Battlestar Galactica.</i>"])
71
- end
72
-
73
- it "should have the expected sequence number on the last subtitle" do
74
- subject.lines.last.sequence.should eq(600)
75
- end
76
-
77
- it "should have the expected timecodes on the last subtitle" do
78
- subject.lines.last.time_str.should eq("00:43:26,808 --> 00:43:28,139")
79
- end
80
-
81
- it "should have the expected text on the last subtitle" do
82
- subject.lines.last.text.should eq(["Thank you."])
83
- end
84
- end
85
- end
86
-
87
- describe ".parse with uncommon formats" do
88
- context "when parsing a spanish language WOTW SRT file with unknown encoding" do
89
- let(:file) { SRT::File.parse(File.open("./spec/wotw-dubious.srt")) }
90
-
91
- it "should parse" do
92
- file.class.should eq(SRT::File)
93
- end
94
-
95
- it "should have 1123 lines" do
96
- file.lines.size.should eq(1123)
97
- end
98
-
99
- it "should have no errors" do
100
- file.errors.should be_empty
101
- end
102
- end
103
-
104
- context "when parsing a dummy SRT file containing display coordinates" do
105
- let(:file) { SRT::File.parse(File.open("./spec/coordinates-dummy.srt")) }
106
-
107
- it "should return an SRT::File" do
108
- file.class.should eq(SRT::File)
109
- end
110
-
111
- it "should have 3 lines" do
112
- file.lines.size.should eq(3)
113
- end
114
-
115
- it "should have no errors" do
116
- file.errors.should be_empty
117
- end
118
-
119
- it "should have the expected display coordinates on the first subtitle" do
120
- file.lines.first.display_coordinates.should eq("X1:100 X2:600 Y1:1 Y2:4")
121
- end
122
-
123
- it "should have the expected display coordinates on the last subtitle" do
124
- file.lines.last.display_coordinates.should eq("X1:1 X2:333 Y1:50 Y2:29")
125
- end
126
- end
127
- end
128
-
129
- describe SRT::File, "when initialized with a valid BSG SRT string" do
130
- subject { SRT::File.parse(File.read("./spec/bsg-s01e01.srt")) }
131
- it_should_behave_like "an SRT file"
132
- end
133
-
134
- describe SRT::File, "when initialized with a valid BSG SRT File" do
135
- subject { SRT::File.parse(File.open("./spec/bsg-s01e01.srt")) }
136
- it_should_behave_like "an SRT file"
137
- end
138
-
139
- describe "#append" do
140
- context "when calling it on the first (part1) of two seperate SRT files for Black Swan" do
141
- let(:part1) { SRT::File.parse(File.open("./spec/blackswan-part1.srt")) }
142
- let(:part2) { SRT::File.parse(File.open("./spec/blackswan-part2.srt")) }
143
-
144
- context "when passing { \"00:53:57,241\" => part2 }" do
145
- before { part1.append({ "00:53:57,241" => part2 }) }
146
-
147
- it "should have grown to 808 subtitles" do
148
- part1.lines.length.should eq(808)
149
- end
150
-
151
- it "should have appended subtitles starting with sequence number 448" do
152
- part1.lines[447].sequence.should eq(448)
153
- end
154
-
155
- it "should have appended subtitles ending with sequence number 808" do
156
- part1.lines.last.sequence.should eq(808)
157
- end
158
-
159
- it "should have appended subtitles relatively from 00:53:57,241" do
160
- part1.lines[447].time_str.should eq("00:54:02,152 --> 00:54:04,204")
161
- end
162
- end
163
-
164
- context "when passing { \"+7.241s\" => part2 }" do
165
- before { part1.append({ "+7.241s" => part2 }) }
166
-
167
- it "should have appended subtitles relatively from +7.241s after the previously last subtitle" do
168
- part1.lines[447].time_str.should eq("00:54:02,283 --> 00:54:04,335")
169
- end
170
- end
171
- end
172
- end
173
-
174
- describe "#split" do
175
- context "when calling it on a properly formatted BSG SRT file" do
176
- let(:file) { SRT::File.parse(File.open("./spec/bsg-s01e01.srt")) }
177
-
178
- context "when passing { :at => \"00:19:24,500\" }" do
179
- let(:result) { file.split( :at => "00:19:24,500" ) }
180
-
181
- it "should return an array containing two SRT::File instances" do
182
- result.length.should eq(2)
183
- result[0].class.should eq(SRT::File)
184
- result[1].class.should eq(SRT::File)
185
- end
186
-
187
- it "should include a subtitle that overlaps a splitting point in the first file" do
188
- result[0].lines.last.text.should eq(["I'll see you guys in combat."])
189
- end
190
-
191
- it "should make an overlapping subtitle end at the splitting point in the first file" do
192
- result[0].lines.last.time_str.should eq("00:19:23,901 --> 00:19:24,500")
193
- end
194
-
195
- it "should include a subtitle that overlaps a splitting point in the second file as well" do
196
- result[1].lines.first.text.should eq(["I'll see you guys in combat."])
197
- end
198
-
199
- it "should make an overlapping subtitle remain at the beginning in the second file" do
200
- result[1].lines.first.time_str.should eq("00:00:00,000 --> 00:00:01,528")
201
- end
202
-
203
- it "should shift back all timecodes of the second file relative to the new file beginning" do
204
- result[1].lines[1].time_str.should eq("00:00:01,737 --> 00:00:03,466")
205
- end
206
- end
207
-
208
- context "when passing { :at => \"00:19:24,500\", :timeshift => false }" do
209
- let(:result) { file.split( :at => "00:19:24,500", :timeshift => false ) }
210
-
211
- it "should return an array containing two SRT::File instances" do
212
- result.length.should eq(2)
213
- result[0].class.should eq(SRT::File)
214
- result[1].class.should eq(SRT::File)
215
- end
216
-
217
- it "should include a subtitle that overlaps a splitting point in the first file" do
218
- result[0].lines.last.text.should eq(["I'll see you guys in combat."])
219
- end
220
-
221
- it "should not make an overlapping subtitle end at the splitting point in the first file" do
222
- result[0].lines.last.time_str.should eq("00:19:23,901 --> 00:19:26,028")
223
- end
224
-
225
- it "should include a subtitle that overlaps a splitting point in the second file as well" do
226
- result[1].lines.first.text.should eq(["I'll see you guys in combat."])
227
- end
228
-
229
- it "should not make an overlapping subtitle remain at the beginning in the second file" do
230
- result[1].lines.first.time_str.should eq("00:19:23,901 --> 00:19:26,028")
231
- end
232
-
233
- it "should not shift back timecodes of the second file relative to the new file beginning" do
234
- result[1].lines[1].time_str.should eq("00:19:26,237 --> 00:19:27,966")
235
- end
236
- end
237
-
238
- context "when passing { :at => [\"00:15:00,000\", \"00:30:00,000\"] }" do
239
- let(:result) { file.split( :at => ["00:15:00,000", "00:30:00,000"] ) }
240
-
241
- it "should return an array containing three SRT::File instances" do
242
- result.length.should eq(3)
243
- result[0].class.should eq(SRT::File)
244
- result[1].class.should eq(SRT::File)
245
- result[2].class.should eq(SRT::File)
246
- end
247
-
248
- it "should let subtitles start at sequence number #1 in all three files" do
249
- result[0].lines.first.sequence.should eq(1)
250
- result[1].lines.first.sequence.should eq(1)
251
- result[2].lines.first.sequence.should eq(1)
252
- end
253
-
254
- it "should put 176 subtitles in the first file" do
255
- result[0].lines.length.should eq(176)
256
- result[0].lines.last.sequence.should eq(176)
257
- end
258
-
259
- it "should put 213 subtitles in the second file" do
260
- result[1].lines.length.should eq(213)
261
- result[1].lines.last.sequence.should eq(213)
262
- end
263
-
264
- it "should put 212 subtitles in the third file" do
265
- result[2].lines.length.should eq(212)
266
- result[2].lines.last.sequence.should eq(212)
267
- end
268
- end
269
- end
270
- end
271
-
272
- describe "#timeshift" do
273
- context "when calling it on a properly formatted BSG SRT file" do
274
- let(:file) { SRT::File.parse(File.open("./spec/bsg-s01e01.srt")) }
275
-
276
- context "when passing { :all => \"+2.5s\" }" do
277
- before { file.timeshift({ :all => "+2.5s" }) }
278
-
279
- it "should have timecodes shifted forward by 2.5s for subtitle #24" do
280
- file.lines[23].time_str.should eq("00:01:59,291 --> 00:02:00,815")
281
- end
282
-
283
- it "should have timecodes shifted forward by 2.5s for subtitle #43" do
284
- file.lines[42].time_str.should eq("00:03:46,164 --> 00:03:47,631")
285
- end
286
- end
287
-
288
- context "when passing { \"25fps\" => \"23.976fps\" }" do
289
- before { file.timeshift({ "25fps" => "23.976fps" }) }
290
-
291
- it "should have correctly scaled timecodes for subtitle #24" do
292
- file.lines[23].time_str.should eq("00:01:52,007 --> 00:01:53,469")
293
- end
294
-
295
- it "should have correctly scaled timecodes for subtitle #43" do
296
- file.lines[42].time_str.should eq("00:03:34,503 --> 00:03:35,910")
297
- end
298
- end
299
-
300
- context "when passing { 24 => \"00:03:53,582\", 42 => \"00:04:24,656\" }" do
301
- before { file.timeshift({ 24 => "00:03:53,582", 42 => "00:04:24,656" }) }
302
-
303
- it "should have shifted timecodes for subtitle #24" do
304
- file.lines[23].time_str.should eq("00:03:53,582 --> 00:03:54,042")
305
- end
306
-
307
- it "should have differently shifted timecodes for subtitle #43" do
308
- file.lines[41].time_str.should eq("00:04:24,656 --> 00:04:25,298")
309
- end
310
- end
311
- end
312
-
313
- context "when calling it on a spanish language WOTW SRT file with unknown encoding" do
314
- let(:file) { SRT::File.parse(File.open("./spec/wotw-dubious.srt")) }
315
-
316
- context "when passing { :all => \"-2.7m\" }" do
317
- before { file.timeshift({ :all => "-2.7m" }) }
318
-
319
- it "should have dumped 16 lines with now negative timecodes, leaving 1107" do
320
- file.lines.size.should eq(1107)
321
- end
322
- end
323
-
324
- context "when passing { \"00:03:25,430\" => \"00:00:44,200\", \"01:49:29,980\" => \"01:46:35,600\" }" do
325
- before { file.timeshift({ "00:03:25,430" => "00:00:44,200", "01:49:29,980" => "01:46:35,600" }) }
326
-
327
- it "should have dumped 16 lines with now negative timecodes, leaving 1107" do
328
- file.lines.size.should eq(1107)
329
- end
330
- end
331
- end
332
-
333
- describe "#to_s" do
334
- context "when calling it on a short SRT file" do
335
- let(:file) { SRT::File.parse(File.open("./spec/bsg-s01e01.srt")) }
336
-
337
- before { file.lines = file.lines[0..2] }
338
-
339
- it "should produce the exactly correct output" do
340
- OUTPUT =<<END
341
- 1
342
- 00:00:02,110 --> 00:00:04,578
343
- <i>(male narrator) Previously
344
- on Battlestar Galactica.</i>
345
-
346
- 2
347
- 00:00:05,313 --> 00:00:06,871
348
- Now you're telling me
349
- you're a machine.
350
-
351
- 3
352
- 00:00:07,014 --> 00:00:08,003
353
- The robot.
354
- END
355
- file.to_s.should eq(OUTPUT)
356
- end
357
- end
358
- end
359
- end
360
- end
361
- end