Class: ActiveEncode::EngineAdapters::FfmpegAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_encode/engine_adapters/ffmpeg_adapter.rb

Constant Summary collapse

WORK_DIR =

Should read from config

ENV["ENCODE_WORK_DIR"] || "encodes"

Instance Method Summary collapse

Instance Method Details

#cancel(id) ⇒ Object

Cancel ongoing encode using pid file



100
101
102
103
104
105
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 100

def cancel(id)
  pid = get_pid(id)
  Process.kill 'SIGTERM', pid.to_i

  find id
end

#create(input_url, options = {}) ⇒ Object



10
11
12
13
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
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 10

def create(input_url, options = {})
  new_encode = ActiveEncode::Base.new(input_url, options)
  new_encode.id = SecureRandom.uuid
  new_encode.created_at = Time.now.utc
  new_encode.updated_at = Time.now.utc
  new_encode.current_operations = []
  new_encode.output = []

  # Create a working directory that holds all output files related to the encode
  FileUtils.mkdir_p working_path("", new_encode.id)
  FileUtils.mkdir_p working_path("outputs", new_encode.id)

  # Extract technical metadata from input file
  `mediainfo --Output=XML --LogFile=#{working_path("input_metadata", new_encode.id)} #{input_url}`
  new_encode.input = build_input new_encode

  if new_encode.input.duration.blank?
    new_encode.state = :failed
    new_encode.percent_complete = 1

    new_encode.errors = if new_encode.input.file_size.blank?
                          ["#{input_url} does not exist or is not accessible"]
                        else
                          ["Error inspecting input: #{input_url}"]
                        end

    write_errors new_encode
    return new_encode
  end

  new_encode.state = :running
  new_encode.percent_complete = 1
  new_encode.errors = []

  # Run the ffmpeg command and save its pid
  command = ffmpeg_command(input_url, new_encode.id, options)
  pid = Process.spawn(command)
  File.open(working_path("pid", new_encode.id), 'w') { |file| file.write pid }
  new_encode.input.id = pid

  # Prevent zombie process
  Process.detach(pid)

  new_encode
end

#find(id, opts = {}) ⇒ Object

Return encode object from file system



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 57

def find(id, opts = {})
  encode_class = opts[:cast]
  encode_class ||= ActiveEncode::Base
  encode = encode_class.new(nil, opts)
  encode.id = id
  encode.output = []
  encode.created_at, encode.updated_at = get_times encode.id
  encode.input = build_input encode
  encode.percent_complete = calculate_percent_complete encode

  pid = get_pid(id)
  encode.input.id = pid if pid.present?

  if File.file? working_path("error.log", id)
    error = File.read working_path("error.log", id)
    if error.present?
      encode.state = :failed
      encode.errors = [error]

      return encode
    end
  end

  encode.errors = []

  encode.current_operations = []
  encode.created_at, encode.updated_at = get_times encode.id

  if running? pid
    encode.state = :running
    encode.current_operations = ["transcoding"]
  elsif progress_ended?(encode.id) && encode.percent_complete == 100
    encode.state = :completed
  elsif encode.percent_complete < 100
    encode.state = :cancelled
  end

  encode.output = build_outputs encode if encode.completed?

  encode
end