Class: Falcon::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/falcon/media.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, instance, options = {}) ⇒ Media

Returns a new instance of Media.



16
17
18
19
20
21
22
23
# File 'lib/falcon/media.rb', line 16

def initialize(name, instance, options = {})
  @name = name
  @instance = instance
  @options = self.class.default_options.merge(options)
  @profiles = @options[:profiles]
  @encode = @options[:encode]
  @dirty = false
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



14
15
16
# File 'lib/falcon/media.rb', line 14

def instance
  @instance
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/falcon/media.rb', line 14

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/falcon/media.rb', line 14

def options
  @options
end

Class Method Details

.default_optionsObject



5
6
7
8
9
10
11
12
# File 'lib/falcon/media.rb', line 5

def self.default_options
  @default_options ||= {
    :profiles => ['web_mp4', 'web_ogg'],
    :metadata => {},
    :source => nil,
    :encode => nil
  }
end

Instance Method Details

#all_ready?Boolean

Check if source encoded by all profiles

Returns:

  • (Boolean)


101
102
103
# File 'lib/falcon/media.rb', line 101

def all_ready?
  instance.falcon_encodings.success.count == profiles.keys.size
end

#assign(source) ⇒ Object



109
110
111
112
113
114
# File 'lib/falcon/media.rb', line 109

def assign(source)
  if File.exist?(source)
    @source_path = source
    @dirty = true
  end
end

#destroyObject

Destroy files end encodings



89
90
91
92
93
# File 'lib/falcon/media.rb', line 89

def destroy
  flush_deletes
  @dirty = false
  true
end

#dirty?Boolean

Returns true if there are changes that need to be saved.

Returns:

  • (Boolean)


67
68
69
# File 'lib/falcon/media.rb', line 67

def dirty?
  @dirty
end

#exist?Boolean

Check if source file exists

Returns:

  • (Boolean)


96
97
98
# File 'lib/falcon/media.rb', line 96

def exist?
  File.exist?(source_path)
end

#metadataObject

A hash of metadatas for video:

{ :title => ”, :author => ”, :copyright => ”,

:comment => '', :description => '', :language => ''}


47
48
49
50
51
52
# File 'lib/falcon/media.rb', line 47

def 
  @metadata ||= begin
    method = @options[:metadata]
    method.respond_to?(:call) ? method.call(self) : instance.send(method)
  end
end

#output_directoryObject



62
63
64
# File 'lib/falcon/media.rb', line 62

def output_directory
  @output_directory ||= File.dirname(source_path)
end

#path(profile) ⇒ Object

Returns the path of the generated media file by profile object or profile name



72
73
74
# File 'lib/falcon/media.rb', line 72

def path(profile)
  Falcon::Profile.detect(profile).path(source_path, name)
end

#profilesObject

Array of processing profiles



26
27
28
29
30
31
32
33
34
35
# File 'lib/falcon/media.rb', line 26

def profiles
  unless @normalized_profiles
    @normalized_profiles = {}
    (@profiles.respond_to?(:call) ? @profiles.call(self) : @profiles).each do |name|
      @normalized_profiles[name] = Falcon::Profile.find(name)
    end
  end
  
  @normalized_profiles
end

#ready?(profile) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/falcon/media.rb', line 105

def ready?(profile)
  instance.falcon_encodings.with_profile(profile).success.exists?
end

#saveObject



81
82
83
84
85
86
# File 'lib/falcon/media.rb', line 81

def save
  flush_deletes
  create_encodings
  @dirty = false
  true
end

#screenshots(&block) ⇒ Object

Yield generated screenshots and remove them



117
118
119
120
121
122
# File 'lib/falcon/media.rb', line 117

def screenshots(&block)
  Dir.glob("#{output_directory}/*.{jpg,JPG}").each do |filepath|
   yield filepath
   FileUtils.rm(filepath, :force => true)
 end
end

#source_pathObject

Path for media source file



55
56
57
58
59
60
# File 'lib/falcon/media.rb', line 55

def source_path
  @source_path ||= begin
    method = options[:source]
    method.respond_to?(:call) ? method.call(instance) : instance.send(method)
  end
end

#sourcesObject

List of generated video files



38
39
40
# File 'lib/falcon/media.rb', line 38

def sources
  @sources ||= profiles.values.map{|profile| url(profile) }
end

#url(profile) ⇒ Object

Returns the public URL of the media, with a given profile



77
78
79
# File 'lib/falcon/media.rb', line 77

def url(profile)
  "/" + path(profile).relative_path_from( Rails.root.join('public') )
end