gbissett (owner)

Revisions

gist: 219394 Download_button fork
public
Public Clone URL: git://gist.github.com/219394.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Paperclip
  # Creates short snippets of an mp3
  # SoX FTW! http://sox.sourceforge.net/sox.html
  class TrackPreview < Processor
 
    def initialize(file, options = {})
      super
      @file = file
      @current_format = File.extname(@file.path)
      @basename = File.basename(@file.path, @current_format)
    end
 
    def make
      output_file = Tempfile.new([@basename, @current_format].compact.join("."))
      output_file.binmode
      trim = "trim 10 30" # start at 10 seconds, duration 30 seconds.
      fade = "fade 2 30" # fade duration is 2 seconds, end fade-out at 30 seconds.
      begin
        success = Paperclip.run('sox', "-t mp3 #{File.expand_path(file.path)} -t mp3 #{File.expand_path(output_file.path)} #{trim} #{fade}")
      rescue PaperclipCommandLineError
        raise PaperclipError, "There was an error processing the clip for #{@basename}"
      end
      output_file
    end
 
  end
end