Class: MP4::Cache::SQLite

Inherits:
Object
  • Object
show all
Defined in:
lib/mp4/cache/sqlite.rb

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ SQLite

Returns a new instance of SQLite.



3
4
5
6
7
8
9
# File 'lib/mp4/cache/sqlite.rb', line 3

def initialize(location = nil)
  require 'sqlite3'
  @cache_db = ::SQLite3::Database.new(location || ':memory:')
  @cache_db.results_as_hash = true

  load_cache_tables
end

Instance Method Details

#add_chunk(track_id:, chunk_index:, samples_per_chunk:, sample_counter:, description_index:, offset:) ⇒ Object



49
50
51
52
53
54
# File 'lib/mp4/cache/sqlite.rb', line 49

def add_chunk(track_id:, chunk_index:, samples_per_chunk:, sample_counter:, description_index:, offset:)
  execute(
    'INSERT INTO chunk_cache VALUES(?, ?, ?, ?, ?, ?)',
    track_id, chunk_index, samples_per_chunk, sample_counter, description_index, offset,
  )
end

#add_dinf_content(track_id:, content:) ⇒ Object



56
57
58
# File 'lib/mp4/cache/sqlite.rb', line 56

def add_dinf_content(track_id:, content:)
  execute('INSERT INTO dinf_cache VALUES(?, ?)', track_id.to_i, content)
end

#add_elst_list_item(**opts) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mp4/cache/sqlite.rb', line 141

def add_elst_list_item(**opts)
  insert_sql = <<-SQL
    INSERT INTO elst_cache VALUES(
      :track_id,
      :track_duration,
      :media_time,
      :media_rate,
      :media_rate_fraction
    );
  SQL

  self.execute(
    insert_sql,
    opts.fetch(:track_id),
    opts.fetch(:track_duration),
    opts.fetch(:media_time),
    opts.fetch(:media_rate),
    opts.fetch(:media_rate_fraction),
  )
end

#add_ftyp(major_brand:, minor_version:, compatible_brands:) ⇒ Object



33
34
35
36
37
38
# File 'lib/mp4/cache/sqlite.rb', line 33

def add_ftyp(major_brand:, minor_version:, compatible_brands:)
  execute(
    'INSERT INTO ftyp_cache VALUES(?, ?, ?)',
    major_brand.to_s, minor_version.to_i, compatible_brands.to_s,
  )
end

#add_sample(track_id:, chunk_index:, sample_index:, **opts) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mp4/cache/sqlite.rb', line 162

def add_sample(track_id:, chunk_index:, sample_index:, **opts)
  insert_sql = <<-SQL
    INSERT INTO samples_cache VALUES(
      ?, -- track_id
      ?, -- chunk_index
      ?, -- sample_index
      ?, -- sample_size
      ?, -- sample_offset
      ?, -- chunk_offset
      ?, -- original_chunk_offset
      ?, -- sample_delta
      ?, -- delta_sum
      ?, -- delta_sum_millsec
      ?, -- c_delta
      ? -- is_keyframe
    );
  SQL

  self.execute(
    insert_sql,
    track_id,
    chunk_index,
    sample_index,
    opts.fetch(:sample_size),
    opts.fetch(:sample_offset, 0),
    opts.fetch(:chunk_offset),
    opts.fetch(:original_chunk_offset, 0),
    opts.fetch(:sample_delta),
    opts.fetch(:delta_sum),
    opts.fetch(:absolute_delta_sum),
    opts.fetch(:composition_offset),
    opts.fetch(:is_keyframe, 'FALSE'),
  )
end

#add_segment_sample(track_id:, segment_index:, chunk_index:, sample_index:, **opts) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mp4/cache/sqlite.rb', line 104

def add_segment_sample(track_id:, segment_index:, chunk_index:, sample_index:, **opts)
  insert_sql = <<-SQL
    INSERT INTO segments_cache VALUES(
      ?, -- track_id
      ?, -- segment_index
      ?, -- chunk_index
      ?, -- sample_index
      ?, -- sample_size
      ?, -- sample_offset
      ?, -- chunk_offset
      ?, -- original_chunk_offset
      ?, -- sample_delta
      ?, -- delta_sum
      ?, -- delta_sum_millsec
      ?, -- c_delta
      ? -- is_keyframe
    );
  SQL

  self.execute(
    insert_sql,
    track_id,
    segment_index,
    chunk_index,
    sample_index,
    opts.fetch(:sample_size),
    opts.fetch(:sample_offset, 0),
    opts.fetch(:chunk_offset),
    opts.fetch(:original_chunk_offset, 0),
    opts.fetch(:sample_delta),
    opts.fetch(:delta_sum),
    opts.fetch(:absolute_delta_sum),
    opts.fetch(:composition_offset),
    opts.fetch(:is_keyframe, 'FALSE'),
  )
