Class: ActiveEncode::EngineAdapters::PassThroughAdapter

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

Constant Summary collapse

WORK_DIR =

Should read from config

ENV["ENCODE_WORK_DIR"] || "encodes"
MEDIAINFO_PATH =
ENV["MEDIAINFO_PATH"] || "mediainfo"

Instance Method Summary collapse

Instance Method Details

#cancel(id) ⇒ Object

Cancel ongoing encode using pid file



117
118
119
120
121
122
123
124
125
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 117

def cancel(id)
  # Check for errors and if not then create cancelled file else raise CancelError?
  if running?(id)
    File.write(working_path("cancelled", id), "")
    find id
  else
    raise CancelError
  end
end

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



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 19

def create(input_url, options = {})
  # Decode file uris for ffmpeg (mediainfo works either way)
  input_url = URI.decode(input_url) if input_url.starts_with? "file:///"

  new_encode = ActiveEncode::Base.new(input_url, options)
  new_encode.id = SecureRandom.uuid
  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_PATH} --Output=XML --LogFile=#{working_path("input_metadata", new_encode.id)} #{input_url.shellescape}`
  new_encode.input = build_input new_encode
  new_encode.input.id = new_encode.id
  new_encode.created_at, new_encode.updated_at = get_times new_encode.id

  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

  # For saving filename to label map used to find the label when building outputs
  filename_label_hash = {}

  # Copy derivatives to work directory
  options[:outputs].each do |opt|
    url = opt[:url]
    output_path = working_path("outputs/#{sanitize_base opt[:url]}#{File.extname opt[:url]}", new_encode.id)
    FileUtils.cp FileLocator.new(url).location, output_path
    filename_label_hash[output_path] = opt[:label]
  end

  # Write filename-to-label map so we can retrieve them on build_output
  File.write working_path("filename_label.yml", new_encode.id), filename_label_hash.to_yaml

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

  new_encode
rescue StandardError => e
  new_encode.state = :failed
  new_encode.percent_complete = 1
  new_encode.errors = [e.full_message]
  write_errors new_encode
  return new_encode
end

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

Return encode object from file system



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 80

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

  encode.errors = read_errors(id)
  if encode.errors.present?
    encode.state = :failed
    encode.percent_complete = 1
  elsif cancelled?(id)
    encode.state = :cancelled
    encode.percent_complete = 1
  elsif completed?(id)
    encode.state = :completed
    encode.percent_complete = 100
  else
    encode.output = build_outputs encode
    encode.state = :completed
    encode.percent_complete = 100
  end

  encode
rescue StandardError => e
  encode.state = :failed
  encode.percent_complete = 1
  encode.errors = [e.full_message]
  write_errors encode
  return encode
end