srt 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -78,19 +78,24 @@ Other example options, e.g.: `:all => "+700mil"`, `:all => "1.34m"`, `:all => "0
78
78
  **Progressive timeshift**
79
79
 
80
80
  ```ruby
81
- file.timeshift({ 1 => "00:02:12,000", 843 => "01:38:06,000" }) # Correct drifting-out-of-sync
81
+ file.timeshift({ "#1" => "00:02:12,000", "#843" => "01:38:06,000" }) # Correct drifting-out-of-sync
82
82
  ```
83
83
 
84
84
  This example call would shift the **first subtitle** to `00:02:12`, the **last subtitle** (assuming here that `#843` is the last one in your file) to `01:38:06`, and all the ones before, after, and in between those two reference points seamlessly to their own resulting earlier or later begin times.
85
85
 
86
- To make this work pass two `original timecode/id => target timecode` pairs where each takes any of these 4 forms:
86
+ To make this work pass two `origin timecode => target timecode` pairs, where the *origin timecodes* can be supplied as:
87
87
 
88
- * `[id] => "[hh]:[mm]:[ss],[mil]"`
89
- * `[id] => "[+/-][amount][h|m|s|mil]"`
90
- * `"[hh]:[mm]:[ss],[mil]" => "[hh]:[mm]:[ss],[mil]"`
91
- * `"[hh]:[mm]:[ss],[mil]" => "[+/-][amount][h|m|s|mil]"`
88
+ * `float` providing the raw timecode in *seconds*, e.g.: `195.65`
89
+ * `"[hh]:[mm]:[ss],[mil]"` string, which is a timecode in SRT notation, e.g.: `"00:02:12,000"`
90
+ * `"#[id]"` string, which references the timecode of the subtitle with the supplied id, e.g.: `"#317"`
92
91
 
93
- Another full example: `{ "00:00:51,400" => "+13s", "01:12:44,320" => "+2.436m" }`
92
+ ... and the *target timecodes* can be supplied as:
93
+
94
+ * `float` providing the raw timecode in *seconds*, e.g.: `3211.3`
95
+ * `"[hh]:[mm]:[ss],[mil]"` string, which is a timecode in SRT notation, e.g.: `"01:01:03,300"`
96
+ * `"[+/-][amount][h|m|s|mil]"` string, describing the amount by which to shift the origin timecode, e.g.: `"+1.5s"`
97
+
98
+ So for example: `{ "00:00:51,400" => "+13s", "01:12:44,320" => "+2.436m" }`
94
99
 
95
100
  This method can be used to fix subtitles that are *at different times differently out of sync*,
96
101
  and comes in handy especially if you have no idea what framerate your video or the video for which your subtitles
data/lib/srt/file.rb CHANGED
@@ -164,13 +164,24 @@ module SRT
164
164
  end
165
165
  end
166
166
  elsif options.length == 2
167
- original_timecode_a = (options.keys[0].is_a?(String) ? SRT::File.parse_timecode(options.keys[0]) : lines[options.keys[0] - 1].start_time)
168
- original_timecode_b = (options.keys[1].is_a?(String) ? SRT::File.parse_timecode(options.keys[1]) : lines[options.keys[1] - 1].start_time)
169
- target_timecode_a = SRT::File.parse_timecode(options.values[0]) || (original_timecode_a + SRT::File.parse_timespan(options.values[0]))
170
- target_timecode_b = SRT::File.parse_timecode(options.values[1]) || (original_timecode_b + SRT::File.parse_timespan(options.values[1]))
167
+ origins, targets = options.keys, options.values
171
168
 
172
- time_rescale_factor = (target_timecode_b - target_timecode_a) / (original_timecode_b - original_timecode_a)
173
- time_rebase_shift = target_timecode_a - original_timecode_a * time_rescale_factor
169
+ [0,1].each do |i|
170
+ if origins[i].is_a?(String) && SRT::File.parse_id(origins[i])
171
+ origins[i] = lines[SRT::File.parse_id(origins[i]) - 1].start_time
172
+ elsif origins[i].is_a?(String) && SRT::File.parse_timecode(origins[i])
173
+ origins[i] = SRT::File.parse_timecode(origins[i])
174
+ end
175
+
176
+ if targets[i].is_a?(String) && SRT::File.parse_timecode(targets[i])
177
+ targets[i] = SRT::File.parse_timecode(targets[i])
178
+ elsif targets[i].is_a?(String) && SRT::File.parse_timespan(targets[i])
179
+ targets[i] = origins[i] + SRT::File.parse_timespan(targets[i])
180
+ end
181
+ end
182
+
183
+ time_rescale_factor = (targets[1] - targets[0]) / (origins[1] - origins[0])
184
+ time_rebase_shift = targets[0] - origins[0] * time_rescale_factor
174
185
 
