Permalink
Browse files

add Scissor::Chunk.new_from_url

  • Loading branch information...
1 parent e8520db commit f16eba012102b964156c0afe2d4c2cb8db797a53 @youpy committed Dec 1, 2011
Showing with 52 additions and 2 deletions.
  1. +1 −0 Rakefile
  2. +6 −2 lib/scissor.rb
  3. +32 −0 lib/scissor/chunk.rb
  4. +13 −0 spec/scissor_spec.rb
View
@@ -63,6 +63,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency('riff', '<= 0.3.0')
s.add_dependency('tempdir')
s.add_dependency('streamio-ffmpeg')
+ s.add_development_dependency('fakeweb')
#s.required_ruby_version = '>= 1.8.2'
s.files = %w(README.rdoc ChangeLog Rakefile) +
View
@@ -5,8 +5,12 @@
require 'scissor/sequence'
require 'scissor/writer'
-def Scissor(*args)
- Scissor::Chunk.new(*args)
+def Scissor(filename_or_url = nil)
+ if filename_or_url && filename_or_url =~ /^http/
+ Scissor::Chunk.new_from_url(filename_or_url)
+ else
+ Scissor::Chunk.new(filename_or_url)
+ end
end
require 'logger'
View
@@ -1,3 +1,6 @@
+require 'open-uri'
+require 'tempfile'
+
module Scissor
class Chunk
class Error < StandardError; end
@@ -17,6 +20,35 @@ def initialize(filename = nil)
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
View
@@ -2,6 +2,7 @@
require 'spec_helper'
require 'fileutils'
+require 'fakeweb'
include FileUtils
@@ -11,12 +12,24 @@
@mp3 = Scissor(fixture('sample.mp3'))
mkdir '/tmp/scissor-test'
+
+ FakeWeb.clean_registry
end
after do
rm_rf '/tmp/scissor-test'
end
+ it "should create from url" do
+ url = 'http://example.com/sample_mp3'
+ FakeWeb.register_uri(:get, url, :body => fixture('sample.mp3'), :content_type => 'audio/mpeg')
+
+ mp3 = Scissor(url)
+
+ mp3.should be_an_instance_of(Scissor::Chunk)
+ mp3.duration.should be_close(178.1, 0.1)
+ end
+
it "should get duration" do
@mp3.should respond_to(:duration)
@mp3.duration.should be_close(178.1, 0.1)

0 comments on commit f16eba0

Please sign in to comment.