bmffglitch 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 649b00f133f5e76d23fb68194912af6b6b098469
4
+ data.tar.gz: 95bcf459d67b5d7a7cfad7c18a3e5f2a8b11fea0
5
+ SHA512:
6
+ metadata.gz: 417615a7b73c5f0750bdb0cf637ba66689c9ebe2e78a14a2f4662fac8da29ae7bb3ac54326235784170f68fd68b4a93fecff99276295a497fc650858db2bb41e
7
+ data.tar.gz: 8ef97e7b2e10cc1b11fdfd6d73be57f83ba5d63d9cbfc94eb9d9ca2c8cdc209d08bad23121297e7392ee82d938248187cba7e4d5e691944255d221405a58fcfd
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.6
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at tyuyu@sabishiro.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bmffglitch.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bmffglitch (0.0.1)
5
+ bmff (~> 0.1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ bmff (0.1.2)
11
+ uuidtools
12
+ diff-lcs (1.3)
13
+ rake (10.5.0)
14
+ rspec (3.8.0)
15
+ rspec-core (~> 3.8.0)
16
+ rspec-expectations (~> 3.8.0)
17
+ rspec-mocks (~> 3.8.0)
18
+ rspec-core (3.8.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-expectations (3.8.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-mocks (3.8.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-support (3.8.0)
27
+ uuidtools (2.1.5)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bmffglitch!
34
+ bundler (~> 2.0)
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 sabishirop
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # BMFFGlitch
2
+
3
+ BMFFGlitch enables you to destroy your files stored in ISO Base Media File Format(BMFF) and its relatives(such as MP4/MOV).
4
+ BMFFGlitch makes use of [BMFF library](https://github.com/zuku/bmff/) made by Takayuki OGISO, and is influenced by
5
+ [AviGlitch](http://ucnv.github.io/aviglitch/) made by ucnv.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bmffglitch'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bmffglitch
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'bmffglitch'
27
+
28
+ bmff = BMFFGlitch.open("/path/to/input.mp4")
29
+ bmff.samples.each do |sample|
30
+ if sample.is_syncsample?
31
+ sample.data.gsub!(/\d/, '0')
32
+ end
33
+ end
34
+ bmff.output('/path/to/output.mp4')
35
+ ```
36
+
37
+ This library also includes a command line tool named `bmffdtmsh`.
38
+ It creates the syncsample(keyframe)-removed video.
39
+
40
+ ```sh
41
+ $ bmffdtmsh /path/to/your.mp4 -o /path/to/broken.mp4
42
+ ```
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sabishirop/bmffglitch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the Bmffglitch project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bmffglitch/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/bmffdtmsh ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+
4
+ require 'optparse'
5
+ require 'bmffglitch'
6
+
7
+ all = false
8
+ ofn = './out.mp4'
9
+
10
+ opts = OptionParser.new
11
+
12
+ opts.banner = "bmffdtmsh - Datamoshing video generator using BMFFGlitch."
13
+ opts.define_head("Usage: #{File.basename($0)} [options] <input_filename>")
14
+ opts.separator("Options:")
15
+ opts.on("-o", "--output [OUTPUT]", "Output the video to OUTPUT (./out.mp4 by default)") {|v| ofn = v }
16
+ opts.on("-a", "--all", "Remove all keyframes (It remains a first keyframe by default)") {|v| all = true}
17
+ opts.on_tail("-h", "--help", "Show this message") {|v|
18
+ puts opts
19
+ exit
20
+ }
21
+
22
+ opts.parse!(ARGV)
23
+
24
+ if ARGV.length != 1
25
+ opts.banner = "You must specify one BMFF file.\n\n"
26
+ puts opts
27
+ exit 1
28
+ end
29
+
30
+ bmff = BMFFGlitch.open(ARGV[0])
31
+ bmff.samples.delete_if {|sample| sample.is_syncsample? && (all || sample.sample_number != 1)}
32
+ bmff.output(ofn)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sample_gem"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bmffglitch/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bmffglitch"
8
+ spec.version = BMFFGlitch::VERSION
9
+ spec.authors = ["sabishirop"]
10
+ spec.email = ["sabishirop@gmail.com"]
11
+
12
+ spec.summary = %q{A Ruby library to destroy files stored in ISO Base Media File Format(BMFF) and its relatives}
13
+ spec.description = spec.summary
14
+ spec.homepage = "http://sabishirop.github.com/bmffglitch"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 2.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+
29
+ spec.add_dependency "bmff", "~> 0.1.2"
30
+ end
@@ -0,0 +1,292 @@
1
+ module BMFFGlitch
2
+ class Base
3
+ attr_accessor :samples
4
+
5
+ def initialize(path)
6
+ @io = File.open(path, 'rb')
7
+ @file_container = BMFF::FileContainer.parse(@io)
8
+ @samples = get_samples(@file_container)
9
+ end
10
+
11
+ def get_samples(file_container)
12
+ samples = []
13
+ file_container.select_descendants("trak").each do |trak|
14
+ if !trak.select_descendants(BMFF::Box::VisualSampleEntry).empty?
15
+ flag = BMFFGlitch::Sample::VISUALSAMPLE
16
+ elsif !trak.select_descendants(BMFF::Box::AudioSampleEntry).empty?
17
+ flag = BMFFGlitch::Sample::AUDIOSAMPLE
18
+ elsif !trak.select_descendants(BMFF::Box::HintSampleEntry).empty?
19
+ flag = BMFFGlitch::Sample::HINTSAMPLE
20
+ else
21
+ raise "Malformed trak"
22
+ end
23
+
24
+ box = trak.select_descendants("stsz")
25
+ if (box == nil || box.empty?)
26
+ raise "Sample Size Boxes(stsz) is missing"
27
+ end
28
+ stsz = box[0]
29
+ stsz.sample_count.times {|idx|
30
+ # sample start with 1
31
+ sample_number = idx + 1
32
+ sample = BMFFGlitch::Sample.new(sample_number, flag)
33
+ sample.size = (stsz.sample_size != 0) ? stsz.sample_size : stsz.entry_size[idx]
34
+ samples.push(sample)
35
+ }
36
+
37
+ box = trak.select_descendants("stss")
38
+ if box != nil && !box.empty?
39
+ stss = box[0]
40
+ stss.sample_number.each {|sample_number|
41
+ sample = samples.find {|sample| (sample.sample_number == sample_number) && (sample.flag & flag != 0)}
42
+ sample.flag |= BMFFGlitch::Sample::SYNCSAMPLE
43
+ }
44
+ end
45
+
46
+ box = trak.select_descendants("ctts")
47
+ if box != nil && !box.empty?
48
+ ctts = box[0]
49
+ sample_number = 1
50
+ ctts.entry_count.times {|i|
51
+ ctts.sample_count[i].times do
52
+ sample = samples.find {|sample| (sample.sample_number == sample_number) && (sample.flag & flag != 0)}
53
+ sample.sample_offset = ctts.sample_offset[i]
54
+ sample_number += 1
55
+ end
56
+ }
57
+ end
58
+
59
+ box = trak.select_descendants("stts")
60
+ if (box == nil || box.empty?)
61
+ raise "(Decoding)Time to Sample(stts) is missing"
62
+ end
63
+ stts = box[0]
64
+ sample_number = 1
65
+ stts.entry_count.times {|i|
66
+ stts.sample_count[i].times do
67
+ sample = samples.find {|sample| (sample.sample_number == sample_number) && (sample.flag & flag != 0)}
68
+ sample.sample_delta = stts.sample_delta[i]
69
+ sample_number += 1
70
+ end
71
+ }
72
+
73
+ box = trak.select_descendants("stsc")
74
+ if (box == nil || box.empty?)
75
+ raise "Sample Table Box(stsc) is missing"
76
+ end
77
+ stsc = box[0]
78
+
79
+ box = trak.select_descendants("stco")
80
+ if (box == nil || box.empty?)
81
+ raise "Chunk offset(stco) is missing"
82
+ end
83
+ stco = box[0]
84
+
85
+ sample_number = 1
86
+ chunk_number = 1
87
+ stsc_idx = 0
88
+ while (chunk_number <= stco.entry_count)
89
+ if (stsc_idx == stsc.entry_count - 1 || chunk_number < stsc.first_chunk[stsc_idx+1])
90
+ offset_from_chunk_offset = 0
91
+ stsc.samples_per_chunk[stsc_idx].times do
92
+ sample = samples.find {|sample| (sample.sample_number == sample_number) && (sample.flag & flag != 0)}
93
+ sample.chunk_number = chunk_number
94
+ sample.chunk_offset = stco.chunk_offset[chunk_number - 1]#chunk starts with 1
95
+ sample.sample_description_index = stsc.sample_description_index[stsc_idx]
96
+ sample.file_offset = sample.chunk_offset + offset_from_chunk_offset
97
+ # prepare for next sample in the same chunk
98
+ sample_number += 1
99
+ offset_from_chunk_offset += sample.size
100
+ end
101
+ chunk_number += 1
102
+ else
103
+ stsc_idx += 1
104
+ end
105
+ end
106
+ end
107
+
108
+ @io.rewind
109
+ samples.each {|sample|
110
+ @io.seek(sample.file_offset, IO::SEEK_SET)
111
+ sample.data = @io.read(sample.size)
112
+ }
113
+ return samples.sort {|a, b| a.file_offset <=> b.file_offset}
114
+ 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
+
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
+ @file_container.select_descendants("trak").each do |trak|
141
+ if !trak.select_descendants(BMFF::Box::VisualSampleEntry).empty?
142
+ samples = @samples.find_all{|sample| sample.is_visualsample? }
143
+ elsif !trak.select_descendants(BMFF::Box::AudioSampleEntry).empty?
144
+ samples = @samples.find_all{|sample| sample.is_audiosample? }
145
+ elsif !trak.select_descendants(BMFF::Box::HintSampleEntry).empty?
146
+ samples = @samples.find_all{|sample| sample.is_hintsample? }
147
+ else
148
+ raise "Malformed trak"
149
+ end
150
+
151
+ # re-assign sample number and chunk number
152
+ # sample_number starts from 1
153
+ samples.each_with_index{|sample, i|
154
+ sample.sample_number = i + 1
155
+ sample.chunk_number = i + 1
156
+ }
157
+
158
+ box = trak.select_descendants("stsz")
159
+ if (box == nil || box.empty?)
160
+ raise "Sample Size Boxes(stsz) is missing"
161
+ end
162
+ stsz = box[0]
163
+
164
+ stsz.sample_count = samples.length
165
+ size = samples.uniq{|sample| sample.size}
166
+ if (size.length == 1) && (size[0].size != 0)
167
+ # The size of all sample is the same, so we store the actual size in stsz.sample_size
168
+ stsz.sample_size = size[0].size
169
+ stsz.entry_size = []
170
+ else
171
+ # The size of sample varies(or all sample size is 0), we store each size in stsz.entry_size
172
+ stsz.sample_size = 0
173
+ stsz.entry_size = []
174
+ # Do I have to sort samples?
175
+ samples.each {|sample|
176
+ stsz.entry_size.push(sample.size)
177
+ }
178
+ end
179
+
180
+ box = trak.select_descendants("stss")
181
+ if box != nil && !box.empty?
182
+ stss = box[0]
183
+ stss.sample_number = []
184
+ samples.find_all {|sample| sample.is_syncsample?}.each {|sample|
185
+ stss.sample_number.push(sample.sample_number)
186
+ }
187
+ stss.entry_count = stss.sample_number.length
188
+ end
189
+
190
+ box = trak.select_descendants("ctts")
191
+ if box != nil && !box.empty?
192
+ ctts = box[0]
193
+ ctts.sample_count = []
194
+ ctts.sample_offset = []
195
+ sample_count = 0
196
+ previous_sample_offset = nil
197
+ samples.each{|sample|
198
+ if (sample.sample_offset != previous_sample_offset)
199
+ # store the previous values
200
+ if (previous_sample_offset != nil)
201
+ ctts.sample_count.push(sample_count)
202
+ ctts.sample_offset.push(previous_sample_offset)
203
+ end
204
+
205
+ # prepare for the next value
206
+ sample_count = 1
207
+ previous_sample_offset = sample.sample_offset
208
+ else
209
+ sample_count += 1
210
+ end
211
+ }
212
+ # store the last values
213
+ ctts.sample_count.push(sample_count)
214
+ ctts.sample_offset.push(previous_sample_offset)
215
+ ctts.entry_count = ctts.sample_count.length
216
+
217
+ end
218
+
219
+ box = trak.select_descendants("stts")
220
+ if box != nil && !box.empty?
221
+ stts = box[0]
222
+ stts.sample_count = []
223
+ stts.sample_delta = []
224
+ sample_count = 0
225
+ previous_sample_delta = nil
226
+ samples.each{|sample|
227
+ if (sample.sample_delta != previous_sample_delta)
228
+ # store the previous values
229
+ if (previous_sample_delta != nil)
230
+ stts.sample_count.push(sample_count)
231
+ stts.sample_delta.push(previous_sample_delta)
232
+ end
233
+ # prepare for the next value
234
+ sample_count = 1
235
+ previous_sample_delta = sample.sample_delta
236
+ else
237
+ sample_count += 1
238
+ end
239
+ }
240
+ # store the last values
241
+ stts.sample_count.push(sample_count)
242
+ stts.sample_delta.push(previous_sample_delta)
243
+ stts.entry_count = stts.sample_count.length
244
+ end
245
+
246
+ box = trak.select_descendants("stco")
247
+ if (box == nil || box.empty?)
248
+ raise "Chunk offset(stco) is missing"
249
+ end
250
+ stco = box[0]
251
+ # Because of the nature of sample-based glitch, we need to handle all sample separately.
252
+ # I dare to disassemble chunks and each chunk contains only one sample
253
+ stco.entry_count = samples.length
254
+ stco.chunk_offset = []
255
+ samples.each {|sample|
256
+ stco.chunk_offset.push(sample.chunk_offset)
257
+ }
258
+
259
+ box = trak.select_descendants("stsc")
260
+ if (box == nil || box.empty?)
261
+ STDERR.puts "Sample Table Box(stsc) is missing"
262
+ exit
263
+ end
264
+ stsc = box[0]
265
+ # Because of the nature of sample-based glitch, we need to handle all samples individually.
266
+ # I dare to disassemble chunks and each chunk contains only one sample
267
+
268
+ stsc.first_chunk = []
269
+ stsc.samples_per_chunk = []
270
+ stsc.sample_description_index = []
271
+ previous_sample_description_index = nil
272
+
273
+ samples.each {|sample|
274
+ if (sample.sample_description_index != previous_sample_description_index)
275
+ stsc.first_chunk.push(sample.sample_number)
276
+ stsc.samples_per_chunk.push(1)
277
+ stsc.sample_description_index.push(sample.sample_description_index)
278
+ previous_sample_description_index = sample.sample_description_index
279
+ end
280
+ }
281
+ stsc.entry_count = stsc.first_chunk.length
282
+ end
283
+ end
284
+
285
+ def output(path)
286
+ update
287
+ File.open(path, 'wb') do |f|
288
+ f.write @file_container
289
+ end
290
+ end
291
+ end
292
+ end
@@ -0,0 +1,269 @@
1
+ require 'bmff'
2
+
3
+ # At first, I intended to use "refinements" mechanism to minimize the effects of monkey patch, but BMFF::BOX::Base.parse_data
4
+ # can't be called from anywhere. I guess there is something wrong with the inheritance relationship, but I have no idea.
5
+ # So I have to use bare monkey patch, sorry.
6
+
7
+ class BMFF::FileContainer
8
+ def to_s
9
+ @children.map {|box| box.to_s}.join
10
+ end
11
+ end
12
+
13
+ class BMFF::Box::Base
14
+ attr_accessor :raw_data
15
+ def compose(data)
16
+ # size(uint32) + type(uint32)
17
+ size = 4 + 4
18
+ if @type == 'uuid'
19
+ # extended_type(uint8[16])
20
+ size += 16
21
+ end
22
+ # 8 is the size of largesize(uint64)
23
+ if size + 8 + data.length > 0xffffffff
24
+ largesize = size + 8 + data.length
25
+ size = 1
26
+ else
27
+ size += data.length
28
+ end
29
+
30
+ sio = StringIO.new("", "r+")
31
+ sio.set_encoding("ascii-8bit")
32
+ sio.extend(BMFF::BinaryAccessor)
33
+
34
+ sio.write_uint32(size)
35
+ sio.write_ascii(@type)
36
+ sio.write_uint64(largesize) if size == 1
37
+ sio.write_uuid(@usertype) if @type == 'uuid'
38
+ return sio.string + data
39
+ end
40
+
41
+ def parse_data
42
+ data_start_pos = @io.pos
43
+ seek_to_end
44
+ data_end_pos = @io.pos
45
+ @io.pos = data_start_pos
46
+ @raw_data = @io.read(data_end_pos - data_start_pos)
47
+ @io.pos = data_start_pos
48
+ end
49
+
50
+ def to_s
51
+ if container?
52
+ compose(@children.map {|box| box.to_s}.join)
53
+ else
54
+ compose(@raw_data)
55
+ end
56
+ end
57
+ end
58
+
59
+ class BMFF::Box::Full
60
+ def compose(data)
61
+ sio = StringIO.new("", "r+")
62
+ sio.set_encoding("ascii-8bit")
63
+ sio.extend(BMFF::BinaryAccessor)
64
+
65
+ sio.write_uint8(@version)
66
+ sio.write_uint24(@flags)
67
+ super(sio.string + data)
68
+ end
69
+
70
+ def parse_data
71
+ @version = io.get_uint8
72
+ @flags = io.get_uint24
73
+ super
74
+ end
75
+ end
76
+
77
+ class BMFF::Box::DataReference
78
+ def to_s
79
+ sio = StringIO.new("", "r+")
80
+ sio.set_encoding("ascii-8bit")
81
+ sio.extend(BMFF::BinaryAccessor)
82
+
83
+ sio.write_uint32(@entry_count)
84
+ compose(sio.string + @children.map {|box| box.to_s}.join)
85
+ end
86
+ end
87
+
88
+ class BMFF::Box::SampleDescription
89
+ def to_s
90
+ sio = StringIO.new("", "r+")
91
+ sio.set_encoding("ascii-8bit")
92
+ sio.extend(BMFF::BinaryAccessor)
93
+
94
+ sio.write_uint32(@entry_count)
95
+ compose(sio.string + @children.map {|box| box.to_s}.join)
96
+ end
97
+ end
98
+
99
+ class BMFF::Box::SampleSize
100
+ def to_s
101
+ sio = StringIO.new("", "r+")
102
+ sio.set_encoding("ascii-8bit")
103
+ sio.extend(BMFF::BinaryAccessor)
104
+
105
+ sio.write_uint32(@sample_size)
106
+ sio.write_uint32(@sample_count)
107
+ if @sample_size == 0
108
+ @sample_count.times do |i|
109
+ sio.write_uint32(@entry_size[i])
110
+ end
111
+ end
112
+ compose(sio.string)
113
+ end
114
+ end
115
+
116
+ class BMFF::Box::SyncSample
117
+ def to_s
118
+ sio = StringIO.new("", "r+")
119
+ sio.set_encoding("ascii-8bit")
120
+ sio.extend(BMFF::BinaryAccessor)
121
+
122
+ sio.write_uint32(@entry_count)
123
+ @entry_count.times do |i|
124
+ sio.write_uint32(@sample_number[i])
125
+ end
126
+ compose(sio.string)
127
+ end
128
+ end
129
+
130
+ class BMFF::Box::TimeToSample
131
+ def to_s
132
+ sio = StringIO.new("", "r+")
133
+ sio.set_encoding("ascii-8bit")
134
+ sio.extend(BMFF::BinaryAccessor)
135
+
136
+ sio.write_uint32(@entry_count)
137
+ @entry_count.times do |i|
138
+ sio.write_uint32(@sample_count[i])
139
+ sio.write_uint32(@sample_delta[i])
140
+ end
141
+ compose(sio.string)
142
+ end
143
+ end
144
+
145
+ class BMFF::Box::TimeToSample
146
+ def to_s
147
+ sio = StringIO.new("", "r+")
148
+ sio.set_encoding("ascii-8bit")
149
+ sio.extend(BMFF::BinaryAccessor)
150
+
151
+ sio.write_uint32(@entry_count)
152
+ @entry_count.times do |i|
153
+ sio.write_uint32(@sample_count[i])
154
+ sio.write_uint32(@sample_delta[i])
155
+ end
156
+ compose(sio.string)
157
+ end
158
+ end
159
+
160
+ class BMFF::Box::CompositionOffset
161
+ def to_s
162
+ sio = StringIO.new("", "r+")
163
+ sio.set_encoding("ascii-8bit")
164
+ sio.extend(BMFF::BinaryAccessor)
165
+
166
+ sio.write_uint32(@entry_count)
167
+ @entry_count.times do |i|
168
+ sio.write_uint32(@sample_count[i])
169
+ sio.write_uint32(@sample_offset[i])
170
+ end
171
+ compose(sio.string)
172
+ end
173
+ end
174
+
175
+ class BMFF::Box::CompositionOffset
176
+ def to_s
177
+ sio = StringIO.new("", "r+")
178
+ sio.set_encoding("ascii-8bit")
179
+ sio.extend(BMFF::BinaryAccessor)
180
+
181
+ sio.write_uint32(@entry_count)
182
+ @entry_count.times do |i|
183
+ sio.write_uint32(@sample_count[i])
184
+ sio.write_uint32(@sample_offset[i])
185
+ end
186
+ compose(sio.string)
187
+ end
188
+ end
189
+
190
+ class BMFF::Box::ChunkOffset
191
+ def to_s
192
+ sio = StringIO.new("", "r+")
193
+ sio.set_encoding("ascii-8bit")
194
+ sio.extend(BMFF::BinaryAccessor)
195
+
196
+ sio.write_uint32(@entry_count)
197
+ @entry_count.times do |i|
198
+ sio.write_uint32(@chunk_offset[i])
199
+ end
200
+ compose(sio.string)
201
+ end
202
+ end
203
+
204
+ class BMFF::Box::SampleToChunk
205
+ def to_s
206
+ sio = StringIO.new("", "r+")
207
+ sio.set_encoding("ascii-8bit")
208
+ sio.extend(BMFF::BinaryAccessor)
209
+
210
+ sio.write_uint32(@entry_count)
211
+ @entry_count.times do |i|
212
+ sio.write_uint32(@first_chunk[i])
213
+ sio.write_uint32(@samples_per_chunk[i])
214
+ sio.write_uint32(@sample_description_index[i])
215
+ end
216
+ compose(sio.string)
217
+ end
218
+ end
219
+
220
+ class BMFF::Box::SampleEntry
221
+ def parse_data
222
+ @reserved1 = []
223
+ 6.times do
224
+ @reserved1 << io.get_uint8
225
+ end
226
+ @data_reference_index = io.get_uint16
227
+ super
228
+ end
229
+
230
+ def compose(data)
231
+ sio = StringIO.new("", "r+")
232
+ sio.set_encoding("ascii-8bit")
233
+
234
+ sio.extend(BMFF::BinaryAccessor)
235
+ 6.times do |i|
236
+ sio.write_uint8(@reserved1[i])
237
+ end
238
+ sio.write_uint16(@data_reference_index)
239
+ super(sio.string + data)
240
+ end
241
+ end
242
+
243
+ class BMFF::Box::VisualSampleEntry
244
+ def to_s
245
+ sio = StringIO.new("", "r+")
246
+ sio.set_encoding("ascii-8bit")
247
+ sio.extend(BMFF::BinaryAccessor)
248
+
249
+ sio.write_uint16(@pre_defined1)
250
+ sio.write_uint16(@reserved2)
251
+ 3.times do |i|
252
+ sio.write_uint32(@pre_defined2[i])
253
+ end
254
+ sio.write_uint16(@width)
255
+ sio.write_uint16(@height)
256
+ sio.write_uint32(@horizresolution)
257
+ sio.write_uint32(@vertresolution)
258
+ sio.write_uint32(@reserved3)
259
+ sio.write_uint16(@frame_count)
260
+ sio.write_uint8(@compressorname.length)
261
+ sio.write_ascii(@compressorname)
262
+ (31 - @compressorname.length).times do
263
+ sio.write_uint8(0) #padding with 0
264
+ end
265
+ sio.write_uint16(@depth)
266
+ sio.write_int16(@pre_defined3)
267
+ compose(sio.string + @children.map {|box| box.to_s}.join)
268
+ end
269
+ end
@@ -0,0 +1,52 @@
1
+ module BMFFGlitch
2
+ class Sample
3
+ VISUALSAMPLE = 1
4
+ AUDIOSAMPLE = 2
5
+ HINTSAMPLE = 4
6
+ SYNCSAMPLE = 8
7
+
8
+ attr_accessor :sample_number, :flag, :data, :size, :sample_offset, :sample_delta, :chunk_number, :chunk_offset, :file_offset, :sample_description_index
9
+ def initialize(sample_number, flag)
10
+ @sample_number = sample_number
11
+ @flag = flag
12
+ @data = ""
13
+ @size = 0
14
+ @sample_offset = 0
15
+ @sample_delta = 0
16
+ @chunk_number = 0
17
+ @chunk_offset = 0
18
+ @file_offset = 0
19
+ @sample_description_index = 0
20
+ end
21
+
22
+ def initialize_copy(obj)
23
+ # make deep copy
24
+ @sample_number = obj.sample_number
25
+ @flag = obj.flag
26
+ @data = obj.data.dup
27
+ @size = obj.size
28
+ @sample_offset = obj.sample_offset
29
+ @sample_delta = obj.sample_delta
30
+ @chunk_number = obj.chunk_number
31
+ @chunk_offset = obj.chunk_offset
32
+ @file_offset = obj.file_offset
33
+ @sample_description_index = obj.sample_description_index
34
+ end
35
+
36
+ def is_visualsample?
37
+ (flag & VISUALSAMPLE) != 0 ? true : false
38
+ end
39
+
40
+ def is_audiosample?
41
+ (flag & AUDIOSAMPLE) != 0 ? true : false
42
+ end
43
+
44
+ def is_hintsample?
45
+ (flag & HINTSAMPLE) != 0 ? true : false
46
+ end
47
+
48
+ def is_syncsample?
49
+ (flag & SYNCSAMPLE) != 0 ? true : false
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module BMFFGlitch
2
+ VERSION = '0.0.1'
3
+ end
data/lib/bmffglitch.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'bmffglitch/version'
2
+ require 'bmffglitch/bmffex'
3
+ require 'bmffglitch/base'
4
+ require 'bmffglitch/sample'
5
+
6
+ # BMFFGlitch: A library to destroy your movies stored in ISO Base Media File Format(BMFF)
7
+ # and its relatives(such as MP4), like AviGlitch
8
+ #
9
+ # == Synopsis:
10
+ #
11
+ # You can manipulate each sample(this may be video frame or audio data), like this:
12
+ #
13
+ # bmff = BMFFGlitch.open("/path/to/input.mp4")
14
+ # bmff.samples.each do |sample|
15
+ # if sample.is_syncsample?
16
+ # sample.data.gsub!(/\d/, '0')
17
+ # end
18
+ # end
19
+ # bmff.output('/path/to/output.mp4')
20
+
21
+ module BMFFGlitch
22
+ def self.open(path)
23
+ BMFFGlitch::Base.new(path)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bmffglitch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - sabishirop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bmff
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.2
69
+ description: A Ruby library to destroy files stored in ISO Base Media File Format(BMFF)
70
+ and its relatives
71
+ email:
72
+ - sabishirop@gmail.com
73
+ executables:
74
+ - bmffdtmsh
75
+ - console
76
+ - setup
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".rspec"
82
+ - ".travis.yml"
83
+ - CODE_OF_CONDUCT.md
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/bmffdtmsh
90
+ - bin/console
91
+ - bin/setup
92
+ - bmffglitch.gemspec
93
+ - lib/bmffglitch.rb
94
+ - lib/bmffglitch/base.rb
95
+ - lib/bmffglitch/bmffex.rb
96
+ - lib/bmffglitch/sample.rb
97
+ - lib/bmffglitch/version.rb
98
+ homepage: http://sabishirop.github.com/bmffglitch
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.6.13
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: A Ruby library to destroy files stored in ISO Base Media File Format(BMFF)
122
+ and its relatives
123
+ test_files: []