lib/aviglitch/frames.rb in aviglitch-0.1.3 vs lib/aviglitch/frames.rb in aviglitch-0.1.4
- old
+ new
@@ -16,12 +16,14 @@
# of AviGlitch::Frame object.
#
class Frames
include Enumerable
- SAFE_FRAMES_COUNT = 150000 #:nodoc:
- @@warn_if_frames_are_too_large = true #:nodoc:
+ # :stopdoc:
+ SAFE_FRAMES_COUNT = 150000
+ @@warn_if_frames_are_too_large = true
+ # :startdoc:
attr_reader :meta
##
# Creates a new AviGlitch::Frames object.
@@ -54,23 +56,37 @@
@io = io
end
##
# Enumerates the frames.
+ # It returns Enumerator if a block is not given.
def each
- temp = Tempfile.new 'frames'
- frames_data_as_io(temp, Proc.new)
- overwrite temp
- temp.close!
+ if block_given?
+ temp = Tempfile.new 'frames'
+ frames_data_as_io(temp, Proc.new)
+ overwrite temp
+ temp.close!
+ else
+ self.enum_for :each
+ end
end
##
# Returns the number of frames.
def size
@meta.size
end
+ ##
+ # Returns the number of the specific +frame_type+.
+ def size_of frame_type
+ detection = "is_#{frame_type.to_s.sub(/frames$/, 'frame')}?"
+ @meta.select { |m|
+ Frame.new(nil, m[:id], m[:flag]).send detection
+ }.size
+ end
+
def frames_data_as_io io = nil, block = nil #:nodoc:
io = Tempfile.new('tmep') if io.nil?
@meta = @meta.select do |m|
@io.pos = @pos_of_movi + m[:offset] + 8 # 8 for id and size
frame = Frame.new(@io.read(m[:size]), m[:id], m[:flag])
@@ -97,24 +113,23 @@
unless safe_frames_count? @meta.size
@io.close!
exit
end
# Overwrite the file
- data.seek 0, IO::SEEK_END
@io.pos = @pos_of_movi - 4 # 4 for size
@io.print [data.pos + 4].pack('V') # 4 for 'movi'
@io.print 'movi'
data.rewind
- while d = data.read(1024) do
+ while d = data.read(BUFFER_SIZE) do
@io.print d
end
@io.print 'idx1'
@io.print [@meta.size * 16].pack('V')
- @meta.each do |m|
- @io.print m[:id]
- @io.print [m[:flag], m[:offset], m[:size]].pack('V3')
- end
+ idx = @meta.collect { |m|
+ m[:id] + [m[:flag], m[:offset], m[:size]].pack('V3')
+ }.join
+ @io.print idx
eof = @io.pos
@io.truncate eof
# Fix info
## file size
@@ -150,11 +165,11 @@
other_data = Tempfile.new 'other'
other_frames.frames_data_as_io other_data
this_data.seek 0, IO::SEEK_END
this_size = this_data.pos
other_data.rewind
- while d = other_data.read(1024) do
+ while d = other_data.read(BUFFER_SIZE) do
this_data.print d
end
other_data.close!
# meta
other_meta = other_frames.meta.collect do |m|
@@ -322,11 +337,11 @@
def delete_at n
self.slice! n
end
##
- # Modify keyframes to deltaframes at given range, or all.
- def clear_keyframes! range = nil
+ # Mutates keyframes into deltaframes at given range, or all.
+ def mutate_keyframes_into_deltaframes! range = nil
range = 0..self.size if range.nil?
self.each_with_index do |frame, i|
if range.include? i
frame.flag = 0 if frame.is_keyframe?
end