175
186
  lines.each do |line|
176
187
  line.start_time = line.start_time * time_rescale_factor + time_rebase_shift
@@ -208,6 +219,11 @@ module SRT
208
219
  mres ? mres["fps"].to_f : nil
209
220
  end
210
221
 
222
+ def self.parse_id(id_string)
223
+ mres = id_string.match(/#(?<id>\d+)/)
224
+ mres ? mres["id"].to_i : nil
225
+ end
226
+
211
227
  def self.parse_timecode(timecode_string)
212
228
  mres = timecode_string.match(/(?<h>\d+):(?<m>\d+):(?<s>\d+),(?<mil>\d+)/)
213
229
  mres ? "#{mres["h"].to_i * 3600 + mres["m"].to_i * 60 + mres["s"].to_i}.#{mres["mil"]}".to_f : nil
@@ -218,10 +234,10 @@ module SRT
218
234
  "mil" => 0.001,
219
235
  "s" => 1,
220
236
  "m" => 60,
221
- "h" => 3600
237
+ "h" => 3600
222
238
  }
223
239
 
224
- mres = timespan_string.match(/(?<amount>(\+|-)?\d+((\.)?\d+))(?<unit>mil|s|m|h)/)
240
+ mres = timespan_string.match(/(?<amount>(\+|-)?\d+((\.)?\d+)?)(?<unit>mil|s|m|h)/)
225
241
  mres ? mres["amount"].to_f * factors[mres["unit"]] : nil
226
242
  end
227
243
  end
data/lib/srt/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SRT
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/spec/srt_spec.rb CHANGED
@@ -26,6 +26,13 @@ describe SRT do
26
26
  end
27
27
 
28
28
  describe SRT::File do
29
+
30
+ describe ".parse_id" do
31
+ it "should convert the id string (#[id]) to an int representing the sequence number" do
32
+ SRT::File.parse_id("#317").should eq(317)
33
+ end
34
+ end
35
+
29
36
  describe ".parse_timecode" do
30
37
  it "should convert the SRT timecode format to a float representing seconds" do
31
38
  SRT::File.parse_timecode("01:03:44,200").should eq(3824.2)
@@ -36,6 +43,10 @@ describe SRT do
36
43
  it "should convert a timespan string ([+|-][amount][h|m|s|mil]) to a float representing seconds" do
37
44
  SRT::File.parse_timespan("-3.5m").should eq(-210)
38
45
  end
46
+
47
+ it "should convert a timespan string ([+|-][amount][h|m|s|mil]) to a float representing seconds" do
48
+ SRT::File.parse_timespan("-1s").should eq(-1)
49
+ end
39
50
  end
40
51
 
41
52
  describe ".parse_framerate" do
@@ -297,8 +308,8 @@ describe SRT do
297
308
  end
298
309
  end
299
310
 
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" }) }
311
+ context "when passing { \"#24\" => \"00:03:53,582\", \"#42\" => \"00:04:24,656\" }" do
312
+ before { file.timeshift({ "#24" => "00:03:53,582", "#42" => "00:04:24,656" }) }
302
313
 
303
314
  it "should have shifted timecodes for subtitle #24" do
304
315
  file.lines[23].time_str.should eq("00:03:53,582 --> 00:03:54,042")
@@ -308,6 +319,18 @@ describe SRT do
308
319
  file.lines[41].time_str.should eq("00:04:24,656 --> 00:04:25,298")
309
320
  end
310
321
  end
322
+
323
+ context "when passing { 180 => \"+1s\", 264 => \"+1.5s\" }" do
324
+ before { file.timeshift({ 180 => "+1s", 264 => "+1.5s" }) }
325
+
326
+ it "should have shifted by +1s at 180 seconds" do
327
+ file.lines[23].time_str.should eq("00:01:57,415 --> 00:01:58,948")
328
+ end
329
+
330
+ it "should have shifted by +1.5s at 264 seconds" do
331
+ file.lines[41].time_str.should eq("00:03:40,997 --> 00:03:43,136")
332
+ end
333
+ end
311
334
  end
312
335
 
313
336
  context "when calling it on a spanish language WOTW SRT file with unknown encoding" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: srt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-24 00:00:00.000000000 Z
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake