Class: WebVTT::Segmenter
- Inherits:
-
Object
- Object
- WebVTT::Segmenter
- Defined in:
- lib/webvtt/segmenter.rb
Instance Attribute Summary collapse
-
#webvtt ⇒ Object
readonly
Returns the value of attribute webvtt.
Instance Method Summary collapse
- #find_segment_files(cue) ⇒ Object
- #find_segments(cue) ⇒ Object
- #generate_playlist(files) ⇒ Object
-
#initialize(webvtt, options = {}) ⇒ Segmenter
constructor
A new instance of Segmenter.
- #split_to_files ⇒ Object
Constructor Details
#initialize(webvtt, options = {}) ⇒ Segmenter
Returns a new instance of Segmenter.
22 23 24 25 26 27 28 |
# File 'lib/webvtt/segmenter.rb', line 22 def initialize(webvtt, ={}) @webvtt = webvtt @options = @options[:length] ||= 10 @options[:output] ||= "fileSequence-%05d.vtt" @options[:playlist] ||= "prog_index.m3u8" end |
Instance Attribute Details
#webvtt ⇒ Object (readonly)
Returns the value of attribute webvtt.
20 21 22 |
# File 'lib/webvtt/segmenter.rb', line 20 def webvtt @webvtt end |
Instance Method Details
#find_segment_files(cue) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/webvtt/segmenter.rb', line 30 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/webvtt/segmenter.rb', line 41 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
63 64 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 |
# File 'lib/webvtt/segmenter.rb', line 63 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_files ⇒ Object
97 98 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 |
# File 'lib/webvtt/segmenter.rb', line 97 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") # FIXME: the value should be configurable header << "\nX-TIMESTAMP-MAP=MPEGTS:900000,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 |