Class: Hardcode::Worker

Inherits:
Object
  • Object
show all
Includes:
Sneakers::Worker
Defined in:
lib/hardcode/worker.rb

Instance Method Summary collapse

Instance Method Details

#work(msg) ⇒ Object

[View source]

14
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
# File 'lib/hardcode/worker.rb', line 14

def work(msg)
  begin
    job = JSON.parse(msg)
    source_file = job['source']
    ffmpeg_options = if job['ffmpeg_options']
      "--ffmpeg-options \"#{job['ffmpeg_options']}\""
    else
      ""
    end
    # move mp4 and mp3 file directly without encoding
    if File.extname(source_file).match("^\.(mp4|mp3)$") != nil
      FileUtils.mv(source_file, job['dest_dir'], verbose: true)
    else
    puts output = %x[stack-encode encode --no-progress --log-file #{STACK_ENCODE_LOG} #{ffmpeg_options} '#{source_file}']
      if $?.success?
        filename = output[/.*>\s(.*)$/, 1]
        logger.info "Transcoding successful, deleting source file."
        FileUtils.mv(File.join(File.dirname(source_file), filename), job['dest_dir'], verbose: true)
        FileUtils.rm(source_file, verbose: true)
      else
        logger.error "Error: Transcoding failed."
      end
    end
  rescue => e
    message = "Error: #{e.message} - #{e.backtrace}"
    logger.fatal message
    raise message
  end
  logger.info "Finished: #{job.to_s}"
  ack!
end