Class: Kithe::FfmpegTransformer

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

Instance Method Summary collapse

Constructor Details

#initialize(output_suffix:, bitrate: nil, force_mono: false, audio_codec: nil, other_ffmpeg_args: nil) ⇒ FfmpegTransformer

Specifies the most important args to send to ffmpeg for creating audio and video derivatives.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/derivative_transformers/kithe/ffmpeg_transformer.rb', line 14

def initialize(output_suffix:, bitrate:nil, force_mono:false, audio_codec:nil, other_ffmpeg_args:nil)
  @output_suffix, @bitrate, @force_mono, @audio_codec, @other_ffmpeg_args =
   output_suffix,  bitrate,  force_mono,  audio_codec,  other_ffmpeg_args

  # Default settings:
  @audio_codec ||= 'libopus' if output_suffix == 'webm'

  # Validation:
  if other_ffmpeg_args && other_ffmpeg_args.class != Array
    raise ArgumentError.new('If "other_ffmpeg_args" is not nil, it needs to be an array of strings.')
  end
  if bitrate && !/\d+k/.match(bitrate)
    raise ArgumentError.new('If "bitrate" is not nil, it needs to be a string like "64k" or "128k".')
  end
end

Instance Method Details

#call(original_file) ⇒ Object

Will raise TTY::Command::ExitError if the ffmpeg returns non-null.



42
43
44
45
46
47
48
49
50
# File 'app/derivative_transformers/kithe/ffmpeg_transformer.rb', line 42

def call(original_file)
  tempfile = Tempfile.new(['temp_deriv', ".#{@output_suffix}"])
  # -y tells ffmpeg to overwrite the abovementioned tempfile (still empty)
  # with the output of ffmpeg.
  ffmpeg_args =  [ffmpeg_command, "-y", "-i", original_file.path]
  ffmpeg_args += transform_arguments + [tempfile.path]
  TTY::Command.new(printer: :null).run(*ffmpeg_args)
  return tempfile
end

#transform_argumentsObject

These are the arguments that specify the nature of the derivative transformation (rather than those specifying input and output files, which are in the call method.



32
33
34
35
36
37
38
39
# File 'app/derivative_transformers/kithe/ffmpeg_transformer.rb', line 32

def transform_arguments
  result  = []
  result += ["-ac", "1"] if @force_mono
  result += ["-codec:a", @audio_codec] if @audio_codec
  result += ["-b:a", @bitrate] if @bitrate
  result += @other_ffmpeg_args if @other_ffmpeg_args
  result
end