Module: MP4::Metadata::Memoize::Track

Defined in:
lib/mp4/metadata/memoize/track.rb

Class Method Summary collapse

Class Method Details

.call(cache, track) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/mp4/metadata/memoize/track.rb', line 8

def call(cache, track)
  current_track_id = get_track_id(track)
  ::MP4.logger.debug { "track #{current_track_id}: memoize started" }

  memoize_overall_track_information(current_track_id, cache, track)
  memoize_elst(current_track_id, cache, track)
  memoize_dinf(current_track_id, cache, track)
  memoize_stbl(current_track_id, cache, track)
end

.get_track_id(track) ⇒ Object



93
94
95
# File 'lib/mp4/metadata/memoize/track.rb', line 93

def get_track_id(track)
  track.fetch(:tkhd).fetch(:track_id).to_i
end

.memoize_dinf(track_id, cache, track) ⇒ Object



46
47
48
49
50
51
# File 'lib/mp4/metadata/memoize/track.rb', line 46

def memoize_dinf(track_id, cache, track)
  cache.add_dinf_content(
    track_id: track_id,
    content: track.dig!(:mdia, :minf, :dinf, :content),
  )
end

.memoize_elst(current_track_id, cache, track) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mp4/metadata/memoize/track.rb', line 77

def memoize_elst(current_track_id, cache, track)
  elst_matrix = track.dig(:edts, :elst, :matrix)

  return if elst_matrix.nil?

  elst_matrix.each do |matrix|
    cache.add_elst_list_item(
      track_id: current_track_id,
      track_duration: matrix.fetch(:segment_duration),
      media_time: matrix.fetch(:media_time),
      media_rate: matrix.fetch(:media_rate),
      media_rate_fraction: matrix.fetch(:media_rate_fraction),
    )
  end
end

.memoize_overall_track_information(track_id, cache, track) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mp4/metadata/memoize/track.rb', line 18

def memoize_overall_track_information(track_id, cache, track)
  codec_data = Stbl::Codec.parse(track.dig!(:mdia, :minf, :stbl, :stsd, :content))
  handler_type = track.dig!(:mdia, :hdlr, :handler_type).to_s

  cache.add_track(
    track_id: track_id,
    handler_type: handler_type,
    handler_name: track.dig!(:mdia, :hdlr, :handler_name).to_s,
    have_composition_time: track.dig!(:mdia, :minf, :stbl).fetch(:ctts, nil) ? 1 : 0,
    timescale: track.dig!(:mdia, :mdhd, :timescale).to_i,
    creation_time: track.dig!(:mdia, :mdhd, :creation_time).to_i,
    width: track.dig!(:tkhd, :width).to_i,
    height: track.dig!(:tkhd, :height).to_i,
    volume: track.dig!(:tkhd, :volume).to_i,
    channel_count: codec_data.dig(0, :channel_count),
    par: codec_data.dig(0, :par),
    sar: codec_data.dig(0, :sar),
    codec: codec_data.dig(0, :codec),
    sample_rate: codec_data.dig(0, :sample_rate),
    max_bitrate: codec_data.dig(0, :max_bitrate),
    avg_bitrate: codec_data.dig(0, :avg_bitrate),
    mime_type: { 'vide' => 'video/mp4', 'soun' => 'audio/mp4' }.fetch(handler_type),
    alternate_group: track.dig!(:tkhd, :alternate_group),
  )

  ::MP4.logger.debug { "track #{track_id}: metadata memoized (#{handler_type}, #{codec_data.dig(0, :codec)})" }
end

.memoize_stbl(current_track_id, cache, track) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mp4/metadata/memoize/track.rb', line 53

def memoize_stbl(current_track_id, cache, track)
  stbl_start = Time.now

  Stbl::BinaryStsd.call(current_track_id, cache, track.dig!(:mdia, :minf, :stbl, :stsd, :content))

  Stbl::Chunks.call(
    current_track_id, cache,
    track.dig!(:mdia, :minf, :stbl, :stco),
    track.dig!(:mdia, :minf, :stbl, :stsc),
  )

  Stbl::Samples.call(
    current_track_id, cache,
    track.dig!(:mdia, :minf, :stbl).fetch(:stsc, nil),
    track.dig!(:mdia, :minf, :stbl).fetch(:stco, nil),
    track.dig!(:mdia, :minf, :stbl).fetch(:stss, nil),
    track.dig!(:mdia, :minf, :stbl).fetch(:stts, nil),
    track.dig!(:mdia, :minf, :stbl).fetch(:ctts, nil),
    track.dig!(:mdia, :minf, :stbl).fetch(:stsz, nil),
  )

  ::MP4.logger.debug { "track #{current_track_id}: stbl memoized in #{(Time.now - stbl_start).round(2)} s" }
end