# Copyright (c) 2011, uEncode, Cássio 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 OverlayUrl
include Transfer
private
def root_name; "url"; end
end
class Position
ATTRIBUTES = [:x, :y]
include AttrSetting
def to_xml
%Q{
<#{x.nil? ? '' + x.to_s + '' : ""}
<#{y.nil? ? '' + y.to_s + '' : ""}
}
end
end
class Overlay
ATTRIBUTES = [:quadrant]
include AttrSetting
def position=(_position)
_position = Position.new(_position) unless _position.instance_of?(Position) || _position.nil?
instance_variable_set :@position, _position
end
def position
@position
end
def source_image=(_source)
_source = OverlayUrl.new(_source) unless _source.instance_of?(OverlayUrl) || _source.nil?
instance_variable_set :@source_image, _source
end
def source_image
@source_image
end
def to_xml
%Q{
#{!source_image.nil? ? source_image.to_xml : ""}
#{!@position.nil? ? @position.to_xml : ""}
#{!quadrant.nil? ? '' + quadrant.to_s + '' : ""}
}
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 overlay=(_overlay)
_overlay = Overlay.new(_overlay) unless _overlay.instance_of?(Overlay) || _overlay.nil?
instance_variable_set :@overlay, _overlay
end
def overlay
@overlay
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 : ""}
#{@overlay ? @overlay.to_xml : ""}
}
end
end
class Metadata
ATTRIBUTES = [:title, :author, :composer, :album, :year, :track, :comment, :genre, :copyright, :description, :synopsis, :show, :episode_id, :network]
include AttrSetting
def to_xml
%Q{
#{!title.nil? ? '
' + title.to_s + '' : ""}
#{!author.nil? ? '' + author.to_s + '' : ""}
#{!composer.nil? ? '' + composer.to_s + '' : ""}
#{!album.nil? ? '' + album.to_s + '' : ""}
#{!year.nil? ? '' + year.to_s + '' : ""}
#{!track.nil? ? '' : ""}
#{!comment.nil? ? '' + comment.to_s + '' : ""}
#{!genre.nil? ? '' + genre.to_s + '' : ""}
#{!copyright.nil? ? '' + copyright.to_s + '' : ""}
#{!description.nil? ? '' + description.to_s + '' : ""}
#{!synopsis.nil? ? '' + synopsis.to_s + '' : ""}
#{!show.nil? ? '' + show.to_s + '' : ""}
#{!episode_id.nil? ? '' + episode_id.to_s + '' : ""}
#{!network.nil? ? '' + network.to_s + '' : ""}
}
end
end
class Clip
ATTRIBUTES = [:start, :duration]
include AttrSetting
def to_xml
%Q{
#{!start.nil? ? '' + start.to_s + '' : ""}
#{!duration.nil? ? '' + duration.to_s + '' : ""}
}
end
end
class VideoOutput
ATTRIBUTES = [:destination, :container]
include AttrSetting
include Enumerable
attr_writer :container
attr_accessor :streams
def overlay=(_overlay)
_overlay = Overlay.new(_overlay) unless _overlay.instance_of?(Overlay) || _overlay.nil?
instance_variable_set :@overlay, _overlay
end
def overlay
@overlay
end
def clip=(_clip)
_clip = Clip.new(_clip) unless _clip.instance_of?(Clip) || _clip.nil?
instance_variable_set :@clip, _clip
end
def clip
@clip
end
def metadata=(_metadata)
_metadata = Metadata.new(_metadata) unless _metadata.instance_of?(Metadata) || _metadata.nil?
instance_variable_set :@metadata, _metadata
end
def metadata
@metadata
end
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)
vid = hash[:video]
aud = hash[:audio]
if !vid.nil?
configure_video do |c|
vid.each_pair { |key, value| c.send("#{key}=", value)}
end
end
if !aud.nil?
configure_audio do |c|
aud.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 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