Class: VTools::Thumbnailer

Inherits:
Object
  • Object
show all
Includes:
SharedMethods
Defined in:
lib/vtools/thumbnailer.rb

Overview

Takes care about generating thumbnails requires ffmpegthumbnailer

Instance Method Summary collapse

Methods included from SharedMethods

included

Methods included from SharedMethods::Common

#config, #fix_encoding, #generate_path, #hash_to_obj, #json_to_obj, #keys_to_sym, #log, #logger=, #network_call, #parse_json, #path_generator

Constructor Details

#initialize(video) ⇒ Thumbnailer

Returns a new instance of Thumbnailer.



10
11
12
# File 'lib/vtools/thumbnailer.rb', line 10

def initialize video
  @video = video
end

Instance Method Details

#runObject

thumbnailer job



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vtools/thumbnailer.rb', line 15

def run

  options = @video.thumbs_options
  errors = []
  thumbs = []
  postfix = options[:postfix]
  @total = options[:thumb_count].to_i

  @output_file = "#{generate_path @video.name, "thumb"}/#{@video.name}_"
  command = "#{CONFIG[:thumb_binary]} -i '#{@video.path}' #{options} "

  # callback
  Hook.exec :before_thumb, @video, options

  # process cicle
  @total.times do |count|

    seconds = (options && options[:t]) || set_point(options || count)

    file = "#{@output_file}#{ postfix || (options && options[:t]) || count }.jpg"
    exec = "#{command} -t #{seconds} -o '#{file}' 2>&1"
    options = nil

    Open3.popen3(exec) do |stdin, stdout, stderr|
      lines = stdout.readlines.join(" ")
      VTools.fix_encoding lines
      # save thumb if no error
      if (error = lines).empty?
        thumbs << thumb = {:path => file, :offset => time_offset(seconds)}
        Hook.exec :in_thumb, @video, thumb # callbacks
      else
        errors << "#{error} (#{file})"
      end
    end
  end

  # callbacks
  if errors.empty?
    Hook.exec :thumb_success, @video, thumbs
  else
    errors = " Errors: #{errors.flatten.join(";").gsub(/\n/, ' ')}. "
    Hook.exec :thumb_error, @video, errors
    raise ProcessError, "Thumbnailer error: #{errors}" if thumbs.empty? && @total > 0
  end

  thumbs
end