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"
MEDIAINFO_PATH =
ENV["MEDIAINFO_PATH"] || "mediainfo"
FFMPEG_PATH =
ENV["FFMPEG_PATH"] || "ffmpeg"

Instance Method Summary collapse

Instance Method Details

#cancel(id) ⇒ Object

Cancel ongoing encode using pid file



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 113

def cancel(id)
  encode = find id
  if encode.running?
    pid = get_pid(id)

    IO.popen("ps -ef | grep #{pid}") do |pipe|
      child_pids = pipe.readlines.map do |line|
        parts = line.split(/\s+/)
        parts[1] if parts[2] == pid.to_s && parts[1] != pipe.pid.to_s
      end.compact

      child_pids.each do |cpid|
        Process.kill 'SIGTERM', cpid.to_i
      end
    end

    Process.kill 'SIGTERM', pid.to_i
    File.write(working_path("cancelled", id), "")
    encode = find id
  end
  encode
rescue Errno::ESRCH
  raise NotRunningError
rescue StandardError
  raise CancelError
end

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



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 13

def create(input_url, options = {})
  # Decode file uris for ffmpeg (mediainfo works either way)
  case input_url
  when /^file\:\/\/\//
    input_url = URI.decode(input_url)
  when /^s3\:\/\//
    require 'file_locator'

    s3_object = FileLocator::S3File.new(input_url).object
    input_url = URI.parse(s3_object.presigned_url(:get))
  end

  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_PATH} --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, err: working_path('error.log', new_encode.id))
  File.open(working_path("pid", new_encode.id), 'w') { |file| file.write pid }
  new_encode.input.id = pid

  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
ensure
  # Prevent zombie process
  Process.detach(pid) if pid.present?
end

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

Return encode object from file system



77
78
79
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
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 77

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?

  encode.current_operations = []
  encode.created_at, encode.updated_at = get_times encode.id
  encode.errors = read_errors(id)
  if encode.errors.present?
    encode.state = :failed
  elsif running? pid
    encode.state = :running
    encode.current_operations = ["transcoding"]
  elsif progress_ended?(encode.id) && encode.percent_complete == 100
    encode.state = :completed
  elsif cancelled? encode.id
    encode.state = :cancelled
  elsif encode.percent_complete < 100
    encode.errors << "Encoding has completed but the output duration is shorter than the input"
    encode.state = :failed
  end

  encode.output = build_outputs encode if encode.completed?

  encode
end