bmffglitch 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f7dd2fae0ad9e8deab7efd5578871e4835e876f
4
- data.tar.gz: bf3e95c394b6cb56f7139c9b90a0b55fed46aaf5
3
+ metadata.gz: d1d4237bd37c2566cf2885fa15a2a4056d7220aa
4
+ data.tar.gz: ad0893cd502a8ba51ad1ede091701dd715e3d348
5
5
  SHA512:
6
- metadata.gz: 1f66b8743d9cb26cc1ea20940a4c0c4b7b7a26d8f062947ee47c26d9d68b58dfb6c5df5392e62d0d80997d3c748a46948cf44b5c7680467cce7ada4dc27461ed
7
- data.tar.gz: dcaa4241d586b08c3bfa216f751dbee2f82f0e1e849717425e75b5791c0abc36c66b424d707bd63fa9525c1e1d2df926368fca9e8c3ee627df73c6ebcc08773d
6
+ metadata.gz: 647e3da80afa96b05ca2899497f54faff891b9fb3314986695ab1ff5c70983b679b0876629c0bf9cdd293babd3a60ca35a2e3e296add27aaf5b9538fd9e9f86f
7
+ data.tar.gz: bf56e7e0e76bd8e20ecc12ea7f806ea3c6e9d195250308cd06473131b76a48983f8c3a31187b54d23868a76d39a3861d6f3aa33014a302df0c36237938e511a3
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bmffglitch (0.0.1)
4
+ bmffglitch (0.0.3)
5
5
  bmff (~> 0.1.2)
6
6
 
7
7
  GEM
@@ -6,6 +6,30 @@ module BMFFGlitch
6
6
  @io = File.open(path, 'rb')
7
7
  @file_container = BMFF::FileContainer.parse(@io)
8
8
  @samples = get_samples(@file_container)
9
+ @is_faststart = nil
10
+ end
11
+
12
+ def is_faststart?
13
+ return @is_faststart if @is_faststart != nil
14
+
15
+ # get mdat.offset which has the smallest offset from the top of file.
16
+ box = @file_container.select_descendants("mdat")
17
+ if (box == nil || box.empty?)
18
+ raise "Media Data Box(mdat) is missing"
19
+ else
20
+ first_mdat_offset = box.sort{|a,b| a.offset <=> b.offset }[0].offset
21
+ end
22
+
23
+ # get moov.offset
24
+ box = @file_container.select_descendants("moov")
25
+ if (box == nil || box.empty?)
26
+ raise "Movie Box(moov) is missing"
27
+ else
28
+ moov_offset = box[0].offset
29
+ end
30
+ @is_faststart = (first_mdat_offset > moov_offset) ? true : false
31
+
32
+ return @is_faststart
9
33
  end
10
34
 
11
35
  def get_samples(file_container)
@@ -112,31 +136,8 @@ module BMFFGlitch
112
136
  }
113
137
  return samples.sort {|a, b| a.file_offset <=> b.file_offset}
114
138
  end
115
-
116
- def update
117
- box = @file_container.select_descendants("mdat")
118
- if (box == nil || box.empty?)
119
- raise "Media Data Box(mdat) is missing"
120
- elsif (box.length > 2) # delete unnecessary mdat to ease re-calculate offset
121
- box.sort{|a,b| a.offset <=> b.offset }[1..box.length].each {|mdat| @file_container.children.delete(mdat) }
122
- end
123
- mdat = box[0]
124
139
 
125
- # data starts from mdat.offset + size(uint32) + type(uint32)
126
- mdat_data_offset = mdat.offset + 4 + 4
127
-
128
- # re-calculate offset
129
- # Because of the nature of sample-based glitch, we need to handle all samples individually.
130
- # I dare to disassemble chunks and each chunk contains only one sample
131
- @samples.each {|sample|
132
- sample.chunk_offset = mdat_data_offset
133
- sample.file_offset = sample.chunk_offset# file_offset and chunk_offset has the same value because each chunk has only one sample
134
-
135
- mdat_data_offset += sample.data.length
136
- }
137
-
138
- mdat.raw_data = @samples.map {|sample| sample.data }.join
139
-
140
+ def update_moov
140
141
  @file_container.select_descendants("trak").each do |trak|
141
142
  if !trak.select_descendants(BMFF::Box::VisualSampleEntry).empty?
142
143
  samples = @samples.find_all{|sample| sample.is_visualsample? }
@@ -281,6 +282,53 @@ module BMFFGlitch
281
282
  stsc.entry_count = stsc.first_chunk.length
282
283
  end
283
284
  end
285
+
286
+ def update
287
+ moov_offset_change = 0
288
+ # if the file is faststart, moov box appears earlier than mdat, so we have to re-calculate the size of moov box to adjust
289
+ # the offset of mdat.
290
+ if is_faststart?
291
+ # get old moov
292
+ box = @file_container.select_descendants("moov")
293
+ if (box == nil || box.empty?)
294
+ raise "Movie Box(moov) is missing"
295
+ else
296
+ old_moov_size = box[0].to_s.size
297
+ end
298
+
299
+ # update moov
300
+ update_moov
301
+
302
+ new_moov_size = box[0].to_s.size
303
+
304
+ moov_offset_change = new_moov_size - old_moov_size
305
+ end
306
+
307
+ box = @file_container.select_descendants("mdat")
308
+ if (box == nil || box.empty?)
309
+ raise "Media Data Box(mdat) is missing"
310
+ elsif (box.length > 2) # delete unnecessary mdat to ease the re-calculation of offsets.
311
+ box.sort{|a,b| a.offset <=> b.offset }[1..box.length].each {|mdat| @file_container.children.delete(mdat) }
312
+ end
313
+ mdat = box[0]
314
+
315
+ # data starts from mdat.offset + size(uint32) + type(uint32) + moov_offset_change(if faststart)
316
+ mdat_data_offset = mdat.offset + 4 + 4 + moov_offset_change
317
+
318
+ # re-calculate offset
319
+ # Because of the nature of sample-based glitch, we need to handle all samples individually.
320
+ # I dare to disassemble chunks and each chunk contains only one sample
321
+ @samples.each {|sample|
322
+ sample.chunk_offset = mdat_data_offset
323
+ sample.file_offset = sample.chunk_offset# file_offset and chunk_offset has the same value because each chunk has only one sample
324
+
325
+ mdat_data_offset += sample.data.length
326
+ }
327
+
328
+ mdat.raw_data = @samples.map {|sample| sample.data }.join
329
+
330
+ update_moov
331
+ end
284
332
 
285
333
  def output(path)
286
334
  update
@@ -1,3 +1,3 @@
1
1
  module BMFFGlitch
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmffglitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - sabishirop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-29 00:00:00.000000000 Z
11
+ date: 2019-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler