Module: M4S::Dash::Manifest

Extended by:
Manifest
Included in:
Manifest
Defined in:
lib/m4s/dash/manifest.rb

Constant Summary collapse

VIDEO_TRACK =
'video/mp4'.freeze
AUDIO_TRACK =
'audio/mp4'.freeze
TRACKS_PRIORITY =
{ VIDEO_TRACK => 0, AUDIO_TRACK => 1 }.freeze
SCHEMA_LOCATION =
'urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd'.freeze
PROFILE =
'urn:mpeg:dash:profile:isoff-live:2011'.freeze

Instance Method Summary collapse

Instance Method Details

#calculate_max_segment_duration(mp4) ⇒ Object



43
44
45
46
47
# File 'lib/m4s/dash/manifest.rb', line 43

def calculate_max_segment_duration(mp4)
  track = primary_track(mp4)
  max = mp4.max_segment_duration_ticks(track.fetch('track_id')).to_f
  max / track.fetch('timescale').to_f
end

#calculate_media_presentation_duration(mp4) ⇒ Object



49
50
51
52
# File 'lib/m4s/dash/manifest.rb', line 49

def calculate_media_presentation_duration(mp4)
  track = primary_track(mp4)
  mp4.track_sample_delta_sum(track.fetch('track_id')).to_f / track.fetch('timescale')
end

#format_duration(float_seconds) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/m4s/dash/manifest.rb', line 54

def format_duration(float_seconds)
  total_seconds = float_seconds.to_f
  minutes = (total_seconds / 60).to_i
  seconds = total_seconds % 60

  if minutes.positive?
    seconds.positive? ? "PT#{minutes}M#{seconds.round(1)}S" : "PT#{minutes}M"
  else
    "PT#{seconds.round(1)}S"
  end
end

#generate(mp4, init_segment_path:, segment_path:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/m4s/dash/manifest.rb', line 11

def generate(mp4, init_segment_path:, segment_path:)
  media_presentation_duration = calculate_media_presentation_duration(mp4)
  max_segment_duration = calculate_max_segment_duration(mp4)

  init_template  = join_path(init_segment_path,  'init-$RepresentationID$.mp4')
  media_template = join_path(segment_path,       'chunk-$RepresentationID$-$Number%05d$.m4s')

  builder = Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
    xml.MPD(
      'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
      'xmlns'              => 'urn:mpeg:dash:schema:mpd:2011',
      'xmlns:xlink'        => 'http://www.w3.org/1999/xlink',
      'xsi:schemaLocation' => SCHEMA_LOCATION,
      'profiles'           => PROFILE,
      'type'               => 'static',
      'mediaPresentationDuration' => format_duration(media_presentation_duration),
      'maxSegmentDuration'        => format_duration(max_segment_duration),
      'minBufferTime'             => format_duration(max_segment_duration * 2.0),
    ) do
      xml.ProgramInformation
      xml.ServiceDescription(id: '0')
      xml.Period(id: '0', start: 'PT0.0S') do
        sorted_tracks(mp4).each_with_index do |track, index|
          render_adaptation_set(xml, mp4, track, index, init_template, media_template)
        end
      end
    end
  end

  builder.to_xml
end