class MovieMasher::Asset

represents a downloadable media asset

Public Class Methods

__download_asset(asset, url, path, job) click to toggle source
# File lib/asset.rb, line 34
def self.__download_asset(asset, url, path, job)
  url = asset[:input_url].to_s
  path = url_path(url)
  unless File.exist?(path)
    begin
      FileHelper.safe_path(File.dirname(path))
      asset.download(job: job, path: path)
      raise("zero length #{url}") unless File.size?(path)
    rescue => e
      puts "CAUGHT #{e.is_a?(Error::Job)} #{e.message} #{e.backtrace}"
      raise(Error::JobInput, e.message)
    end
  end
  path
end
__populate_asset_info(asset) click to toggle source
# File lib/asset.rb, line 49
def self.__populate_asset_info(asset)
  path = asset[:cached_file]
  case asset[:type]
  when Type::VIDEO
    unless FloatUtil.nonzero(asset[:duration])
      asset[:duration] = Info.get(path, Info::DURATION).to_f
    end
    asset[:no_audio] = !Info.get(path, Info::AUDIO)
    asset[:dimensions] = Info.get(path, Info::DIMENSIONS)
    asset[:no_video] = !asset[:dimensions]
    asset[:av] = Mash.init_av_input(asset)
  when Type::AUDIO
    unless FloatUtil.nonzero(asset[:duration])
      asset[:duration] = Info.get(path, Info::AUDIO_DURATION).to_f
    end
    unless FloatUtil.gtr(asset[:duration], FloatUtil::ZERO)
      asset[:duration] = Info.get(path, Info::VIDEO_DURATION).to_f
    end
  when Type::IMAGE
    asset[:dimensions] = Info.get(path, Info::DIMENSIONS)
    unless asset[:dimensions]
      raise(Error::JobInput, 'could not determine image dimensions')
    end
  end
end
av_type(input) click to toggle source
# File lib/asset.rb, line 9
def self.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

Returns a new instance.

# File lib/asset.rb, line 6
def self.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 23
def self.download_asset(asset, job)
  path = nil
  url = asset[:input_url].to_s
  unless url.empty?
    path = __download_asset(asset, url, path, 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 74
def self.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, u) click to toggle source
# File lib/asset.rb, line 254
def __url(type, base_url, u)
  if Type::FILE == type
    Path.concat(base_url, u)
  else
    URI.join(base_url, u).to_s
  end
end
av() click to toggle source
String

The AV type.

Constant

AV::AUDIO_ONLY, AV::VIDEO_ONLY, AV::BOTH, orAV::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 changeafter probing.

# File lib/asset.rb, line 86
def av
  _get __method__
end
dimensions() click to toggle source
# File lib/asset.rb, line 89
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 95
def dimensions=(value)
  _set __method__, value
end
download(options) click to toggle source
# File lib/asset.rb, line 98
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 222
def download_source
  d_source = source
  if source && source.is_a?(Source)
    if 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
  end
  d_source
end
duration() click to toggle source
# File lib/asset.rb, line 106
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 112
def duration=(value)
  _set __method__, value
end
error?() click to toggle source
# File lib/asset.rb, line 115
def error?
  nil
end
gain() click to toggle source
# File lib/asset.rb, line 118
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 andmultiplier 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 inover the first 10% of the element's length and out over the last10%.

Default

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

Types

Type::AUDIO and Type::VIDEO.

# File lib/asset.rb, line 129
def gain=(value)
  _set __method__, value
end
length() click to toggle source
# File lib/asset.rb, line 132
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 137
def length=(value)
  _set __method__, value
end
loop() click to toggle source
# File lib/asset.rb, line 140
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 145
def loop=(value)
  _set __method__, value
end
metadata() click to toggle source
# File lib/asset.rb, line 148
def metadata
  (self[:cached_file] ? MetaReader.new(self[:cached_file]) : {})
end
no_audio() click to toggle source
# File lib/asset.rb, line 151
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 157
def no_audio=(value)
  _set __method__, value
end
no_video() click to toggle source
# File lib/asset.rb, line 160
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 166
def no_video=(value)
  _set __method__, value
end
offset() click to toggle source
# File lib/asset.rb, line 169
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 175
def offset=(value)
  _set __method__, value
end
preflight(job = nil) click to toggle source
# File lib/asset.rb, line 178
def preflight(job = nil)
  if source
    self.source = Source.create_if source
    if job
      self[:base_source] = job.base_source unless base_source
      self[:module_source] = job.module_source unless module_source
      # puts "base_source: #{base_source}"
      unless Type::MASH == type && @hash[:mash]
        self[:input_url] = url(base_source, module_source)
      end
    end
  end
  # puts "preflight #{self.class.name} URL: #{self[:input_url]}"
end
source() click to toggle source
# File lib/asset.rb, line 192
def source
  s = _get __method__
  s
end
source=(value) click to toggle source

Describes the download request for the element, as either a URL orHash/Source. If the URL is relative it's based fromJob#base_source. Assets of Type::MASH can point to anything thatresponds with a JSON formatted mash. After download they will passthe parsed Hash to MovieMasher::Mash.new and reset their source to thereturned instance. Alternatively, source can be initially set toa 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::MASHAssets, a JSON formatted Mash. The former is sent to Source.createwhile the later is sent to MovieMasher::Mash.new.

Returns

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

# File lib/asset.rb, line 210
def source=(value)
  _set __method__, value
end
type() click to toggle source
# File lib/asset.rb, line 213
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 219
def type=(value)
  _set __method__, value
end
url(base_src = nil, module_src = nil) click to toggle source
# File lib/asset.rb, line 237
def url(base_src = nil, module_src = nil)
  u = nil
  if source.is_a?(Source)
    u = 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
        u = Path.strip_slash_start(u)
        u = __url(base_src[:type], base_url, u)
      end
    end
  end
  # puts "#{self.class.name}#url URL: #{u}"
  u
end