cirandas.net

ref: master

plugins/html5_video/lib/presenters/video.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class FilePresenter::Video < FilePresenter

  def self.accepts?(f)
    return nil if !f.respond_to?(:content_type) || f.content_type.nil?
    ( f.content_type[0..4] == 'video' ) ? 10 : nil
  end

  def short_description
    _('Video (%s)') % content_type.split('/')[1].upcase
  end

  def meta_data #video_info
    Noosfero::Plugin::Settings.new(encapsulated_instance, Html5VideoPlugin)
  end

  def original_video
    meta_data.original_video ||= {}
  end

  def original_video=(hash)
    meta_data.original_video = hash
  end

  def web_versions
    meta_data.web_versions ||= {}
  end

  def web_versions=(hash)
    meta_data.web_versions = hash
  end

  # adds the orig version tho the web_versions if that is a valid to HTML5
  def web_versions!
    list = web_versions.clone
    streams = original_video.empty? ? [] : original_video[:streams]
    video_stream = streams.find{|s| s[:type] == 'video' }
    audio_stream = streams.find{|s| s[:type] == 'audio' }
    return list unless video_stream && audio_stream
    type = original_video[:type].to_s.upcase.to_sym
    type = :OGV if video_stream[:codec]=='theora' && original_video[:type]=='ogg'
    if [:OGV, :MP4, :WEBM].include? type
      vb = video_stream[:bitrate] || original_video[:global_bitrate] || 0
      ab = audio_stream[:bitrate] || 0
      info = {
        :original => true,
        :file_name => File.basename(public_filename),
        :abrate => ab,
        :vbrate => vb,
        :size => video_stream[:size],
        :size_name => 'orig',
        :status => 'done',
        :type => type,
        :path => public_filename
      }
      list[type][:orig] = info
    end
    list
  end

  def ready_web_versions
    ready = {}
    web_versions!.select do |type, type_block|
      ready[type] = {}
      type_block.select do |size, size_block|
        ready[type][size] = size_block if size_block[:status] == 'done'
      end
    end
    ready
  end

  def has_ogv_version
    not ready_web_versions[:OGV].blank?
  end

  def has_mp4_version
    not ready_web_versions[:MP4].blank?
  end

  def has_webm_version
    not ready_web_versions[:WEBM].blank?
  end

  def has_web_version
    ready = ready_web_versions
    not (ready[:OGV].blank? and ready[:MP4].blank? and ready[:WEBM].blank?)
  end

  def tiniest_web_version( type )
    return nil if ready_web_versions[type].nil?
    video = ready_web_versions[type].
            select{|size,data| data[:status] == 'done' }.
            sort_by{|v| v[1][:vbrate] }.first
    video ? video[1] : nil
  end

  #TODO: add this to the user interface:
  def web_version_jobs
    #FIXME: in a newer version, the Delayed::Job may be searcheable in a uglyless way.
    Delayed::Job.where("handler LIKE '%CreateVideoForWebJob%file_id: #{self.id}%'").all
    #Delayed::Job.all :conditions => ['handler LIKE ?',
    #                                 "%CreateVideoForWebJob%file_id: #{self.id}%"]
  end

  def web_preview_jobs
    #FIXME: in a newer version, the Delayed::Job may be searcheable in a uglyless way.
    Delayed::Job.where("handler LIKE '%CreateVideoPreviewJob%file_id: #{self.id}%'").all
    #Delayed::Job.all :conditions => ['handler LIKE ?',
    #                                 "%CreateVideoPreviewJob%file_id: #{self.id}%"]
  end

  def has_previews?
    not(previews.nil?) && previews.kind_of?(Hash) && !previews.empty?
  end

  def previews
    meta_data.image_previews
  end

  def previews=(hash)
    meta_data.image_previews = hash
  end

  def image_preview(size=nil)
    if has_previews? && previews[size]
      File.dirname( public_filename ) + previews[size]
    else
      "/plugins/html5_video/images/video-preview-#{size}.png"
    end
  end

  def conversion_errors
    errors = {}
    web_versions!.select do |type, type_block|
      type_block.select do |size, conv_info|
        if conv_info[:status] == 'error converting'
          errors[type] ||= {}
          err_base = {:message=>_('Undefined'), :code=>-2, :output=>'undefined'}
          errors[type][size] = err_base.merge( conv_info[:error] || {} )
        end
      end
    end
    errors
  end

end