end

#add_stsd_content(track_id:, content:) ⇒ Object



64
65
66
# File 'lib/mp4/cache/sqlite.rb', line 64

def add_stsd_content(track_id:, content:)
  execute('INSERT INTO stsd_cache VALUES(?, ?)', track_id.to_i, content)
end

#add_track(**opts) ⇒ Object



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
102
# File 'lib/mp4/cache/sqlite.rb', line 76

def add_track(**opts)
  insert_sql = <<-SQL
    INSERT INTO tracks_cache VALUES
      (
      :track_id,
      :handler_type,
      :handler_name,
      :have_composition_time,
      :timescale,
      :creation_time,
      :width,
      :height,
      :volume,
      :channel_count,
      :par,
      :sar,
      :codec,
      :max_bitrate,
      :avg_bitrate,
      :sample_rate,
      :mime_type,
      :alternate_group
    );
  SQL

  self.execute(insert_sql, opts)
end

#chunks(track_id) ⇒ Object



217
218
219
# File 'lib/mp4/cache/sqlite.rb', line 217

def chunks(track_id)
  query('select * from chunk_cache where track_id = ?', track_id)
end

#dinf_content(track_id) ⇒ Object



60
61
62
# File 'lib/mp4/cache/sqlite.rb', line 60

def dinf_content(track_id)
  query('SELECT content FROM dinf_cache WHERE track_id = ?', track_id).dig(0, 'content')
end

#each_sample(track_id, &block) ⇒ Object



227
228
229
# File 'lib/mp4/cache/sqlite.rb', line 227

def each_sample(track_id, &block)
  query('select * from samples_cache where track_id = ?', track_id, &block)
end

#each_sample_ordered_across_tracks(&block) ⇒ Object



303
304
305
# File 'lib/mp4/cache/sqlite.rb', line 303

def each_sample_ordered_across_tracks(&block)
  query('SELECT track_id, size, offset FROM samples_cache ORDER BY sample_index ASC, track_id ASC', &block)
end

#each_trackObject



221
222
223
224
225
# File 'lib/mp4/cache/sqlite.rb', line 221

def each_track
  query('select DISTINCT track_id from chunk_cache') do |row|
    yield(row.fetch('track_id'))
  end
end

#elst_entries(track_id) ⇒ Object



72
73
74
# File 'lib/mp4/cache/sqlite.rb', line 72

def elst_entries(track_id)
  query('SELECT * FROM elst_cache WHERE track_id = ?', track_id)
end

#execute(*query) ⇒ Object



25
26
27
# File 'lib/mp4/cache/sqlite.rb', line 25

def execute(*query)
  @cache_db.execute(*query)
end

#first_sample(track_id) ⇒ Object



243
244
245
# File 'lib/mp4/cache/sqlite.rb', line 243

def first_sample(track_id)
  query('SELECT * FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC LIMIT 1', track_id).first
end

#first_sample_size(track_id) ⇒ Object



287
288
289
# File 'lib/mp4/cache/sqlite.rb', line 287

def first_sample_size(track_id)
  query('SELECT size FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC LIMIT 1', track_id).dig(0, 'size')
end

#first_video_track_idObject



231
232
233
# File 'lib/mp4/cache/sqlite.rb', line 231

def first_video_track_id
  query('SELECT track_id FROM tracks_cache WHERE handler_type = ? ORDER BY track_id ASC LIMIT 1', 'vide').dig(0, 'track_id')
end

#ftypObject



40
41
42
43
44
45
46
47
# File 'lib/mp4/cache/sqlite.rb', line 40

def ftyp
  row = query('SELECT * FROM ftyp_cache').first
  row && {
    'major_brand'       => row['major_brand'],
    'minor_version'     => row['minor_version'],
    'compatible_brands' => row['compatible_brands'],
  }
end

#keyframes_grouped_by_second(track_id) ⇒ Object



271
272
273
# File 'lib/mp4/cache/sqlite.rb', line 271

def keyframes_grouped_by_second(track_id)
  query("SELECT sample_index, delta_sum FROM samples_cache WHERE track_id = ? AND keyframe = 'TRUE' GROUP BY (delta_sum_millisec / 1000)", track_id)
