lib/scissor/chunk.rb in scissor-0.1.2 vs lib/scissor/chunk.rb in scissor-0.2.0

- old
+ new

@@ -1,5 +1,8 @@ +require 'open-uri' +require 'tempfile' + module Scissor class Chunk class Error < StandardError; end class EmptyFragment < Error; end class OutOfDuration < Error; end @@ -13,9 +16,38 @@ @fragments << Fragment.new( filename, 0, SoundFile.new(filename).length) end + end + + def self.new_from_url(url) + file = nil + content_types = { + 'audio/wav' => 'wav', + 'audio/x-wav' => 'wav', + 'audio/wave' => 'wav', + 'audio/x-pn-wav' => 'wav', + 'audio/mpeg' => 'mp3', + 'audio/x-mpeg' => 'mp3', + 'audio/mp3' => 'mp3', + 'audio/x-mp3' => 'mp3', + 'audio/mpeg3' => 'mp3', + 'audio/x-mpeg3' => 'mp3', + 'audio/mpg' => 'mp3', + 'audio/x-mpg' => 'mp3', + 'audio/x-mpegaudio' => 'mp3', + } + + open(url) do |f| + ext = content_types[f.content_type.downcase] + + file = Tempfile.new(['audio', '.' + ext]) + file.write(f.read) + file.flush + end + + new(file.path) end def add_fragment(fragment) @fragments << fragment end