Class: AviGlitch::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/aviglitch/base.rb

Overview

Base is the object that provides interfaces mainly used. To glitch, and save file. The instance is returned through AviGlitch#open.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_or_object, tmpdir: nil) ⇒ Base

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.



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#aviObject (readonly)

The input file



11
12
13
# File 'lib/aviglitch/base.rb', line 11

def avi
  @avi
end

#framesObject

AviGlitch::Frames object generated from the file.



9
10
11
# File 'lib/aviglitch/base.rb', line 9

def frames
  @frames
end

Class Method Details

.surely_formatted?(file, debug = false) ⇒ Boolean

Checks if the file is a correctly formetted AVI file. file can be String or Pathname or IO.

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

Instance Method Details

#closeObject

An explicit file close.



41
42
43
# File 'lib/aviglitch/base.rb', line 41

def close
  @avi.close
end

#glitch(target = :all, &block) ⇒ Object

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.



60
61
62
63
64
65
66
67
68
69
70
71
# 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

#glitch_with_index(target = :all, &block) ⇒ Object

Do glitch with index.



75
76
77
78
79
80
81
82
83
84
# 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

#has_keyframe?Boolean Also known as: has_keyframes?

Check if it has keyframes.

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
# 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

#mutate_keyframes_into_deltaframes!(range = nil) ⇒ Object

Mutates all (or in range) keyframes into deltaframes. It’s an alias for Frames#mutate_keyframes_into_deltaframes!



89
90
91
92
# File 'lib/aviglitch/base.rb', line 89

def mutate_keyframes_into_deltaframes! range = nil
  self.frames.mutate_keyframes_into_deltaframes! range
  self
end

#output(path, do_file_close = true) ⇒ Object Also known as: write

Outputs the glitched file to path, and close the file.



33
34
35
36
37
# File 'lib/aviglitch/base.rb', line 33

def output path, do_file_close = true
  @avi.output path
  close if do_file_close
  self
end

#remove_all_keyframes!Object

Removes all keyframes. It is same as glitch(:keyframes){|f| nil }



110
111
112
113
114
115
# File 'lib/aviglitch/base.rb', line 110

def remove_all_keyframes!
  self.glitch :keyframe do |f|
    nil
  end
  self
end