class AviGlitch::Base
Base
is the object that provides interfaces mainly used. To glitch, and save file. The instance is returned through AviGlitch#open.
Attributes
The input file
AviGlitch::Frames
object generated from the file
.
Public Class Methods
Creates a new instance of AviGlitch::Base
, open the file and make it ready to manipulate. It requires path
as Pathname or an instance of AviGlirtch::Avi.
# File lib/aviglitch/base.rb, line 17 def initialize path_or_object, tmpdir: nil if path_or_object.kind_of?(Avi) @avi = path_or_object else unless AviGlitch::Base.surely_formatted? path_or_object raise 'Unsupported file passed.' end @avi = Avi.new @avi.tmpdir = tmpdir @avi.path = path_or_object end @frames = Frames.new @avi end
Checks if the file
is a correctly formetted AVI file. file
can be String or Pathname or IO.
# File lib/aviglitch/base.rb, line 132 def surely_formatted? file, debug = false passed = true begin riff = Avi.rifftree file { 'RIFF-AVI sign': /^RIFF \(\d+\) ’AVI ’$/, 'movi': /^\s+LIST \(\d+\) ’movi’$/, 'idx1': /^\s+idx1 \(\d+\)$/ }.each do |m, r| unless riff =~ r warn "#{m} is not found." if debug passed = false end end rescue => e warn e.message if debug passed = false end passed end
Public Instance Methods
An explicit file close.
# File lib/aviglitch/base.rb, line 41 def close @avi.close end
Swaps the frames with other Frames
data.
# File lib/aviglitch/base.rb, line 119 def frames= other raise TypeError unless other.kind_of?(Frames) @frames.clear @frames.concat other end
Glitches each frame data. It is a convenient method to iterate each frame.
The argument target
takes symbols listed below:
:keyframe
or:iframe
-
select video key frames (aka I-frame)
:deltaframe
or:pframe
-
select video delta frames (difference frames)
:videoframe
-
select both of keyframe and deltaframe
:audioframe
-
select audio frames
:all
-
select all frames
It also requires a block. In the block, you take the frame data as a String parameter. To modify the data, simply return a modified data. Without a block it returns Enumerator, with a block it returns self
.
# File lib/aviglitch/base.rb, line 60 def glitch target = :all, &block # :yield: data if block_given? @frames.each do |frame| if frame.is? target frame.data = yield frame.data end end self else self.enum_for :glitch, target end end
Do glitch with index.
# File lib/aviglitch/base.rb, line 75 def glitch_with_index target = :all, &block # :yield: data, index if block_given? self.glitch(target).with_index do |x, i| yield x, i end self else self.glitch target end end
Check if it has keyframes.
# File lib/aviglitch/base.rb, line 96 def has_keyframe? result = false self.frames.each do |f| if f.is_keyframe? result = true break end end result end
Mutates all (or in range
) keyframes into deltaframes. It’s an alias for Frames#mutate_keyframes_into_deltaframes!
# File lib/aviglitch/base.rb, line 89 def mutate_keyframes_into_deltaframes! range = nil self.frames.mutate_keyframes_into_deltaframes! range self end
Outputs the glitched file to path
, and close the file.
# File lib/aviglitch/base.rb, line 33 def output path, do_file_close = true @avi.output path close if do_file_close self end
Removes all keyframes. It is same as +glitch(:keyframes){|f| nil }+
# File lib/aviglitch/base.rb, line 110 def remove_all_keyframes! self.glitch :keyframe do |f| nil end self end