lib/dragonfly-ffmpeg/encoder.rb in dragonfly-ffmpeg-0.0.3 vs lib/dragonfly-ffmpeg/encoder.rb in dragonfly-ffmpeg-0.0.4

- old
+ new

@@ -27,11 +27,11 @@ include ::Dragonfly::Configurable configurable_attr :encoder_profiles, { :mp4 => [ - Profile.new(:default, + Profile.new(:html5, :video_codec => "libx264", :resolution => "1280x720", :frame_rate => 29.97, :video_bitrate => 3072, :audio_codec => "libfaac", @@ -39,22 +39,22 @@ :audio_sample_rate => 48000, :video_preset => "hq" ) ], :ogv => [ - Profile.new(:default, + Profile.new(:html5, :video_codec => "libtheora", :resolution => "1280x720", :frame_rate => 29.97, :video_bitrate => 3072, :audio_codec => "libvorbis", :audio_channels => 2, :audio_sample_rate => 48000 ) ], :webm => [ - Profile.new(:default, + Profile.new(:html5, :video_codec => "libvpx", :resolution => "1280x720", :frame_rate => 29.97, :video_bitrate => 3072, :audio_codec => "libvorbis", @@ -65,35 +65,45 @@ ] } configurable_attr :output_directory, '/tmp' - def encode(temp_object, format, profile_id = :default, options = {}) - if profile_id.is_a?(Profile) - profile = profile_id - else - raise UnknownEncoderProfile unless profile_defined?(format, profile_id.to_sym) - profile = get_profile(format, profile_id.to_sym) + def encode(temp_object, format, profile = :html5, options = {}) + format = format.to_sym + raise UnsupportedFormat, "Format not supported - #{format}" unless supported_format?(format) + unless profile.is_a?(Profile) + raise UnknownEncoderProfile unless profile_defined?(format, profile.to_sym) + profile = get_profile(format, profile.to_sym) end options.merge!(profile.encoding_options) origin = ::FFMPEG::Movie.new(temp_object.path) - new_filename = ::Pathname.new(temp_object.path).sub_ext(".#{format}").basename - new_filepath = File.join(output_directory, new_filename) - transcoded = origin.transcode(new_filepath, options) - ::Dragonfly::TempObject.new(File.read(transcoded.path)) + tempfile = new_tempfile(format) + transcoded = origin.transcode(tempfile.path, options) + ::Dragonfly::TempObject.new(File.new(transcoded.path)) end - + private + def new_tempfile(ext = nil) + tempfile = ext ? Tempfile.new(["dragonfy-video", ".#{ext}"]) : Tempfile.new("dragonfly-video") + tempfile.binmode + tempfile.close + tempfile + end + def profiles(format) encoder_profiles[format] end def get_profile(format, profile_name) result = profiles(format).select { |profile| profile.name == profile_name } result.first + end + + def supported_format?(format) + encoder_profiles.has_key?(format) end def profile_defined?(format, profile_name) return false if profiles(format).nil?