class ActiveStorage::Analyzer::VideoAnalyzer
Extracts the following from a video blob:
-
Width (pixels)
-
Height (pixels)
-
Duration (seconds)
-
Angle (degrees)
-
Display aspect ratio
Example:
ActiveStorage::Analyzer::VideoAnalyzer.new(blob).metadata # => { width: 640.0, height: 480.0, duration: 5.0, angle: 0, display_aspect_ratio: [4, 3] }
When a video's angle is 90 or 270 degrees, its width and height are automatically swapped for convenience.
This analyzer requires the FFmpeg system library, which is not provided by Rails.
Public Class Methods
accept?(blob)
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 20 def self.accept?(blob) blob.video? end
Public Instance Methods
metadata()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 24 def metadata { width: width, height: height, duration: duration, angle: angle, display_aspect_ratio: display_aspect_ratio }.compact end
Private Instance Methods
angle()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 49 def angle Integer(tags["rotate"]) if tags["rotate"] end
computed_height()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 69 def computed_height if encoded_width && display_height_scale encoded_width * display_height_scale end end
display_aspect_ratio()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 53 def display_aspect_ratio if descriptor = video_stream["display_aspect_ratio"] if terms = descriptor.split(":", 2) numerator = Integer(terms[0]) denominator = Integer(terms[1]) [numerator, denominator] unless numerator == 0 end end end
display_height_scale()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 83 def display_height_scale @display_height_scale ||= Float(display_aspect_ratio.last) / display_aspect_ratio.first if display_aspect_ratio end
duration()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 45 def duration Float(video_stream["duration"]) if video_stream["duration"] end
encoded_height()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 79 def encoded_height @encoded_height ||= Float(video_stream["height"]) if video_stream["height"] end
encoded_width()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 75 def encoded_width @encoded_width ||= Float(video_stream["width"]) if video_stream["width"] end
ffprobe_path()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 113 def ffprobe_path ActiveStorage.paths[:ffprobe] || "ffprobe" end
height()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 37 def height if rotated? encoded_width else computed_height || encoded_height end end
probe()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 100 def probe download_blob_to_tempfile { |file| probe_from(file) } end
probe_from(file)
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 104 def probe_from(file) IO.popen([ ffprobe_path, "-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
rotated?()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 65 def rotated? angle == 90 || angle == 270 end
streams()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 96 def streams probe["streams"] || [] end
video_stream()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 92 def video_stream @video_stream ||= streams.detect { |stream| stream["codec_type"] == "video" } || {} end
width()
click to toggle source
# File lib/active_storage/analyzer/video_analyzer.rb, line 29 def width if rotated? computed_height || encoded_height else encoded_width end end