class MovieMasher::Asset

represents a downloadable media asset

Public Class Methods

av_type(input) click to toggle source

Returns

Returns a new instance.

# File lib/asset.rb, line 8
def av_type(input)
  case input[:type]
  when Type::AUDIO
    AV::AUDIO_ONLY
  when Type::IMAGE, Type::FONT, Type::FRAME
    AV::VIDEO_ONLY
  when Type::VIDEO, Type::MASH
    if input[:no_audio]
      AV::VIDEO_ONLY
    else
      (input[:no_video] ? AV::AUDIO_ONLY : AV::BOTH)
    end
  end
end
create(hash = nil) click to toggle source
# File lib/asset.rb, line 23
def create(hash = nil)
  (hash.is_a?(Asset) ? hash : Asset.new(hash))
end
download_asset(asset, job) click to toggle source
# File lib/asset.rb, line 27
def download_asset(asset, job)
  path = nil
  url = asset[:input_url].to_s
  unless url.empty?
    path = __download_asset(asset, url, job)
    Info.set(path, Info::AT, Time.now.to_i)
    asset[:cached_file] = path
    __populate_asset_info(asset)
  end
  path
end
url_path(url) click to toggle source
# File lib/asset.rb, line 39
def url_path(url)
  dir = MovieMasher.configuration[:download_directory]
  dir = MovieMasher.configuration[:render_directory] if dir.to_s.empty?
  hex = Digest::SHA2.new(256).hexdigest(url)
  Path.concat(dir, "#{hex}/#{Info::DOWNLOADED}#{File.extname(url)}")
end

Public Instance Methods

__url(type, base_url, path) click to toggle source
# File lib/asset.rb, line 288
def __url(type, base_url, path)
  if Type::FILE == type
    Path.concat(base_url, path)
  else
    URI.join(base_url, path).to_s
  end
end
av() click to toggle source
String

The AV type.

Constant

AV::AUDIO_ONLY, AV::VIDEO_ONLY, AV::BOTH, or AV::NEITHER if an error was encountered while probing.

Types

All, but Type::MASH reflects its nested elements.

Default

Initially based on type and no_audio, but might change after probing.

# File lib/asset.rb, line 96
def av
  _get __method__
end
dimensions() click to toggle source
# File lib/asset.rb, line 100
def dimensions
  _get __method__
end
dimensions=(value) click to toggle source
String

WIDTHxHEIGHT of element.

Default

Probed from downloaded.

Types

Type::IMAGE and Type::VIDEO.

# File lib/asset.rb, line 107
def dimensions=(value)
  _set __method__, value
end
download(options) click to toggle source
# File lib/asset.rb, line 111
def download(options)
  d_source = download_source
  service = Service.downloader(d_source.type)
  raise(Error::Configuration, "no service #{d_source.type}") unless service

  options[:source] = d_source
  options[:asset] = self
  service.download(options)
end
download_source() click to toggle source
# File lib/asset.rb, line 121
def download_source
  d_source = source
  if source.is_a?(Source) && source&.relative?
    # relative url
    case type
    when Type::THEME, Type::FONT, Type::EFFECT
      d_source = module_source || base_source || source
    else
      d_source = base_source || source
    end
  end
  d_source
end
duration() click to toggle source
# File lib/asset.rb, line 135
def duration
  _get __method__
end
duration=(value) click to toggle source
Float

Seconds of Asset available for presentation.

Default

Probed from downloaded.

Types

All except Type::IMAGE.

# File lib/asset.rb, line 142
def duration=(value)
  _set __method__, value
end
error?() click to toggle source
# File lib/asset.rb, line 146
def error?
  nil
end
gain() click to toggle source
# File lib/asset.rb, line 150
def gain
  _get __method__
end
gain=(value) click to toggle source
Float

Multiplier to adjust volume of audio when mixed into mashup.

Array

Duple Float values signifying element offset and multiplier for arbitrary volume fading over time. For instance, [0.0, 0.0, 0.1, 1.0, 0.9, 1.0, 1.0, 0.0] would fade volume in over the first 10% of the element's length and out over the last 10%.

Default

Gain::None (1.0) means no change in volume.

Types

Type::AUDIO and Type::VIDEO.

