Sha256: 6ab233d181835b9271cb677823beeb537ce53747a46efa44ca898d2b162f8d1d

Contents?: true

Size: 1.63 KB

Versions: 13

Compression:

Stored size: 1.63 KB

Contents

require 'attached/processor/base'
require 'attached/processor/error'

module Attached
  module Processor
    class Audio < Base


      attr_reader :path
      attr_reader :extension
      attr_reader :preset
      attr_reader :attachment


      # Create a processor.
      #
      # Parameters:
      #
      # * file       - The file to be processed.
      # * options    - The options to be applied to the processing.
      # * attachment - The attachment the processor is being run for.

      def initialize(file, options = {}, attachment = nil)
        super

        @path      = self.file.path

        @preset    = options[:preset]
        @extension = options[:extension]

        @extension ||= self.attachment.extension
      end


      # Redirect output path.

      def redirect
        ">/dev/null 2>&1" if File.exist?("/dev/null")
      end


      # Helper function for calling processors.
      #
      # Usage:
      #
      #   self.process

      def process

        result = Tempfile.new(["", self.extension])
        result.binmode

        begin

          parameters = []

          parameters << "--preset #{self.preset}" if self.preset

          parameters << self.path
          parameters << result.path

          parameters = parameters.join(" ").squeeze(" ")

          `lame #{parameters} #{redirect}`

          raise Errno::ENOENT if $?.exitstatus == 127

        rescue Errno::ENOENT
          raise "command 'lame' not found: ensure LAME is installed"
        end

        unless $?.exitstatus == 0
          raise Attached::Processor::Error, "must be an audio file"
        end

        return result

      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
attached-1.0.4 lib/attached/processor/audio.rb
attached-1.0.3 lib/attached/processor/audio.rb
attached-1.0.2 lib/attached/processor/audio.rb
attached-1.0.1 lib/attached/processor/audio.rb
attached-1.0.0 lib/attached/processor/audio.rb
attached-0.6.0 lib/attached/processor/audio.rb
attached-0.5.9 lib/attached/processor/audio.rb
attached-0.5.8 lib/attached/processor/audio.rb
attached-0.5.7 lib/attached/processor/audio.rb
attached-0.5.6 lib/attached/processor/audio.rb
attached-0.5.5 lib/attached/processor/audio.rb
attached-0.5.4 lib/attached/processor/audio.rb
attached-0.5.3 lib/attached/processor/audio.rb