# Copyright (c) 2011, uEncode, Cassio Marques
module UEncode
module SizeBase
ATTRIBUTES = [:width, :height]
include AttrSetting
def to_xml
%Q{
<#{root_name}>
#{width}#{height}
#{root_name}>
}
end
end
class Size
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
def to_xml
%Q{
<#{root_name}>
#{numerator}#{denominator}
#{root_name}>
}
end
def ==(other)
numerator == other.numerator && denominator == other.denominator
end
end
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; "destination"; end
end
class Source
include Transfer
private
def root_name; "source"; end
end
class CaptureOutput
ATTRIBUTES = [:rate, :stretch, :crop, :zip, :prefix]
def initialize(options)
@destinations = []
super
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{
#{rate}
#{!zip.nil? ? '' + zip.to_s + '' : ""}
#{!prefix.nil? ? '' + prefix.to_s + '' : ""}
#{@destinations.inject("") { |s, dest| s << dest.to_xml}}
#{@size ? @size.to_xml : ""}
#{@max_size ? @max_size.to_xml : ""}
}
end
end
class VideoOutput
ATTRIBUTES = [:destination, :container]
include Enumerable
attr_writer :container
attr_accessor :streams
def initialize(options)
@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{
}
end
end
# 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 => { ... }
#
# 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 keys for the :audio hash can be any of the following:
# codec, bitrate, channels, samplerate.
#
def configure(hash)
video = hash[:video]
audio = hash[:audio]
if !video.nil?
configure_video do |c|
video.each_pair { |key, value| c.send("#{key}=", value) }
end
end
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
end
def configure_audio
yield @audio_config
end
def video
@video_config
end
def audio
@audio_config
end
def to_xml
%Q{
}
end
end
# The video configs for each VideoStream
class VideoConfig
attr_accessor :bitrate, :codec, :profile, :quality, :framerate, :passes, :deinterlace, :fix_rotation, :force_square_pixels
def initialize
@deinterlace = false
@profile = "main"
@passes = 1
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
end
# The audio configs for each VideoStream
class AudioConfig
attr_accessor :codec, :bitrate, :channels, :samplerate, :quality
end
class JobStatus
ATTRIBUTES = [:key, :userdata, :customerkey]
include AttrSetting
end
class Job
ATTRIBUTES = [:userdata, :callback, :customerkey]
include AttrSetting
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_outputs = []
@captures = []
source_video = options[:source_video]
super
end
def add_video(video)
@video_outputs << video
end
def add_capture(capture)
@captures << capture
end
def to_xml
xml = %Q{
#{source_video.nil? ? "" : source_video.to_xml}
#{userdata.nil? ? "" : '' + userdata + ''}
#{callback.nil? ? "" : '' + callback + ''}
#{@video_outputs.inject("") { |s, vid| s << vid.to_xml}}
#{@captures.inject("") { |s, cap| s << cap.to_xml }}
}
xml.gsub(/\n?/, "").gsub(/>\s+, "><")
end
end
[SizeBase, Size, MaxSize, VideoOutput, CaptureOutput, Job].each { |klass| klass.send :include, AttrSetting }
end