lib/uencode/elements.rb in uencode-0.0.3 vs lib/uencode/elements.rb in uencode-3.0.0

- old
+ new

@@ -1,33 +1,38 @@ +# Copyright (c) 2011, uEncode, Cassio Marques + module UEncode - class Crop - ATTRIBUTES = [:width, :height, :x, :y] - def to_xml - %Q{ - <crop> - <width>#{width}</width> - <height>#{height}</height> - <x>#{x}</x> - <y>#{y}</y> - </crop> - } + module SizeBase + ATTRIBUTES = [:width, :height] + + include AttrSetting + + def to_xml + %Q{ + <#{root_name}> + <width>#{width}</width> + <height>#{height}</height> + </#{root_name}> + } + end end - end + class Size - ATTRIBUTES = [:width, :height] - - def to_xml - %Q{ - <size> - <width>#{width}</width> - <height>#{height}</heignt> - </size> - } - end + include UEncode::SizeBase + + private + def root_name; "size"; end end + + class MaxSize + include UEncode::SizeBase + + private + def root_name; "max_size"; end + end module RateElement ATTRIBUTES = [:numerator, :denominator] include AttrSetting @@ -44,112 +49,151 @@ def ==(other) numerator == other.numerator && denominator == other.denominator end end - class FrameRate - include RateElement + module Transfer + ATTRIBUTES = [:url, :authentication_type, :timeout, :public_read] + + include AttrSetting + + def to_xml + %Q{ + <#{root_name}#{@authentication_type ? ' authentication_type="' + @authentication_type + '"' : ""}#{@timeout ? ' timeout="'+ @timeout.to_s + '"' : ""}#{@public_read ? ' public_read="' + @public_read.to_s + '"' : ""}>#{@url}</#{root_name}> + } + end + end + class Destination + include Transfer + private - def root_name; "framerate"; end + def root_name; "destination"; end end - - class Par < FrameRate - include RateElement - + + class Source + include Transfer + private - def root_name; "par"; end + def root_name; "source"; end end class CaptureOutput - ATTRIBUTES = [:destination, :rate, :stretch, :crop, :size] + ATTRIBUTES = [:rate, :stretch, :crop, :zip, :prefix] def initialize(options) + @destinations = [] super - @stretch = false if @stretch.nil? end + def add_destination(dest) + @destinations << dest + end + + def size=(_size) + _size = Size.new(_size) unless _size.instance_of?(Size) || _size.nil? + instance_variable_set :@size, _size + end + + def size + @size + end + + def max_size=(_max_size) + _max_size = MaxSize.new(_max_size) unless _max_size.instance_of?(MaxSize) || _max_size.nil? + instance_variable_set :@max_size, _max_size + end + + def max_size + @max_size + end + + def to_xml %Q{ - <output> - <capture> + <capture> <rate>#{rate}</rate> - <destination>#{destination}</destination> - #{@crop ? @crop.to_xml : ""} + #{!zip.nil? ? '<zip>' + zip.to_s + '</zip>' : ""} + #{!prefix.nil? ? '<prefix>' + prefix.to_s + '</prefix>' : ""} + <destinations> + #{@destinations.inject("") { |s, dest| s << dest.to_xml}} + </destinations> #{@size ? @size.to_xml : ""} + #{@max_size ? @max_size.to_xml : ""} </capture> - <//output> } end end class VideoOutput ATTRIBUTES = [:destination, :container] include Enumerable - # a list of Medium - attr_reader :items - attr_writer :destination, :container + attr_writer :container + attr_accessor :streams def initialize(options) - @items = [] + @streams = VideoStreams.new + @destinations = [] super end def each @items.each { |item| yield item } end + + def add_destination(dest) + @destinations << dest + end def to_xml %Q{ - <output> <video> - <destination>#{destination}</destination> + <destinations> + #{@destinations.inject("") { |s, dest| s << dest.to_xml}} + </destinations> <container>#{container}</container> - <media> - #{@items.inject("") { |s, item| s << item.to_xml }} - </media> + #{@streams.to_xml} </video> - </output> } end end - # Medium is a single video to transcode - class Medium + # VideoStreams represents the audio and video streams for a single output + class VideoStreams 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" => { ... } + # 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: + # 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) } + video = hash[:video] + audio = hash[:audio] + if !video.nil? + configure_video do |c| + video.each_pair { |key, value| c.send("#{key}=", value) } + end end - configure_audio do |c| - audio.each_pair { |key, value| c.send("#{key}=", value) } + if !audio.nil? + configure_audio do |c| + audio.each_pair { |key, value| c.send("#{key}=", value) } + end end end def configure_video yield @video_config @@ -167,116 +211,116 @@ @audio_config end def to_xml %Q{ - <medium> <video> - <bitrate>#{video.bitrate}</birate> - <codec>#{video.codec}</birate> - #{!video.cbr.nil? ? '<cbr>' + video.cbr.to_s + '</cbr>' : ""} - #{video.crop ? video.crop.to_xml : ""} - #{video.deinterlace.nil? ? "" : '<deinterlace>' + video.deinterlace.to_s + '</deinterlace>'} - #{video.framerate ? video.framerate.to_xml : ""} - #{video.height.nil? ? "" : '<height>' + video.height.to_s + '</height>'} - #{video.keyframe_interval.nil? ? "" : '<keyframe_interval>' + video.keyframe_interval.to_s + '</keyframe_interval>'} - #{video.maxbitrate.nil? ? "" : '<maxbitrate>' + video.maxbitrate.to_s + '</maxbitrate>'} - #{video.par ? video.par.to_xml : ""} - #{video.profile.nil? ? "" : '<profile>' + video.profile + '</profile>'} + #{!video.bitrate.nil? ? '<bitrate>' + video.bitrate.to_s + '</bitrate>' : ""} + #{!video.codec.nil? ? '<codec>' + video.codec + '</codec>' : ""} + #{!video.profile.nil? ? '<profile>' + video.profile + '</profile>' : ""} + #{!video.quality.nil? ? '<quality>' + video.quality.to_s + '</quality>' : ""} + #{!video.framerate.nil? ? '<framerate>' + video.framerate.to_s + '</framerate>' : ""} + #{!@video_config.size.nil? ? @video_config.size.to_xml : ""} + #{@video_config.max_size ? @video_config.max_size.to_xml : ""} #{video.passes.nil? ? "" : '<passes>' + video.passes.to_s + '</passes>'} - #{[nil, false].include?(video.stretch) ? "" : '<stretch>' + video.stretch.to_s + '</stretch>'} - #{video.width.nil? ? "" : '<width>' + video.width.to_s + '</width>'} + #{video.deinterlace.nil? ? "" : '<deinterlace>' + video.deinterlace.to_s + '</deinterlace>'} + #{video.fix_rotation.nil? ? "" : '<fix_rotation>' + video.fix_rotation.to_s + '</fix_rotation>'} + #{video.force_square_pixels.nil? ? "" : '<force_square_pixels>' + video.force_square_pixels.to_s + '</force_square_pixels>'} </video> <audio> #{audio.codec.nil? ? "" : '<codec>' + audio.codec + '</codec>'} #{audio.bitrate.nil? ? "" : '<bitrate>' + audio.bitrate.to_s + '</bitrate>'} #{audio.channels.nil? ? "" : '<channels>' + audio.channels.to_s + '</channels>'} #{audio.samplerate.nil? ? "" : '<samplerate>' + audio.samplerate.to_s + '</samplerate>'} + #{audio.quality.nil? ? "" : '<quality>' + video.quality.to_s + '</quality>'} </audio> - </medium> } end end - # The video configs for each Medium + # The video configs for each VideoStream class VideoConfig - attr_accessor :bitrate, :codec, :cbr, :crop, :deinterlace, :framerate, :height, :keyframe_interval, - :maxbitrate, :par, :profile, :passes, :stretch, :width + attr_accessor :bitrate, :codec, :profile, :quality, :framerate, :passes, :deinterlace, :fix_rotation, :force_square_pixels def initialize - @cbr = false @deinterlace = false @profile = "main" @passes = 1 - @stretch = false end - def framerate=(_framerate) - _framerate = FrameRate.new(_framerate) unless _framerate.instance_of?(FrameRate) || _framerate.nil? - instance_variable_set :@framerate, _framerate + def size=(_size) + _size = Size.new(_size) unless _size.instance_of?(Size) || _size.nil? + instance_variable_set :@size, _size end - - def par=(_par) - _par = Par.new(_par) unless _par.instance_of?(Par) || _par.nil? - instance_variable_set :@par, _par + + def size + @size end + + def max_size=(_max_size) + _max_size = MaxSize.new(_max_size) unless _max_size.instance_of?(MaxSize) || _max_size.nil? + instance_variable_set :@max_size, _max_size + end + + def max_size + @max_size + end end - # The audio configs for each Medium + # The audio configs for each VideoStream class AudioConfig - attr_accessor :codec, :bitrate, :channels, :samplerate + attr_accessor :codec, :bitrate, :channels, :samplerate, :quality end + class JobStatus + ATTRIBUTES = [:key, :userdata, :customerkey] + + include AttrSetting + end + class Job - ATTRIBUTES = [:source, :userdata, :notify] + ATTRIBUTES = [:userdata, :callback, :customerkey] - include Enumerable + include AttrSetting - def self.from_hash(hash) - new({}) + def source_video=(_source) + _source = Source.new(_source) unless _source.instance_of?(Source) || _source.nil? + instance_variable_set :@source_video, _source end + + def source_video + @source_video + end def initialize(options) - @video_output = VideoOutput.new options[:video_output] || {} + @video_outputs = [] @captures = [] + source_video = options[:source_video] super end - def configure_video_output - yield @video_output + def add_video(video) + @video_outputs << video end - def items - @video_output.items - end - - def <<(item) - @video_output.items << item - end - def add_capture(capture) @captures << capture end - def each(&block) - @video_output.each &block - end - def to_xml xml = %Q{ <job> - <customerkey>#{UEncode.customer_key}</customerkey> - <source>#{source}</source> + #{source_video.nil? ? "" : source_video.to_xml} #{userdata.nil? ? "" : '<userdata>' + userdata + '</userdata>'} - #{notify.nil? ? "" : '<notify>' + notify + '</notify>'} + #{callback.nil? ? "" : '<callback>' + callback + '</callback>'} <outputs> - #{@video_output.to_xml} + #{@video_outputs.inject("") { |s, vid| s << vid.to_xml}} #{@captures.inject("") { |s, cap| s << cap.to_xml }} </outputs> </job> } - Nokogiri::XML(xml).to_xml + xml.gsub(/\n?/, "").gsub(/>\s+</, "><") end end - [Size, Crop, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting } + [SizeBase, Size, MaxSize, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting } end