class Libis::Format::Converter::VideoConverter

Public Class Methods

input_types() click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 12
def self.input_types
  [:WEBM, :MP4, :MPG, :MKV, :MJP2, :QTFF, :AVI, :OGGV, :WMV, :DV, :FLV, :SWF]
end
new() click to toggle source
Calls superclass method Libis::Format::Converter::Base::new
# File lib/libis/format/converter/video_converter.rb, line 21
def initialize
  super
end
output_types(format = nil) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 16
def self.output_types(format = nil)
  return [] unless input_types.include?(format) if format
  [:GIF, :WEBM, :MP4, :MPG, :MKV, :MJP2, :QTFF, :AVI, :OGGV, :WMV, :DV, :FLV, :SWF]
end

Public Instance Methods

assemble_and_convert(sources, target) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 186
def assemble_and_convert(sources, target)
  Tempfile.create(%w(list .txt)) do |f|
    sources.each {|src| f.puts src}
    opts[:global] ||= []
    opts[:global] += %w(-f concat)
    f.close
    target = convert_file(f.to_path, target)
  end
  target
end
audio_bitrate(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 61
def audio_bitrate(value)
  @options[:audio_bitrate] = value
end
audio_channels(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 33
def audio_channels(value)
  @options[:audio_channels] = value
end
audio_codec(codec) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 37
def audio_codec(codec)
  @options[:audio_codec] = codec
end
audio_preset(name) click to toggle source

@param [String] name name of an audio preset. See FFMpeg documentation for more info

# File lib/libis/format/converter/video_converter.rb, line 141
def audio_preset(name)
  @options[:audio_preset] = name
end
audio_quality(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 53
def audio_quality(value)
  @options[:audio_quality] = value
end
constant_rate_factor(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 69
def constant_rate_factor(value)
  @options[:crf] = value
end
convert(source, target, _format, opts = {}) click to toggle source

def video_encoder(value)

@options[:video_encoder] = value

end

def audio_encoder(value)

@options[:audio_encoder] = value

end

def encoder_options(value)

@options[:encoder_options] = value

end

Calls superclass method Libis::Format::Converter::Base#convert
# File lib/libis/format/converter/video_converter.rb, line 162
def convert(source, target, _format, opts = {})
  super

  FileUtils.mkpath(File.dirname(target))

  if source.is_a? Array

    assemble_and_convert(source, target)

  elsif File.directory?(source)

    sources = Dir[File.join(source, '**', '*')].reject {|p| File.directory? p}
    assemble_and_convert(sources, target)

  else

    convert_file(source, target)

  end

  target

end
duration(seconds) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 49
def duration(seconds)
  @options[:duration] = seconds
end
format(format) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 29
def format(format)
  @options[:format] = format
end
frame_rate(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 73
def frame_rate(value)
  @options[:frame_rate] = value
end
preset(name) click to toggle source

@param [String] name name of a preset. See FFMpeg documentation for more info

# File lib/libis/format/converter/video_converter.rb, line 136
def preset(name)
  @options[:preset] = name
end
quiet(v) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 25
def quiet(v)
  @flags[:quiet] = !!v
end
sampling_freq(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 77
def sampling_freq(value)
  @options[:sampling_freq] = value
end
scale(width_x_height) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 81
def scale(width_x_height)
  @options[:scale] = width_x_height
end
start(seconds) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 45
def start(seconds)
  @options[:start] = seconds
end
video_bitrate(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 65
def video_bitrate(value)
  @options[:video_bitrate] = value
end
video_codec(codec) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 41
def video_codec(codec)
  @options[:video_codec] = codec
end
video_preset(name) click to toggle source

@param [String] name name of a video preset. See FFMpeg documentation for more info

# File lib/libis/format/converter/video_converter.rb, line 146
def video_preset(name)
  @options[:video_preset] = name
end
video_quality(value) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 57
def video_quality(value)
  @options[:video_quality] = value
end
watermark_image(file) click to toggle source

@param [String] file Image file path to use as watermark

# File lib/libis/format/converter/video_converter.rb, line 86
def watermark_image(file)
  @options[:watermark_image] = file
end
watermark_opacity(value) click to toggle source

@param [Number] value watermark opacity (0-1) with 0 = invisible and 1 = 100% opaque. Default: 0.5

# File lib/libis/format/converter/video_converter.rb, line 123
def watermark_opacity(value)
  @options[:watermark_opacity] = value.to_f
end
watermark_position(value) click to toggle source

@param [String] value one of 'bottom_left' (default), 'top_left', 'bottom_right', 'top_right', 'center'

# File lib/libis/format/converter/video_converter.rb, line 118
def watermark_position(value)
  @options[:watermark_position] = value
end
watermark_text(value) click to toggle source

@param [String] value text for watermark. No watermark if nil (default)

# File lib/libis/format/converter/video_converter.rb, line 91
def watermark_text(value)
  @options[:watermark_text] = value
end
watermark_text_color(value) click to toggle source

@param [String] value Text color for the watermark text. Default: white

# File lib/libis/format/converter/video_converter.rb, line 102
def watermark_text_color(value)
  @options[:watermark_text_color] = value
end
watermark_text_shadow_color(value) click to toggle source

@param [String] value Text color for the watermark text shadow. Default: black

# File lib/libis/format/converter/video_converter.rb, line 107
def watermark_text_shadow_color(value)
  @options[:watermark_text_shadow_color] = value
end
watermark_text_shadow_offset(value) click to toggle source

@param [Integer] value Offset of the watermark text shadow. Used for both x and y offset; default: 1 If the offset is set to 0, no shadow will be printed

# File lib/libis/format/converter/video_converter.rb, line 113
def watermark_text_shadow_offset(value)
  @options[:watermark_text_offset] = value.to_i
end
watermark_text_size(value) click to toggle source

@param [Integer] value Font size for watermark text. Default: 10 Note that the font is selected by the Config setting

# File lib/libis/format/converter/video_converter.rb, line 97
def watermark_text_size(value)
  @options[:watermark_text_size] = value.to_i
end
web_stream(value) click to toggle source

@param [Boolean] value If set to true automatically selects optimal format for web viewing. Default: false

# File lib/libis/format/converter/video_converter.rb, line 128
def web_stream(value)
  if value
    @options[:video_codec] = 'h264'
    @options[:audio_codec] = 'acc'
  end
end

Protected Instance Methods

convert_file(source, target) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 199
def convert_file(source, target)
  # FLV special: only supports aac and speex audio codecs
  format = (@options[:format] || File.extname(target)[1..-1]).to_s.downcase
  @options[:audio_codec] ||= 'aac' if %w'flv'.include?(format)

  # SWF special: only supports mp3 audio codec
  format = (@options[:format] || File.extname(target)[1..-1]).to_s.downcase
  @options[:audio_codec] ||= 'mp3' if %w'swf'.include?(format)

  # Set up FFMpeg command line parameters
  opts = {global: [], input: [], filter: [], output: []}
  opts[:global] << '-hide_banner'
  opts[:global] << '-loglevel' << (@options[:quiet] ? 'fatal' : 'warning')

  # Watermark info
  @options[:watermark_opacity] ||= 0.5
  if @options[:watermark_image]
    opts[:filter] << '-i' << @options[:watermark_image] << '-filter_complex'
    opts[:filter] << "[1:v]format=argb,colorchannelmixer=aa=%f[wm];[0:v][wm]overlay=%s" %
        [@options[:watermark_opacity], watermark_position_text]
  elsif @options[:watermark_text]
    @options[:watermark_text_size] ||= 10
    @options[:watermark_text_color] ||= 'white'
    @options[:watermark_text_shadow_color] ||= 'black'
    @options[:watermark_text_shadow_offset] ||= 1
    filter_text = "drawtext=text='%s':%s:fontfile=%s:fontsize=%d:fontcolor=%s@%f" %
        [
            @options[:watermark_text],
            watermark_position_text(true),
            Config[:watermark_font],
            @options[:watermark_text_size],
            @options[:watermark_text_color],
            @options[:watermark_opacity]
        ]
    filter_text += ':shadowcolor=%s@%f:shadowx=%d:shadowy=%d' %
        [
            @options[:watermark_text_shadow_color],
            @options[:watermark_opacity],
            @options[:watermark_text_shadow_offset],
            @options[:watermark_text_shadow_offset]
        ] if @options[:watermark_text_shadow_offset] > 0
    opts[:filter] << '-vf' << filter_text
  end
  opts[:output] << '-ac' << @options[:audio_channels].to_s if @options[:audio_channels]
  opts[:output] << '-c:a' << @options[:audio_codec].to_s if @options[:audio_codec]
  opts[:output] << '-c:v' << @options[:video_codec].to_s if @options[:video_codec]
  opts[:output] << '-b:a' << @options[:audio_bitrate].to_s if @options[:audio_bitrate]
  opts[:output] << '-b:v' << @options[:video_bitrate].to_s if @options[:video_bitrate]
  opts[:output] << '-crf' << @options[:crf].to_s if @options[:crf]
  opts[:output] << '-map_metadata:g' << '0:g' # Copy global metadata
  opts[:output] << '-map_metadata:s:a' << '0:s:a' # Copy audio metadata
  opts[:output] << '-map_metadata:s:v' << '0:s:v' # Copy video metadata
  opts[:input] << '-accurate_seek' << (@options[:start].to_i < 0 ? '-sseof' : '-ss') << @options[:start].to_s if @options[:start]
  opts[:input] << '-t' << @options[:duration].to_s if @options[:duration]
  opts[:output] << '-qscale' << @options[:video_quality].to_s if @options[:video_quality]
  opts[:output] << '-q:a' << @options[:audio_quality].to_s if @options[:audio_quality]
  opts[:output] << '-r' << @options[:frame_rate].to_s if @options[:frame_rate]
  opts[:output] << '-ar' << @options[:sampling_freq].to_s if @options[:sampling_freq]
  if @options[:scale]
    scale = @options[:scale].split('x')
    width = scale[0]
    height = scale[1]
    opts[:output] << '-vf' << "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
  end
  opts[:output] << '-f' << @options[:format] if @options[:format]
  opts[:output] << '-pre' << @options[:preset] if @options[:preset]
  opts[:output] << '-apre' << @options[:audio_preset] if @options[:audio_preset]
  opts[:output] << '-vpre' << @options[:video_preset] if @options[:video_preset]
  info "FFMpeg options: #{opts}"
  result = Libis::Format::Tool::FFMpeg.run(source, target, opts)
  info "FFMpeg output: #{result}"
  target
end
watermark_position_text(for_text = false, margin = 10) click to toggle source
# File lib/libis/format/converter/video_converter.rb, line 273
def watermark_position_text(for_text = false, margin = 10)
  w = for_text ? 'tw' : 'w'
  h = for_text ? 'th' : 'h'
  case @options[:watermark_position]
    when 'bottom_left'
      "x=#{margin}:y=H-#{h}-#{margin}"
    when 'top_left'
      "x=#{margin}:y=#{margin}"
    when 'bottom_right'
      "x=W-#{w}-#{margin}:y=H-#{h}-#{margin}"
    when 'top_right'
      "x=W-#{w}-#{margin}:y=#{margin}"
    else
      "x=#{margin}:y=H-#{h}-#{margin}"
  end
end