Module: MP4::Parser::MainBoxes

Defined in:
lib/mp4/parser/main_boxes.rb

Defined Under Namespace

Classes: BoxAlreadyDefinedError, UnnamedBoxFoundError

Class Method Summary collapse

Class Method Details

.get_header(source) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mp4/parser/main_boxes.rb', line 30

def get_header(source)
  bytes = source.read(8)
  box_size = BinData::Uint32be.read(bytes[0..4])
  box_name = bytes[4..8]
  ::MP4.logger.debug { "top-level box: #{box_name}/#{box_size}" } if ::MP4.verbose?
  if box_size == 1
    extra_size_bytes = source.read(8)
    box_size = BinData::Uint64be.read(extra_size_bytes)
  end

  { box_size: box_size, box_name: box_name }
end

.parse(source, _offset = 0, _nested = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mp4/parser/main_boxes.rb', line 8

def parse(source, _offset = 0, _nested = [])
  struct = {}

  10.times do
    break if source.eof?

    current_offset = source.pos
    header = get_header(source)
    box_name = header[:box_name]
    box_size = header[:box_size]

    raise BoxAlreadyDefinedError.new(message: "Box #{box_name} already defined") if struct.key?(box_name)
    raise UnnamedBoxFoundError.new(message: "Box #{box_name} already defined") if box_name.empty?

    struct[box_name.to_sym] = [current_offset, box_size]

    source.set_seek(current_offset + box_size)
  end

  struct
end