lib/scissor/chunk.rb in youpy-scissor-0.0.19 vs lib/scissor/chunk.rb in youpy-scissor-0.0.20
- old
+ new
@@ -1,25 +1,13 @@
require 'digest/md5'
require 'pathname'
-require 'open4'
-require 'logger'
-require 'temp_dir'
module Scissor
class Chunk
- @logger = Logger.new(STDOUT)
- @logger.level = Logger::INFO
-
- class << self
- attr_accessor :logger
- end
-
class Error < StandardError; end
- class FileExists < Error; end
class EmptyFragment < Error; end
class OutOfDuration < Error; end
- class CommandFailed < Error; end
attr_reader :fragments
def initialize(filename = nil)
@fragments = []
@@ -168,106 +156,20 @@
new_instance
end
def to_file(filename, options = {})
- filename = Pathname.new(filename)
+ writer = Writer.new
- if @fragments.empty?
- raise EmptyFragment
- end
+ writer.add_track(@fragments)
+ writer.to_file(filename, options)
- which('ecasound')
- which('ffmpeg')
-
- options = {
- :overwrite => false
- }.merge(options)
-
- filename = Pathname.new(filename)
-
- if filename.exist?
- if options[:overwrite]
- filename.unlink
- else
- raise FileExists
- end
- end
-
- position = 0.0
-
- TempDir.create do |dir|
- tmpdir = Pathname.new(dir)
- tmpfile = tmpdir + 'tmp.wav'
- cmd = %w/ecasound/
-
- @fragments.each_with_index do |fragment, index|
- fragment_filename = fragment.filename
- fragment_duration = fragment.duration
-
- if !index.zero? && (index % 80).zero?
- run_command(cmd.join(' '))
- cmd = %w/ecasound/
- end
-
- fragment_tmpfile =
- fragment_filename.extname.downcase == '.wav' ? fragment_filename :
- tmpdir + (Digest::MD5.hexdigest(fragment_filename) + '.wav')
-
- unless fragment_tmpfile.exist?
- run_command("ffmpeg -i \"#{fragment_filename}\" \"#{fragment_tmpfile}\"")
- end
-
- cmd <<
- "-a:#{index} " +
- "-i:" +
- (fragment.reversed? ? 'reverse,' : '') +
- "select,#{fragment.start},#{fragment_duration},\"#{fragment_tmpfile}\" " +
- "-o:#{tmpfile} " +
- "-y:#{position}"
-
- position += fragment_duration
- end
-
- run_command(cmd.join(' '))
-
- if filename.extname == '.wav'
- File.rename(tmpfile, filename)
- else
- run_command("ffmpeg -i \"#{tmpfile}\" \"#{filename}\"")
- end
- end
-
self.class.new(filename)
end
alias > to_file
def >>(filename)
to_file(filename, :overwrite => true)
- end
-
- def which(command)
- run_command("which #{command}")
- end
-
- def run_command(cmd)
- logger.debug("run_command: #{cmd}")
-
- result = ''
- status = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
- logger.debug(stderr.read)
- result = stdout.read
- end
-
- if status.exitstatus != 0
- raise CommandFailed.new(cmd)
- end
-
- return result
- end
-
- def logger
- self.class.logger
end
end
end