end

#last_sample(track_id) ⇒ Object



247
248
249
# File 'lib/mp4/cache/sqlite.rb', line 247

def last_sample(track_id)
  query('SELECT * FROM samples_cache WHERE track_id = ? ORDER BY sample_index DESC LIMIT 1', track_id).first
end

#max_segment_duration_ticks(track_id) ⇒ Object



327
328
329
# File 'lib/mp4/cache/sqlite.rb', line 327

def max_segment_duration_ticks(track_id)
  query('SELECT SUM(delta) AS d FROM segments_cache WHERE track_id = ? GROUP BY segment_index ORDER BY d DESC LIMIT 1', track_id).dig(0, 'd')
end

#min_sample_offsetObject



307
308
309
# File 'lib/mp4/cache/sqlite.rb', line 307

def min_sample_offset
  query('SELECT MIN(offset) AS o FROM samples_cache').dig(0, 'o')
end

#next_track_idObject



235
236
237
# File 'lib/mp4/cache/sqlite.rb', line 235

def next_track_id
  query('SELECT MAX(track_id) + 1 AS n FROM tracks_cache').dig(0, 'n')
end

#query(*query) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mp4/cache/sqlite.rb', line 11

def query(*query)
  query_result = @cache_db.query(*query)

  if block_given?
    until (row = query_result.next).nil?
      yield(row)
    end

    return
  end

  query_result.to_a
end

#sample_at(track_id, sample_index) ⇒ Object



251
252
253
# File 'lib/mp4/cache/sqlite.rb', line 251

def sample_at(track_id, sample_index)
  query('SELECT * FROM samples_cache WHERE track_id = ? AND sample_index = ? LIMIT 1', track_id, sample_index).first
end

#sample_c_delta_pairs(track_id) ⇒ Object



295
296
297
# File 'lib/mp4/cache/sqlite.rb', line 295

def sample_c_delta_pairs(track_id)
  query('SELECT sample_index, c_delta FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC', track_id)
end

#sample_chunk_indices(track_id) ⇒ Object



299
300
301
# File 'lib/mp4/cache/sqlite.rb', line 299

def sample_chunk_indices(track_id)
  query('SELECT chunk_index FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC', track_id).map { |r| r.fetch('chunk_index') }
end

#sample_count(track_id) ⇒ Object



239
240
241
# File 'lib/mp4/cache/sqlite.rb', line 239

def sample_count(track_id)
  query('SELECT COUNT(1) AS c FROM samples_cache WHERE track_id = ?', track_id).dig(0, 'c')
end

#sample_delta_pairs(track_id) ⇒ Object



291
292
293
# File 'lib/mp4/cache/sqlite.rb', line 291

def sample_delta_pairs(track_id)
  query('SELECT sample_index, delta FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC', track_id)
end

#sample_delta_sum_ms(track_id, sample_index) ⇒ Object



255
256
257
# File 'lib/mp4/cache/sqlite.rb', line 255

def sample_delta_sum_ms(track_id, sample_index)
  query('SELECT delta_sum_millisec FROM samples_cache WHERE track_id = ? AND sample_index = ? LIMIT 1', track_id, sample_index).dig(0, 'delta_sum_millisec')
end

#sample_size_list(track_id) ⇒ Object



279
280
281
# File 'lib/mp4/cache/sqlite.rb', line 279

def sample_size_list(track_id)
  query('SELECT size FROM samples_cache WHERE track_id = ? ORDER BY sample_index ASC', track_id).map { |r| r.fetch('size') }
end

#samples_between_ms(track_id, from_ms, to_ms) ⇒ Object



275
276
277
# File 'lib/mp4/cache/sqlite.rb', line 275

def samples_between_ms(track_id, from_ms, to_ms)
  query('SELECT * FROM samples_cache WHERE track_id = ? AND delta_sum_millisec >= ? AND delta_sum_millisec <= ? ORDER BY sample_index ASC', track_id, from_ms, to_ms)
end

#segment_bytes_sizes_offsets(track_id, segment_index) ⇒ Object



331
332
333
# File 'lib/mp4/cache/sqlite.rb', line 331

def segment_bytes_sizes_offsets(track_id, segment_index)
  query('SELECT size, offset FROM segments_cache WHERE track_id = ? AND segment_index = ? ORDER BY sample_index ASC', track_id, segment_index)
end

#segment_duration_ticks(track_id, segment_index) ⇒ Object



311
312
313
# File 'lib/mp4/cache/sqlite.rb', line 311

