Class: Kithe::FfmpegExtractJpg

Inherits:
Object
  • Object
show all
Defined in:
app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb

Overview

Creates a JPG screen capture using ffmpeg, by default with the ‘thumbnail` filter to choose a representative frame from the first minute or so.

Examples:

tempfile = FfmpegExtractJpg.new.call(shrine_uploaded_file)

tempfile = FfmpegExtractJpg.new.call(url)

tempfile = FfmpegExtractJpg.new(start_seconds: 60).call(shrine_uploaded_file)

tempfile = FfmpegExtractJpg.new(start_seconds: 10, width_pixels: 420).call(shrine_uploaded_file)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_seconds: 0, frame_sample_size: false, width_pixels: nil) ⇒ FfmpegExtractJpg

Returns a new instance of FfmpegExtractJpg.

Parameters:

  • start_seconds (Integer) (defaults to: 0)

    seek to this point to find thumbnail. If it’s after the end of the video, you won’t get a thumb back though! [Default 0]

  • frame_sample_size (Integer, false, nil) (defaults to: false)

    argument passed to ffmpeg thumbnail filter, how many frames to sample, starting at start_seconds, to choose representative thumbnail. If set to false, thumbnail filter won’t be used. If this one goes past the end of the video, ffmpeg is fine with it. Set to ‘false` to disable use of ffmpeg sample feature, and just use exact frame at start_seconds.

    NOTE: This can consume significant RAM depending on value and video resolution.

    Default false, not operative


32
33
34
35
36
# File 'app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb', line 32

def initialize(start_seconds: 0, frame_sample_size: false, width_pixels: nil)
  @start_seconds = start_seconds
  @frame_sample_size = frame_sample_size
  @width_pixels = width_pixels
end

Instance Attribute Details

#frame_sample_sizeObject (readonly)

Returns the value of attribute frame_sample_size.



13
14
15
# File 'app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb', line 13

def frame_sample_size
  @frame_sample_size
end

#start_secondsObject (readonly)

Returns the value of attribute start_seconds.



13
14
15
# File 'app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb', line 13

def start_seconds
  @start_seconds
end

#width_pixelsObject (readonly)

Returns the value of attribute width_pixels.



13
14
15
# File 'app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb', line 13

def width_pixels
  @width_pixels
end

Instance Method Details

#call(input_arg) ⇒ Object

Parameters:

  • input_arg (String, File, Shrine::UploadedFile)

    local File; String that can be URL or local file path; or Shrine::UploadedFile. If Shrine::UploadedFile, we’ll try to get a URL from it if we can, otherwise use or make a local tempfile. Most efficient is if we have a remote URL to give ffmpeg, one way or another!



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/derivative_transformers/kithe/ffmpeg_extract_jpg.rb', line 46

def call(input_arg)
  if input_arg.kind_of?(Shrine::UploadedFile)
    if input_arg.respond_to?(:url) && input_arg.url&.start_with?(/https?\:/)
      _call(input_arg.url)
    else
      Shrine.with_file(input_arg) do |local_file|
        _call(local_file.path)
      end
    end
  elsif input_arg.respond_to?(:path)
    _call(input_arg.path)
  else
    _call(input_arg.to_s)
  end
end