Class: SubsChanger

Inherits:
Object
  • Object
show all
Defined in:
lib/subtitle-library/changer.rb

Instance Method Summary collapse

Constructor Details

#initialize(subs_path) ⇒ SubsChanger

Returns a new instance of SubsChanger.



5
6
7
8
9
# File 'lib/subtitle-library/changer.rb', line 5

def initialize(subs_path)
  @subs_path = subs_path
  @reader = SubsReader.new subs_path
  @reader.load_cues
end

Instance Method Details

#disposition_microdvd(pos, fps, stretch, disp_seconds) ⇒ Object



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
# File 'lib/subtitle-library/changer.rb', line 29

def disposition_microdvd(pos, fps, stretch, disp_seconds)
  bottom_time = Time.mktime 1, 1, 1
  last_end_time = bottom_time
  invalid_timing = false
  if stretch
    step = disp_seconds ? pos * fps : pos
    disposition = 0
  else
    disposition = disp_seconds ? (pos * fps).ceil : pos.ceil
  end
  @reader.cues.each do |cue|
    cue.start += disposition
    cue.ending += disposition
    new_start = bottom_time + cue.start / fps
    new_end = bottom_time + cue.ending / fps
    if new_start.year + new_start.month + new_start.day +
      new_end.year + new_end.month + new_end.day != 6 or
        new_start < bottom_time or new_end < bottom_time or
          new_start < last_end_time
            invalid_timing = true
            puts 'Invalid timing'
            break
    end
    last_end_time = new_end
    disposition = (disposition + step).ceil if stretch
  end
  SubsWriter.new(@reader).save_as(@subs_path, @reader.type) unless invalid_timing
end

#disposition_timing(pos, fps, stretch, disp_seconds) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/subtitle-library/changer.rb', line 58

def disposition_timing(pos, fps, stretch, disp_seconds)
  bottom_time = Time.mktime 1, 1, 1
  last_end_time = bottom_time
  invalid_timing = false
  if stretch
    step = disp_seconds ? pos : pos / fps
    disposition = 0
  else
    disposition = disp_seconds ? pos : pos / fps
  end
  @reader.cues.each do |cue|
    cue.start += disposition
    cue.ending += disposition
    if cue.start.year + cue.start.month + cue.start.day +
      cue.ending.year + cue.ending.month + cue.ending.day != 6 or
        cue.start < bottom_time or cue.ending < bottom_time or
          cue.start < last_end_time
            invalid_timing = true
            puts 'Invalid timing'
            break
    end
    last_end_time = cue.ending
    disposition += step if stretch
  end
  SubsWriter.new(@reader).save_as(@subs_path, @reader.type) unless invalid_timing
end

#set_max_line(max_line) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/subtitle-library/changer.rb', line 85

def set_max_line(max_line)
  line_break = @reader.type == 'sr' ? "\n" : (@reader.type == 'md' ? '|' : '[br]')
  @reader.cues.each do |cue|
    text = cue.text
    lines = text.split /\n+/
    carriage_needed = false
    lines.each do |line|
      if line.length > max_line
        carriage_needed = true
        break
      end
    end
    if carriage_needed
      new_text = ''
      words = lines.collect { |line| line.split /\s+/ }.flatten
      current_line = ''
      for i in (0 .. words.length - 1)
        current_line += (current_line == '' ? '' : ' ') + words[i]
        if i == words.length - 1
          new_text += current_line
          break
        end
        if current_line.length > max_line
          new_text += current_line + line_break
          current_line = ''
        end
      end
      cue.text = new_text
    end
  end
  SubsWriter.new(@reader).save_as @subs_path, @reader.type
end

#shift(disp_type, pos, fps = -1)) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/subtitle-library/changer.rb', line 11

def shift(disp_type, pos, fps = -1)
  fps = @reader.fps if fps == -1
  if @reader.type == 'md'
    disposition_microdvd pos, fps, false, disp_type == 'ss'
  else
    disposition_timing pos, fps, false, disp_type == 'ss'
  end
end

#stretch(disp_type, pos, fps = -1)) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/subtitle-library/changer.rb', line 20

def stretch(disp_type, pos, fps = -1)
  fps = @reader.fps if fps == -1
  if @reader.type == 'md'
    disposition_microdvd pos, fps, true, disp_type == 'ss'
  else
    disposition_timing pos, fps, true, disp_type == 'ss'
  end
end