def segment_duration_ticks(track_id, segment_index)
  query('SELECT SUM(delta) AS d FROM segments_cache WHERE track_id = ? AND segment_index = ?', track_id, segment_index).dig(0, 'd')
end

#segment_duration_ticks_by_index(track_id) ⇒ Object



323
324
325
# File 'lib/mp4/cache/sqlite.rb', line 323

def segment_duration_ticks_by_index(track_id)
  query('SELECT segment_index, SUM(delta) AS duration FROM segments_cache WHERE track_id = ? GROUP BY segment_index ORDER BY segment_index ASC', track_id)
end

#segment_earliest_presentation_ticks(track_id, segment_index) ⇒ Object



315
316
317
# File 'lib/mp4/cache/sqlite.rb', line 315

def segment_earliest_presentation_ticks(track_id, segment_index)
  query('SELECT MIN(delta_sum) AS d FROM segments_cache WHERE track_id = ? AND segment_index = ?', track_id, segment_index).dig(0, 'd')
end

#segment_samples(track_id, segment_index) ⇒ Object



213
214
215
# File 'lib/mp4/cache/sqlite.rb', line 213

def segment_samples(track_id, segment_index)
  query('SELECT * FROM segments_cache WHERE track_id = ? AND segment_index = ? ORDER BY sample_index ASC', track_id, segment_index)
end

#segment_size_bytes(track_id, segment_index) ⇒ Object



319
320
321
# File 'lib/mp4/cache/sqlite.rb', line 319

def segment_size_bytes(track_id, segment_index)
  query('SELECT SUM(size) AS s FROM segments_cache WHERE track_id = ? AND segment_index = ?', track_id, segment_index).dig(0, 's')
end

#segments(track_id) ⇒ Object



209
210
211
# File 'lib/mp4/cache/sqlite.rb', line 209

def segments(track_id)
  query('SELECT segment_index FROM segments_cache WHERE track_id = ? GROUP BY segment_index ORDER BY segment_index ASC', track_id)
end

#stsd_content(track_id) ⇒ Object



68
69
70
# File 'lib/mp4/cache/sqlite.rb', line 68

def stsd_content(track_id)
  query('SELECT content FROM stsd_cache WHERE track_id = ?', track_id).dig(0, 'content')
end

#sum_delta_between(track_id, from_sample_index, to_sample_index) ⇒ Object



259
260
261
# File 'lib/mp4/cache/sqlite.rb', line 259

def sum_delta_between(track_id, from_sample_index, to_sample_index)
  query('SELECT SUM(delta) AS s FROM samples_cache WHERE track_id = ? AND sample_index >= ? AND sample_index <= ?', track_id, from_sample_index, to_sample_index).dig(0, 's')
end

#sync_sample_indices(track_id) ⇒ Object



267
268
269
# File 'lib/mp4/cache/sqlite.rb', line 267

def sync_sample_indices(track_id)
  query("SELECT sample_index FROM samples_cache WHERE track_id = ? AND keyframe = 'TRUE' ORDER BY sample_index ASC", track_id).map { |r| r.fetch('sample_index') }
end

#track(track_id) ⇒ Object



205
206
207
# File 'lib/mp4/cache/sqlite.rb', line 205

def track(track_id)
  query('SELECT * FROM tracks_cache WHERE track_id = ?', track_id).fetch(0)
end

#track_sample_delta_sum(track_id) ⇒ Object



263
264
265
# File 'lib/mp4/cache/sqlite.rb', line 263

def track_sample_delta_sum(track_id)
  query('SELECT SUM(delta) AS s FROM samples_cache WHERE track_id = ?', track_id).dig(0, 's').to_i
end

#tracksObject



201
202
203
# File 'lib/mp4/cache/sqlite.rb', line 201

def tracks
  query('SELECT * FROM tracks_cache ORDER BY handler_type DESC')
end

#tracks_ascObject



197
198
199
# File 'lib/mp4/cache/sqlite.rb', line 197

def tracks_asc
  query('SELECT * FROM tracks_cache ORDER BY track_id ASC')
end

#transaction(&block) ⇒ Object



29
30
31
# File 'lib/mp4/cache/sqlite.rb', line 29

def transaction(&block)
  @cache_db.transaction(&block)
end

#uniform_sample_size?(track_id) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/mp4/cache/sqlite.rb', line 283

def uniform_sample_size?(track_id)
  query('SELECT COUNT(1) AS c FROM samples_cache WHERE track_id = ? GROUP BY size LIMIT 2', track_id).size == 1
end