Class: WebVTT::Segmenter

Inherits:
Object
  • Object
show all
Defined in:
lib/segmenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(webvtt, options = {}) ⇒ Segmenter

Returns a new instance of Segmenter.



24
25
26
27
28
29
30
# File 'lib/segmenter.rb', line 24

def initialize(webvtt, options={})
  @webvtt = webvtt
  @options = options
  @options[:length] ||= 10
  @options[:output] ||= "fileSequence-%05d.webvtt"
  @options[:playlist] ||= "prog_index.m3u8"
end

Instance Attribute Details

#webvttObject (readonly)

Returns the value of attribute webvtt.



22
23
24
# File 'lib/segmenter.rb', line 22

def webvtt
  @webvtt
end

Instance Method Details

#find_segment_files(cue) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/segmenter.rb', line 32

def find_segment_files(cue)
  seg = find_segments(cue)

  # we need to find out how many segments we
  # have to remove from our calculation
  # in the case of first cue not starting at 0
  start = @webvtt.cues[0].start_in_sec
  to_remove = (start / @options[:length]).floor
  return seg.map{|s| s-to_remove}
end

#find_segments(cue) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/segmenter.rb', line 43

def find_segments(cue)
  all_cues = @webvtt.cues
  index_cue = all_cues.index(cue)
  seg = [(cue.start_in_sec / @options[:length]).floor]
  start_seg = seg[0] * @options[:length]
  end_seg = start_seg + @options[:length]

  # if the cue length is > than desired length
  # or if cue end in sec is > end of the segment in sec
  # we display it in the next segment as well

  if (cue.length > @options[:length]) ||
    (cue.end_in_sec > end_seg)

    (cue.length / @options[:length]).ceil.to_i.times.each do |s|
      seg << seg.last + 1
    end
  end

  return seg
end

#generate_playlist(files) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/segmenter.rb', line 65

def generate_playlist(files)
  lines = []
  target_duration = 0
  files.each_with_index do |file,i|

    # if first cue ever we calculate from 0 sec
    if i == 0
      total_length = file.total_length
    else
      total_length = file.actual_total_length
    end

    target_duration = total_length if total_length > target_duration
    if @options[:base_url].nil?
      url = file.filename
    else
      url = ::File.join(@options[:base_url], file.filename)
    end
    lines << %(#EXTINF:#{total_length.round},
#{url})
  end

  playlist = [%(#EXTM3U
#EXT-X-TARGETDURATION:#{target_duration.ceil}
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD)]
  playlist.concat(lines)
  playlist << "#EXT-X-ENDLIST"

  ::File.open(@options[:playlist], "w") {|f| f.write(playlist.join("\n")) }
  return @options[:playlist]
end

#split_to_filesObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/segmenter.rb', line 99

def split_to_files
  filenames = []
  segment_files = []

  @webvtt.cues.each_with_index do |cue,i|
    find_segment_files(cue).each do |seg|
      segment_files[seg] ||= []
      segment_files[seg] << cue
    end
  end

  segment_files.compact.each_with_index do |f,i|
    filename = sprintf(@options[:output], i)
    header = @webvtt.header

    if !header.include?("X-TIMESTAMP-MAP")
      header << "\nX-TIMESTAMP-MAP=MPEGTS:000000,LOCAL:00:00:00.000"
    end

    content = [header, f.map{|c| c.to_webvtt }.join("\n\n")].join("\n\n")

    ::File.open(filename, "w") {|f| f.write(content)}

    filenames << filename
  end
  return filenames.map{|f| File.new(f) }
end