Class: BbEPUB::Transform::AudioOverlay

Inherits:
Bookbinder::Transform
  • Object
show all
Defined in:
lib/bb-epub/transform/audio_overlay.rb

Overview

EPUB 3 only. Spec:

http://www.idpf.org/epub/301/spec/epub-mediaoverlays.html#sec-package-metadata

Each spine item’s manifest item may have a media-overlay attribute, which is an idref pointing at a SMIL manifest item.

Other properties to manage:

<meta property="media:active-class">-epub-media-overlay-active</meta>
<meta property="media:playback-active-class">-epub-media-overlay-playing</meta>
<meta property="media:duration" refines="#ch1_audio">0:32:29</meta>
<meta property="media:duration">1:36:20</meta>
<meta property="media:narrator">Bill Speaker</meta>

Note: “The Package Document must include the duration of each Media Overlay as well as of the entire Publication.”

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



21
22
23
24
25
26
27
# File 'lib/bb-epub/transform/audio_overlay.rb', line 21

def dependencies
  [
    BbEPUB::Transform::,
    BbEPUB::Transform::Resources,
    BbEPUB::Transform::Spine
  ]
end

#from_map(package) ⇒ Object

Create a meta tag for:

audio-overlay-active-class => media:active-class
audio-overlay-playback-active-class => media:playback-active-class
audio-overlay-duration => media:duration
audio-overlay-narrator => media:narrator

For each spine item with a ‘media-overlay’ key:

Find the corresponding map resource
Find the corresponding manifest item for component:
  - set 'media-overlay' to rsrc['id']
Create a top-level meta tag:
  cmpt['audio-overlay-duration'] => meta[property='media:duration'][refines='rsrc["id"]]
Also create a top-level meta tag for media:narrator if cmpt has 'audio-overlay-narrator'


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bb-epub/transform/audio_overlay.rb', line 119

def from_map(package)
  opf_doc = package.file(:opf).document
   = opf_doc.find('opf|metadata')
  prop_to_meta(
    package.map,
    ,
    'audio-overlay-active-class',
    'media:active-class'
  )
  prop_to_meta(
    package.map,
    ,
    'audio-overlay-playback-active-class',
    'media:playback-active-class'
  )
  prop_to_meta(
    package.map,
    ,
    'audio-overlay-duration',
    'media:duration'
  )
  prop_to_meta(
    package.map,
    ,
    'audio-overlay-narrator',
    'media:narrator'
  )
  package.map['spine'].each { |cmpt|
    next  unless cmpt['audio-overlay']
    rsrc = package.map['resources'].detect { |r|
      r['path'] == cmpt['audio-overlay']
    }
    cmpt_manifest_item = opf_doc.find("opf|manifest > opf|item##{cmpt['id']}")
    cmpt_manifest_item['media-overlay'] = rsrc['id']
    duration_meta_tag = prop_to_meta(
      cmpt,
      ,
      'audio-overlay-duration',
      'media:duration'
    )
    duration_meta_tag['refines'] = '#'+rsrc['id']
    narrator_meta_tag = prop_to_meta(
      cmpt,
      ,
      'audio-overlay-narrator',
      'media:narrator'
    )
    narrator_meta_tag['refines'] = '#'+rsrc['id']  if narrator_meta_tag
  }
end

#to_map(package) ⇒ Object



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
57
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bb-epub/transform/audio_overlay.rb', line 30

def to_map(package)
  meta_to_prop(
    package,
    'media:active-class',
    'audio-overlay-active-class'
  )
  meta_to_prop(
    package,
    'media:playback-active-class',
    'audio-overlay-playback-active-class'
  )
  book_overlay_duration = meta_to_prop(
    package,
    'media:duration',
    'audio-overlay-duration'
  ) { |hashes|
    hashes.detect { |hash|
      unless hash.has_key?('refines')
        hash['@'] = smil_clock_value_to_seconds(hash['@'])
        true
      end
    }
  }
  # TODO: check that book does have a duration if there are any overlays.
  meta_to_prop(
    package,
    'media:narrator',
    'audio-overlay-narrator'
  ) { |hashes|
    hashes.detect { |hash| !hash.has_key?('refines') }
  }
  # Find each manifest item (SI) with media-overlay attribute:
  #
  #   - find the map['resources'] item that matches the media-overlay id
  #   - find the map['spine'] item that matches the SI id
  #     - set 'audio-overlay' to the AO path
  #     - set 'audio-overlay-duration' to the refined duration
  #     - set 'audio-overlay-narrator' to the refined narrator
  #
  opf_doc = package.file(:opf).document('r')
  opf_doc.each('opf|manifest > opf|item[media-overlay]') { |cmpt_item|
    cmpt_id = cmpt_item['id']
    cmpt = package.map['spine'].detect { |c| c['id'] == cmpt_id }
    # TODO: nil check cmpt
    ao_id = cmpt_item['media-overlay']
    rsrc = package.map['resources'].detect { |r| r['id'] == ao_id }
    # TODO: nil check rsrc
    cmpt['audio-overlay'] = rsrc['path']
    cmpt_overlay_duration = meta_to_prop(
      package,
      'media:duration',
      'audio-overlay-duration',
      cmpt
    ) { |hashes|
      hashes.detect { |hash|
        if hash['refines']['@'] == '#'+ao_id
          hash['@'] = smil_clock_value_to_seconds(hash['@'])
          true
        end
      }
    }
    # TODO: nil check cmpt_overlay_duration
    meta_to_prop(
      package,
      'media:narrator',
      'audio-overlay-narrator',
      cmpt
    ) { |hashes|
      hash['refines']['@'] == '#'+ao_id
    }
  }
end