Sha256: b0a174d1bdd874e9d6b14f6670a92f6b170bcf0ab63d374f015cb91688ac5a5b

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/hash/compact"

module ActiveStorage
  # Extracts the following from a video blob:
  #
  # * Width (pixels)
  # * Height (pixels)
  # * Duration (seconds)
  # * Angle (degrees)
  # * Aspect ratio
  #
  # Example:
  #
  #   ActiveStorage::VideoAnalyzer.new(blob).metadata
  #   # => { width: 640, height: 480, duration: 5.0, angle: 0, aspect_ratio: [4, 3] }
  #
  # This analyzer requires the {ffmpeg}[https://www.ffmpeg.org] system library, which is not provided by Rails. You must
  # install ffmpeg yourself to use this analyzer.
  class Analyzer::VideoAnalyzer < Analyzer
    def self.accept?(blob)
      blob.video?
    end

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

    private
      def width
        Integer(video_stream["width"]) if video_stream["width"]
      end

      def height
        Integer(video_stream["height"]) if video_stream["height"]
      end

      def duration
        Float(video_stream["duration"]) if video_stream["duration"]
      end

      def angle
        Integer(tags["rotate"]) if tags["rotate"]
      end

      def aspect_ratio
        if descriptor = video_stream["display_aspect_ratio"]
          descriptor.split(":", 2).collect(&:to_i)
        end
      end


      def tags
        @tags ||= video_stream["tags"] || {}
      end

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

      def streams
        probe["streams"] || []
      end

      def probe
        download_blob_to_tempfile { |file| probe_from(file) }
      end

      def probe_from(file)
        IO.popen([ "ffprobe", "-print_format", "json", "-show_streams", "-v", "error", file.path ]) do |output|
          JSON.parse(output.read)
        end
      rescue Errno::ENOENT
        logger.info "Skipping video analysis because ffmpeg isn't installed"
        {}
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activestorage-5.2.0.beta2 lib/active_storage/analyzer/video_analyzer.rb
activestorage-5.2.0.beta1 lib/active_storage/analyzer/video_analyzer.rb