active_encode 1.2.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,156 +0,0 @@
1
- # frozen_string_literal: true
2
- module ActiveEncode
3
- module EngineAdapters
4
- class ZencoderAdapter
5
- # TODO: add a stub for an input helper (supplied by an initializer) that transforms encode.input.url into a zencoder accepted url
6
- def create(input_url, _options = {})
7
- response = Zencoder::Job.create(input: input_url.to_s)
8
- build_encode(get_job_details(response.body["id"]))
9
- end
10
-
11
- def find(id, _opts = {})
12
- build_encode(get_job_details(id))
13
- end
14
-
15
- def cancel(id)
16
- response = Zencoder::Job.cancel(id)
17
- build_encode(get_job_details(id)) if response.success?
18
- end
19
-
20
- private
21
-
22
- def get_job_details(job_id)
23
- Zencoder::Job.details(job_id)
24
- end
25
-
26
- def get_job_progress(job_id)
27
- Zencoder::Job.progress(job_id)
28
- end
29
-
30
- def build_encode(job_details)
31
- return nil if job_details.nil?
32
- encode = ActiveEncode::Base.new(convert_input(job_details), convert_options(job_details))
33
- encode.id = job_details.body["job"]["id"].to_s
34
- encode.state = convert_state(get_job_state(job_details))
35
- job_progress = get_job_progress(encode.id)
36
- encode.current_operations = convert_current_operations(job_progress)
37
- encode.percent_complete = convert_percent_complete(job_progress, job_details)
38
- encode.created_at = job_details.body["job"]["created_at"]
39
- encode.updated_at = job_details.body["job"]["updated_at"]
40
- encode.errors = []
41
-
42
- encode.output = convert_output(job_details, job_progress)
43
-
44
- encode.input.id = job_details.body["job"]["input_media_file"]["id"].to_s
45
- encode.input.errors = convert_input_errors(job_details)
46
- tech_md = convert_tech_metadata(job_details.body["job"]["input_media_file"])
47
- [:width, :height, :frame_rate, :duration, :checksum, :audio_codec, :video_codec,
48
- :audio_bitrate, :video_bitrate, :file_size].each do |field|
49
- encode.input.send("#{field}=", tech_md[field])
50
- end
51
- encode.input.state = convert_state(job_details.body["job"]["input_media_file"]["state"])
52
- encode.input.created_at = job_details.body["job"]["input_media_file"]["created_at"]
53
- encode.input.updated_at = job_details.body["job"]["input_media_file"]["updated_at"]
54
-
55
- encode
56
- end
57
-
58
- def convert_state(state)
59
- case state
60
- when "assigning", "pending", "waiting", "processing" # Should there be a queued state?
61
- :running
62
- when "cancelled"
63
- :cancelled
64
- when "failed", "no_input"
65
- :failed
66
- when "finished"
67
- :completed
68
- end
69
- end
70
-
71
- def get_job_state(job_details)
72
- job_details.body["job"]["state"]
73
- end
74
-
75
- def convert_current_operations(job_progress)
76
- current_ops = []
77
- job_progress.body["outputs"].each { |output| current_ops << output["current_event"] unless output["current_event"].nil? }
78
- current_ops
79
- end
80
-
81
- def convert_percent_complete(job_progress, job_details)
82
- percent = job_progress.body["progress"]
83
- percent ||= 100 if convert_state(get_job_state(job_details)) == :completed
84
- percent ||= 0
85
- percent
86
- end
87
-
88
- def convert_input(job_details)
89
- job_details.body["job"]["input_media_file"]["url"]
90
- end
91
-
92
- def convert_options(_job_details)
93
- {}
94
- end
95
-
96
- def convert_output(job_details, job_progress)
97
- job_details.body["job"]["output_media_files"].collect do |o|
98
- output = ActiveEncode::Output.new
99
- output.id = o["id"].to_s
100
- output.label = o["label"]
101
- output.url = o["url"]
102
- output.errors = Array(o["error_message"])
103
-
104
- tech_md = convert_tech_metadata(o)
105
- [:width, :height, :frame_rate, :duration, :checksum, :audio_codec, :video_codec,
106
- :audio_bitrate, :video_bitrate, :file_size].each do |field|
107
- output.send("#{field}=", tech_md[field])
108
- end
109
- output_progress = job_progress.body["outputs"].find { |out_prog| out_prog["id"] = output.id }
110
- output.state = convert_state(output_progress["state"])
111
- output.created_at = o["created_at"]
112
- output.updated_at = o["updated_at"]
113
- output
114
- end
115
- end
116
-
117
- def convert_input_errors(job_details)
118
- Array(job_details.body["job"]["input_media_file"]["error_message"])
119
- end
120
-
121
- def convert_tech_metadata(media_file)
122
- return {} if media_file.nil?
123
-
124
- metadata = {}
125
- media_file.each_pair do |key, value|
126
- next if value.blank?
127
- case key
128
- when "md5_checksum"
129
- metadata[:checksum] = value
130
- when "format"
131
- metadata[:mime_type] = value
132
- when "duration_in_ms"
133
- metadata[:duration] = value
134
- when "audio_codec"
135
- metadata[:audio_codec] = value
136
- when "channels"
137
- metadata[:audio_channels] = value
138
- when "audio_bitrate_in_kbps"
139
- metadata[:audio_bitrate] = value
140
- when "video_codec"
141
- metadata[:video_codec] = value
142
- when "frame_rate"
143
- metadata[:frame_rate] = value
144
- when "video_bitrate_in_kbps"
145
- metadata[:video_bitrate] = value
146
- when "width"
147
- metadata[:width] = value
148
- when "height"
149
- metadata[:height] = value
150
- end
151
- end
152
- metadata
153
- end
154
- end
155
- end
156
- end