Class: ActiveEncode::EngineAdapters::FfmpegAdapter
- Inherits:
-
Object
- Object
- ActiveEncode::EngineAdapters::FfmpegAdapter
- Extended by:
- Cleaner
- Defined in:
- lib/active_encode/engine_adapters/ffmpeg_adapter.rb,
lib/active_encode/engine_adapters/ffmpeg_adapter/cleaner.rb
Defined Under Namespace
Modules: Cleaner
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
-
#cancel(id) ⇒ Object
Cancel ongoing encode using pid file.
- #create(input_url, options = {}) ⇒ Object
-
#find(id, opts = {}) ⇒ Object
Return encode object from file system.
Methods included from Cleaner
build_file_list, file_check, remove_child_files, remove_empty_directories, remove_files, remove_old_files!
Instance Method Details
#cancel(id) ⇒ Object
Cancel ongoing encode using pid file
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 156 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
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 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 111 112 113 114 115 |
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 20 def create(input_url, = {}) # Decode file uris for ffmpeg (mediainfo works either way) case input_url when /^file\:\/\/\// input_url = Addressable::URI.unencode(input_url) input_url = ActiveEncode.sanitize_input(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, ) 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 curl_option = if && [:headers] headers = [:headers].map { |k, v| "#{k}: #{v}" } (["--File_curl=HttpHeader"] + headers).join(",").yield_self { |s| "'#{s}'" } else "" end clean_url = input_url.is_a?(String) ? ActiveEncode.sanitize_uri(input_url) : input_url `#{MEDIAINFO_PATH} #{curl_option} --Output=XML --LogFile=#{working_path("input_metadata", new_encode.id)} "#{clean_url}"` new_encode.input = build_input new_encode # Log error if file is empty or inaccessible if new_encode.input.duration.blank? && new_encode.input.file_size.blank? file_error(new_encode, input_url) return new_encode # If file is not empty, try copying file to generate missing metadata elsif new_encode.input.duration.blank? && new_encode.input.file_size.present? # This regex removes the query string from URIs. This is necessary to # properly process files originating from S3 or similar providers. filepath = clean_url.to_s.gsub(/\?.*/, '') copy_url = filepath.gsub(filepath, "#{File.basename(filepath, File.extname(filepath))}_temp#{File.extname(filepath)}") copy_path = working_path(copy_url, new_encode.id) # -map 0 sets ffmpeg to copy all available streams. # -c copy sets ffmpeg to copy all codecs # -y automatically overwrites the temp file if one already exists `#{FFMPEG_PATH} -loglevel level+fatal -i \"#{input_url}\" -map 0 -c copy -y \"#{copy_path}\"` # If ffmpeg copy fails, log error because file is either not a media file # or the file extension does not match the codecs used to encode the file unless $CHILD_STATUS.success? file_error(new_encode, input_url) return new_encode end # Write the mediainfo output to a separate file to preserve metadata from original file `#{MEDIAINFO_PATH} #{curl_option} --Output=XML --LogFile=#{working_path("duration_input_metadata", new_encode.id)} "#{copy_path}"` File.delete(copy_path) # Assign duration to the encode created for the original file. new_encode.input.duration = fixed_duration(working_path("duration_input_metadata", new_encode.id)) 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, ) # Capture the exit status in a file in order to differentiate warning output in stderr between real process failure exit_status_file = working_path("exit_status.code", new_encode.id) command = "#{command}; echo $? > #{exit_status_file}" 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.] write_errors new_encode new_encode ensure # Prevent zombie process Process.detach(pid) if pid.present? end |
#find(id, opts = {}) ⇒ Object
Return encode object from file system
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/active_encode/engine_adapters/ffmpeg_adapter.rb', line 118 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.input.duration ||= fixed_duration(working_path("duration_input_metadata", encode.id)) if File.exist?(working_path("duration_input_metadata", encode.id)) 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) exit_code = read_exit_code(id) if exit_code.present? && exit_code != 0 && exit_code != 143 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 < (completeness_threshold || 100) encode.errors.prepend("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 |