Class: SubsWriter

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

Instance Method Summary collapse

Constructor Details

#initialize(subs_reader) ⇒ SubsWriter

Returns a new instance of SubsWriter.



2
3
4
5
6
# File 'lib/subtitle-library/writer.rb', line 2

def initialize(subs_reader)
  @cues = subs_reader.cues
  @fps = subs_reader.fps
  @subs_type = subs_reader.type
end

Instance Method Details

#save_as(new_path, new_type, fps = -1)) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/subtitle-library/writer.rb', line 8

def save_as(new_path, new_type, fps = -1)
  fps = @fps if fps == -1
  if @subs_type == 'md'
    if new_type == 'md'
      save_frames_to_frames new_path
    else
      save_frames_to_timing new_path, new_type, fps
    end
  else
    if new_type == 'md'
      save_timing_to_frames new_path, fps
    else
      save_timing_to_timing new_path, new_type
    end
  end
end

#save_frames_to_frames(new_path) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/subtitle-library/writer.rb', line 25

def save_frames_to_frames(new_path)
  File.open(new_path, 'w') do |subs|
    subs.write('{1}{1}' + @fps.to_s + "\n")
    @cues.each do |cue|
      subs.write('{' + cue.start.to_s + '}{' + cue.ending.to_s + '}' + cue.text.gsub("\n", "|") + "\n")
    end
  end
end

#save_frames_to_timing(new_path, new_type, fps) ⇒ Object



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

def save_frames_to_timing(new_path, new_type, fps)
  fps = fps.to_f
  line_count = 1
  bef_mil, bet_start_end = new_type == 'sr' ? [',', ' --> '] : ['.', ',']
  File.open(new_path, 'w') do |subs|
    subs.write("[STYLE]no\n") if new_type == 'sv'
    @cues.each do |cue|
      start = Time.mktime(1,1,1) + cue.start / fps
      ending = Time.mktime(1,1,1) + cue.ending / fps
      timing_line = start.to_s.split(' ')[1] + bef_mil + ("%03d" % (start.usec / 1000)) + bet_start_end
      timing_line += ending.to_s.split(' ')[1] + bef_mil + ("%03d" % (ending.usec / 1000))
      if start.year + start.month + start.day + ending.year + ending.month + ending.day != 6
        puts 'Invalid timing'
        break
      end
      if new_type == 'sr'
        subs.write(line_count.to_s + "\n" + timing_line + "\n")
        subs.write(cue.text + "\n\n")
        line_count += 1
      else
        subs.write(timing_line + "\n")
        subs.write(cue.text.gsub("\n", '[br]') + "\n\n")
      end
    end
  end
end

#save_timing_to_frames(new_path, fps) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/subtitle-library/writer.rb', line 61

def save_timing_to_frames(new_path, fps)
  fps = fps.to_f
  File.open(new_path, 'w') do |subs|
    subs.write('{1}{1}' + fps.to_s + "\n")
    bottom_time = Time.mktime 1, 1, 1
    @cues.each do |cue|
      start_frame = ((cue.start - bottom_time) * fps).ceil
      end_frame = ((cue.ending - bottom_time) * fps).ceil
      subs.write('{' + start_frame.to_s + '}{' + end_frame.to_s + '}' + cue.text.gsub("\n", "|") + "\n")
    end
  end
end

#save_timing_to_timing(new_path, new_type) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/subtitle-library/writer.rb', line 74

def save_timing_to_timing(new_path, new_type)
  line_count = 1
  bef_mil, bet_start_end = new_type == 'sr' ? [',', ' --> '] : ['.', ',']
  File.open(new_path, 'w') do |subs|
    subs.write("[STYLE]no\n") if new_type == 'sv'
    @cues.each do |cue|
      timing_line = cue.start.to_s.split(' ')[1] + bef_mil + ("%03d" % (cue.start.usec / 1000)) + bet_start_end
      timing_line += cue.ending.to_s.split(' ')[1] + bef_mil + ("%03d" % (cue.ending.usec / 1000))
      if new_type == 'sr'
        subs.write(line_count.to_s + "\n" + timing_line + "\n")
        subs.write(cue.text + "\n\n")
        line_count += 1
      else
        subs.write(timing_line + "\n")
        subs.write(cue.text.gsub("\n", '[br]') + "\n\n")
      end
    end
  end
end