Sha256: 852680fc34b2bd9d4c5992692e51738ca11685660819df54f634469ff12fd268

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module ActiveStorage
  # Extracts the following from a video blob:
  #
  # * Width (pixels)
  # * Height (pixels)
  # * Duration (seconds)
  # * Aspect ratio
  #
  # Example:
  #
  #   ActiveStorage::Analyzer::QiniuVideoAnalyzer.new(blob).metadata
  #   # => {:width=>240, :height=>240, :duration=>"2.000000", :aspect_ratio=>"1:1"}
  #
  class Analyzer::QiniuVideoAnalyzer < Analyzer
    def self.accept?(blob)
      blob.video?
    end

    def metadata
      {width: width, height: height, duration: duration, aspect_ratio: aspect_ratio}.compact
    rescue
      {}
    end

    private

    def width
      video_stream['width']
    end

    def hegiht
      video_stream['height']
    end

    def duration
      video_stream['duration']
    end

    def aspect_ratio
      video_stream['display_aspect_ratio']
    end

    def streams
      @streams ||= begin
        code, result, res = Qiniu::HTTP.api_get(blob.service.url(blob.key, fop: 'avinfo'))
        result['streams']
      end
    end

    def video_stream
      @video_stream ||= streams.detect { |stream| stream["codec_type"] == "video" } || {}
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activestorage_qiniu-0.1.7 lib/active_storage/analyzer/qiniu_video_analyzer.rb
activestorage_qiniu-0.1.6 lib/active_storage/analyzer/qiniu_video_analyzer.rb
activestorage_qiniu-0.1.2 lib/active_storage/analyzer/qiniu_video_analyzer.rb
activestorage_qiniu-0.1.1 lib/active_storage/analyzer/qiniu_video_analyzer.rb