Class: FFMPEG::ActiveStorage::Previewer

Inherits:
ActiveStorage::Previewer
  • Object
show all
Defined in:
lib/ffmpeg/active_storage/previewer.rb

Overview

Active Storage Previewer for video files using FFMPEG

Generates video preview images (thumbnails) for Active Storage.

Examples:

Add to Active Storage previewers

# config/initializers/active_storage.rb
Rails.application.config.active_storage.previewers.prepend(
  FFMPEG::ActiveStorage::Previewer
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept?(blob) ⇒ Boolean

Check if this previewer can process the blob

Parameters:

  • blob (ActiveStorage::Blob)

Returns:

  • (Boolean)


20
21
22
# File 'lib/ffmpeg/active_storage/previewer.rb', line 20

def accept?(blob)
  blob.video?
end

Instance Method Details

#preview(**options) {|io, options| ... } ⇒ Object

Generate a preview image

Yields:

  • (io, options)

    the generated preview image



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ffmpeg/active_storage/previewer.rb', line 27

def preview(**options)
  download_blob_to_tempfile do |input|
    media = FFMPEG::Media.new(input.path)

    # Extract frame at 10% into the video (avoid black frames at start)
    timestamp = (media.duration || 10) * 0.1

    draw_frame(media, timestamp) do |output|
      yield io: output, filename: "#{blob.filename.base}.jpg",
            content_type: "image/jpeg"
    end
  end
end