Class: ActiveEncode::EngineAdapters::PassThroughAdapter
- Inherits:
-
Object
- Object
- ActiveEncode::EngineAdapters::PassThroughAdapter
- 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"
- FFMPEG_PATH =
ENV["FFMPEG_PATH"] || "ffmpeg"
Instance Method Summary collapse
-
#cancel(id) ⇒ Object
Cancel ongoing encode using pid file.
- #create(original_input_url, options = {}) ⇒ Object
-
#find(id, opts = {}) ⇒ Object
Return encode object from file system.
Instance Method Details
#cancel(id) ⇒ Object
Cancel ongoing encode using pid file
136 137 138 139 140 141 142 143 144 |
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 136 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(original_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 |
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 20 def create(original_input_url, = {}) input_url = sanitize_input_url(original_input_url) new_encode = ActiveEncode::Base.new(input_url, ) 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)} "#{ActiveEncode.sanitize_uri(input_url)}"` 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 # 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 = input_url.to_s.gsub(/\?.*/, '') copy_url = filepath.gsub(filepath, "#{File.basename(filepath, File.extname(filepath))}_temp#{File.extname(input_url)}") 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} --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 # For saving filename to label map used to find the label when building outputs filename_label_hash = {} # Copy derivatives to work directory [:outputs].each do |opt| output_path = copy_derivative_to_working_path(opt[:url], new_encode.id) 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.] write_errors new_encode new_encode end |
#find(id, opts = {}) ⇒ Object
Return encode object from file system
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_encode/engine_adapters/pass_through_adapter.rb', line 98 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.input.duration ||= fixed_duration(working_path("duration_input_metadata", encode.id)) if File.exist?(working_path("duration_input_metadata", 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.] write_errors encode encode end |