Skip to content

Instantly share code, notes, and snippets.

@gbissett
Created October 27, 2009 07:23
Show Gist options
  • Save gbissett/219394 to your computer and use it in GitHub Desktop.
Save gbissett/219394 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment