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) ⇒ 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
# File 'lib/aviglitch/base.rb', line 17

def initialize path_or_object
  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 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)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/aviglitch/base.rb', line 130

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.



39
40
41
# File 'lib/aviglitch/base.rb', line 39

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.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aviglitch/base.rb', line 58

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/aviglitch/base.rb', line 73

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)


94
95
96
97
98
99
100
101
102
103
# File 'lib/aviglitch/base.rb', line 94

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!



87
88
89
90
# File 'lib/aviglitch/base.rb', line 87

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.



31
32
33
34
35
# File 'lib/aviglitch/base.rb', line 31

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 }



108
109
110
111
112
113
# File 'lib/aviglitch/base.rb', line 108

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