From 3228e1c75bd9f33ed7f4a704f35682cffb677938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ca=CC=81ssio=20Marques?= Date: Thu, 7 Jul 2011 15:20:47 -0300 Subject: [PATCH] Adds configuration for transcoding using hashes --- lib/uencode/elements.rb | 33 ++++++++++++++++++++++++++++++++ spec/elements_spec.rb | 33 ++++++++++++++++++++++++++++++++ spec/fixtures/configuration.yaml | 23 ++++++++++++++++++++++ spec/spec_helper.rb | 1 - 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/configuration.yaml diff --git a/lib/uencode/elements.rb b/lib/uencode/elements.rb index cd75a7d..3aa59cc 100644 --- a/lib/uencode/elements.rb +++ b/lib/uencode/elements.rb @@ -111,11 +111,40 @@ def to_xml # Medium is a single video to transcode class Medium + attr_reader :video_config, :audio_config + def initialize @video_config = VideoConfig.new @audio_config = AudioConfig.new end + # Configures the transcoding using a nested hash with the following format: + # + # config = {"video" => { ... }, "audio" => { ... } + # + # The keys for the "video" hash can be any of the following: bitrate, codec, cbr, crop, + # deinterlace, framerate, height, keyframe_interval, maxbitrate, par, profile, passes, + # stretch, width. + # + # The "framerate" and "par" values must be also hashes, with the following format: + # + # {"numerator" => 10, "denominator" => 11} + # + # The keys for the "audio" hash can be any of the following: + # codec, bitrate, channels, samplerate. + # + def configure(hash) + video = hash["video"] + audio = hash["audio"] + configure_video do |c| + video.each_pair { |key, value| c.send("#{key}=", value) } + end + + configure_audio do |c| + audio.each_pair { |key, value| c.send("#{key}=", value) } + end + end + def configure_video yield @video_config end @@ -186,6 +215,10 @@ class Job include Enumerable + def self.from_hash(hash) + new({}) + end + def initialize(options) @video_output = VideoOutput.new options[:video_output] || {} @captures = [] diff --git a/spec/elements_spec.rb b/spec/elements_spec.rb index c9ebaad..fb54fa7 100644 --- a/spec/elements_spec.rb +++ b/spec/elements_spec.rb @@ -31,6 +31,9 @@ describe "#configure_video" do let(:medium) { UEncode::Medium.new } + let(:config) { YAML::load_file("spec/fixtures/configuration.yaml") } + let(:video_config) { config["video"] } + let(:audio_config) { config["audio"] } it { medium.video.bitrate.should == 10000 } it { medium.video.codec.should == "mp4" } @@ -46,6 +49,36 @@ it { medium.video.passes.should == 1 } it { medium.video.stretch.should == false } it { medium.video.width.should == 400 } + + context "from a hash (video parameters)" do + before { medium.configure config } + subject { medium.video_config } + + its(:bitrate) { should == video_config["bitrate"] } + its(:codec) { should == video_config["codec"] } + its(:cbr) { should == video_config["cbr"] } + its(:crop) { should == video_config["crop"] } + its(:deinterlace) { should == video_config["deinterlace"] } + its(:framerate) { should == video_config["framerate"] } + its(:height) { should == video_config["height"] } + its(:keyframe_interval) { should == video_config["keyframe_interval"] } + its(:maxbitrate) { should == video_config["maxbitrate"] } + its(:par) { should == video_config["par"] } + its(:profile) { should == video_config["profile"] } + its(:passes) { should == video_config["passes"] } + its(:stretch) { should == video_config["stretch"] } + its(:width) { should == video_config["width"] } + end + + context "from a hash (audio parameters)" do + before { medium.configure config } + subject { medium.audio_config } + + its(:codec) { should == audio_config["codec"] } + its(:bitrate) { should == audio_config["bitrate"] } + its(:channels) { should == audio_config["channels"] } + its(:samplerate) { should == audio_config["samplerate"] } + end end describe "#configure_audio" do diff --git a/spec/fixtures/configuration.yaml b/spec/fixtures/configuration.yaml new file mode 100644 index 0000000..bf2da83 --- /dev/null +++ b/spec/fixtures/configuration.yaml @@ -0,0 +1,23 @@ +video: + bitrate: 4500000 + codec: "mp4" + width: 1920 + height: 1080 + passes: 1 + maxbitrate: 2000000 + deinterlace: false + cbr: false + stretch: false + profile: "baseline" + framerate: + numerator: 30000 + denominator: 1001 + par: + numerator: 1 + denominator: 1 + keyframe_interval: 60 +audio: + codec: "aac" + bitrate: 256000 + channels: 2 + samplerate: 44100 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index be1a525..1bcdd13 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,6 @@ require 'rspec/autorun' require "vcr" - VCR.config do |c| library_dir = File.join(File.dirname(__FILE__), 'fixtures/') c.cassette_library_dir = library_dir