# File lib/asset.rb, line 162
def gain=(value)
  _set __method__, value
end
length() click to toggle source
# File lib/asset.rb, line 166
def length
  _get __method__
end
length=(value) click to toggle source
Float

Seconds the Asset appears in the mashup.

Default

duration - offset

# File lib/asset.rb, line 172
def length=(value)
  _set __method__, value
end
loop() click to toggle source
# File lib/asset.rb, line 176
def loop
  _get __method__
end
loop=(value) click to toggle source
Integer

Number of times to loop Asset.

Types

Just Type::AUDIO.

# File lib/asset.rb, line 182
def loop=(value)
  _set __method__, value
end
metadata() click to toggle source
# File lib/asset.rb, line 186
def metadata
  (self[:cached_file] ? MetaReader.new(self[:cached_file]) : {})
end
no_audio() click to toggle source
# File lib/asset.rb, line 190
def no_audio
  _get __method__
end
no_audio=(value) click to toggle source
Boolean

If true, audio in Asset will be ignored.

Default

Initially based on type, but could change after probing.

Types

Type::MASH and Type::VIDEO, but accessible for others.

# File lib/asset.rb, line 197
def no_audio=(value)
  _set __method__, value
end
no_video() click to toggle source
# File lib/asset.rb, line 201
def no_video
  _get __method__
end
no_video=(value) click to toggle source
Boolean

If true, video in Asset will be ignored.

Default

Initially based on type, but could change after probing.

Types

Type::MASH and Type::VIDEO, but accessible for others.

# File lib/asset.rb, line 208
def no_video=(value)
  _set __method__, value
end
offset() click to toggle source
# File lib/asset.rb, line 212
def offset
  _get __method__
end
offset=(value) click to toggle source
Float

Seconds to remove from beginning of Asset.

Default

0.0 means nothing removed.

Types

Type::AUDIO and Type::VIDEO.

# File lib/asset.rb, line 219
def offset=(value)
  _set __method__, value
end
preflight(job = nil) click to toggle source
# File lib/asset.rb, line 223
def preflight(job = nil)
  return unless source

  self.source = Source.create_if source
  return unless job

  self[:base_source] = job.base_source unless base_source
  self[:module_source] = job.module_source unless module_source
  # puts "base_source: #{base_source}"
  return if Type::MASH == type && @hash[:mash]

  self[:input_url] = url(base_source, module_source)
  # puts "preflight #{self.class.name} URL: #{self[:input_url]}"
end
source() click to toggle source
# File lib/asset.rb, line 238
def source
  _get __method__
end
source=(value) click to toggle source

Describes the download request for the element, as either a URL or Hash/Source. If the URL is relative it's based from Job#base_source. Assets of Type::MASH can point to anything that responds with a JSON formatted mash. After download they will pass the parsed Hash to Mash.new and reset their source to the returned instance. Alternatively, source can be initially set to a Hash/Mash so as to avoid download.

String

A HTTP or HTTPS URL to element, converted to appropriate Source.

Hash

Can describe either a download request or, for Type::MASH Assets, a JSON formatted Mash. The former is sent to Source.create while the later is sent to Mash.new.

Returns

A Source object or, for Type::MASH Assets after downloading, a Mash object.

# File lib/asset.rb, line 256
def source=(value)
  _set __method__, value
end
type() click to toggle source
# File lib/asset.rb, line 260
def type
  _get __method__
end
type=(value) click to toggle source
String

The kind of Asset.

Constant

Type::AUDIO, Type::IMAGE, Type::MASH or Type::VIDEO.

Default

Probed from downloaded.

# File lib/asset.rb, line 267
def type=(value)
  _set __method__, value
end
url(base_src = nil, module_src = nil) click to toggle source
# File lib/asset.rb, line 271
def url(base_src = nil, module_src = nil)
  return unless source.is_a?(Source)

  source_url = source.url
  if source.relative?
    base_src = module_src if module_src && Type::MODULES.include?(type)
    if base_src
      base_url = base_src.url
      base_url = Path.add_slash_end base_url
      source_url = Path.strip_slash_start(source_url)
      source_url = __url(base_src[:type], base_url, source_url)
    end
  end
  # puts "#{self.class.name}#url URL: #{source_url}"
  source_url
end