Skip to content

Commit

Permalink
Fix #7 to allow a custom temp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ucnv committed Jul 8, 2024
1 parent 4dc15b7 commit 485f398
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
8 changes: 6 additions & 2 deletions bin/datamosh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require 'aviglitch'
output = './out.avi'
all = false
fake = false
tmpdir = nil

opts = OptionParser.new do |opts|
opts.banner = "datamosh - AviGlitch's datamoshing video generator."
Expand All @@ -28,6 +29,9 @@ opts = OptionParser.new do |opts|
exit
end
end
opts.on("--tmpdir [DIR]", "Specify the temporary directory") do |dir|
tmpdir = dir
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
Expand All @@ -48,7 +52,7 @@ else
end
end

a = AviGlitch.open input.shift
a = AviGlitch.open input.shift, tmpdir: tmpdir
unless fake
a.glitch_with_index :keyframe do |frame, i|
(!all && i == 0) ? frame : "" # keep the first frame
Expand All @@ -62,7 +66,7 @@ else
end

input.each do |file|
b = AviGlitch.open file
b = AviGlitch.open file, tmpdir: tmpdir
unless fake
b.glitch :keyframe do |frame|
""
Expand Down
5 changes: 3 additions & 2 deletions lib/aviglitch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ class << self
##
# Returns AviGlitch::Base instance.
# It requires +path_or_frames+ as String or Pathname, or Frames instance.
def open path_or_frames
# Additionally, it allows +tmpdir:+ as the internal temporary directory.
def open path_or_frames, tmpdir: nil
if path_or_frames.kind_of?(Frames)
path_or_frames.to_avi
else
AviGlitch::Base.new(Pathname(path_or_frames))
AviGlitch::Base.new(Pathname(path_or_frames), tmpdir: tmpdir)
end
end
end
Expand Down
15 changes: 10 additions & 5 deletions lib/aviglitch/avi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ def inspect
# Object which represents RIFF structure.
attr_accessor :riff

attr_accessor :path, :movi #:nodoc:
attr_accessor :path, :movi, :tmpdir #:nodoc:
protected :path, :path=, :movi, :movi=

##
# Generates an instance with the necessary structure from the +path+.
# Generates an instanc.
def initialize path = nil
return if path.nil?
self.path = path unless path.nil?
end

##
# Set +path+ of the source file.
def path= path #:nodoc:
@path = path
File.open(path, 'rb') do |io|
@movi = Tempfile.new 'aviglitch', binmode: true
@movi = Tempfile.new 'aviglitch', @tmpdir, binmode: true
@riff = []
@indices = []
@superidx = []
Expand Down Expand Up @@ -395,7 +400,7 @@ def initialize_copy avi #:nodoc:
avi.indices = Marshal.load md
md = Marshal.dump @riff
avi.riff = Marshal.load md
newmovi = Tempfile.new 'aviglitch', binmode: true
newmovi = Tempfile.new 'aviglitch', @tmpdir, binmode: true
movipos = @movi.pos
@movi.rewind
newmovi.print @movi.read
Expand Down
6 changes: 4 additions & 2 deletions lib/aviglitch/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class 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.
def initialize path_or_object
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 path_or_object
@avi = Avi.new
@avi.tmpdir = tmpdir
@avi.path = path_or_object
end
@frames = Frames.new @avi
end
Expand Down
3 changes: 2 additions & 1 deletion lib/aviglitch/frames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Frames

##
# Creates a new AviGlitch::Frames object.
# It takes AviGlitch::Avi as an argument.
def initialize avi
@avi = avi
end
Expand All @@ -31,7 +32,7 @@ def initialize avi
# It returns Enumerator if a block is not given.
def each &block
if block_given?
Tempfile.open('temp', binmode: true) do |newmovi|
Tempfile.open('temp', @avi.tmpdir, binmode: true) do |newmovi|
@avi.process_movi do |indices, movi|
newindices = indices.select do |m|
movi.pos = m[:offset] + 8 # 8 for id and size
Expand Down
23 changes: 21 additions & 2 deletions spec/aviglitch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
}.should raise_error(IOError)
end

it 'can explicit close file' do
it 'can close the file explicitly' do
avi = AviGlitch.open @in
avi.close
lambda {
Expand Down Expand Up @@ -160,7 +160,7 @@
end
end

it 'should check if keyframes exist.' do
it 'should check if keyframes exist' do
a = AviGlitch.open @in
a.has_keyframe?.should be true
a.glitch :keyframe do |f|
Expand Down Expand Up @@ -190,5 +190,24 @@

expect(dc1).to eq(dc2)
end

it 'can be set custom temp dir' do
custom_tmpdir = Pathname.new(OUTPUT_DIR) + 'custom_tmpdir'
Dir.mkdir custom_tmpdir unless File.exist? custom_tmpdir

c = Dir.glob((custom_tmpdir + '*').to_s).size
b = AviGlitch.open @in, tmpdir: custom_tmpdir
b2 = b.frames[0, 100].to_avi
b2.frames.each do |f|
Dir.glob((custom_tmpdir + '*').to_s).size.should >= c
end
end

it 'shoud raise an error when specified temp dir is not writable' do
custom_tmpdir = Pathname.new(OUTPUT_DIR) + 'not_dir'
lambda {
AviGlitch.open @in, tmpdir: custom_tmpdir
}.should raise_error(SystemCallError)
end

end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
end

config.after(:each) do
FileUtils.rm Dir.glob((OUTPUT_DIR + '*').to_s)
FileUtils.rm_r Dir.glob((OUTPUT_DIR + '*').to_s)
end
end

0 comments on commit 485f398

Please sign in to comment.