Class: FormatParser::MP4Parser

Inherits:
Object
  • Object
show all
Includes:
IOUtils, ISOBaseMediaFileFormat, ISOBaseMediaFileFormat::Utils
Defined in:
lib/parsers/mp4_parser.rb

Constant Summary collapse

MAGIC_BYTES =
/^ftyp(iso[m2]|mp4[12]|m4[abprv] |avc1|xavc)$/i
BRAND_FORMATS =
{
  'isom' => :mp4, # Prohibited as a major brand by ISO/IEC 14496-12 sec 6.3 paragraph 2, but occasionally used.
  'iso2' => :mp4, # Prohibited as a major brand by ISO/IEC 14496-12 sec 6.3 paragraph 2, but occasionally used.
  'mp41' => :mp4,
  'mp42' => :mp4,
  'avc1' => :mp4,
  'xavc' => :mp4, # Sony XAVC S
  'm4a ' => :m4a,
  'm4b ' => :m4b, # iTunes audiobooks
  'm4p ' => :m4p, # iTunes audio
  'm4r ' => :m4r, # iTunes ringtones
  'm4v ' => :m4v, # iTunes video
}
AUDIO_FORMATS =

iTunes video

Set[:m4a, :m4b, :m4p, :m4r]
VIDEO_FORMATS =
Set[:mp4, :m4v]
AUDIO_MIMETYPE =
'audio/mp4'
VIDEO_MIMETYPE =
'video/mp4'

Constants included from ISOBaseMediaFileFormat::Utils

ISOBaseMediaFileFormat::Utils::IDENTITY_MATRIX

Constants included from IOUtils

IOUtils::INTEGER_DIRECTIVES

Instance Method Summary collapse

Methods included from ISOBaseMediaFileFormat::Utils

#dimensions, #duration, #frame_rate, #video_codecs

Methods included from IOUtils

#read_bytes, #read_fixed_point, #read_int, #safe_read, #safe_skip, #skip_bytes

Instance Method Details

#call(io) ⇒ Object



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
# File 'lib/parsers/mp4_parser.rb', line 33

def call(io)
  @buf = FormatParser::IOConstraint.new(io)

  return unless matches_mp4_definition?

  box_tree = Measurometer.instrument('format_parser.mp4_parser.decoder.build_box_tree') do
    Decoder.new.build_box_tree(0xffffffff, @buf)
  end

  case file_format = file_format(box_tree)
  when VIDEO_FORMATS
    width, height = dimensions(box_tree)
    FormatParser::Video.new(
      codecs: video_codecs(box_tree),
      content_type: VIDEO_MIMETYPE,
      format: file_format,
      frame_rate: frame_rate(box_tree),
      height_px: height,
      intrinsics: box_tree,
      media_duration_seconds: duration(box_tree),
      width_px: width,
    )
  when AUDIO_FORMATS
    FormatParser::Audio.new(
      content_type: AUDIO_MIMETYPE,
      format: file_format,
      intrinsics: box_tree,
      media_duration_seconds: duration(box_tree),
    )
  else
    nil
  end
end

#likely_match?(filename) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/parsers/mp4_parser.rb', line 29

def likely_match?(filename)
  /\.(mp4|m4[abprv])$/i.match?(filename)
end