-
Notifications
You must be signed in to change notification settings - Fork 1
/
amv-uploader-class.rb
254 lines (207 loc) · 5.3 KB
/
amv-uploader-class.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
116
117
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
154
155
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require 'aws-sdk'
require 'logger'
require 'json'
require 'tempfile'
require_relative 'config'
# Base class for renderers
class AmvRendererBase
# Takes the name of the source file
def initialize(src)
@src = src
@log = Logger.new(STDOUT)
end
# Setup logger
def logger(log_obj)
@log = log_obj
end
# Ready to run?
def ready
false
end
# Get MIME Type
def type
'application/octet-string'
end
# Renders a file into another form
# Returns encoded file
def render
return nil
end
# Renders a thumbnail
def thumbnail
return nil
end
end
# MPEG4 Renderer thru ffmpeg
class MP4Renderer < AmvRendererBase
def ready
system "which #{$Settings[:path_to_handbrake]} >/dev/null 2>&1"
end
def render
tempfile_datestamp = Time.now.strftime("%Y-%m-%d-%H-%m-%s")
target_file = Tempfile.new(["amv-process--#{tempfile_datestamp}", ".mp4"])
@log.debug("Exporting to #{target_file.path}")
cmd = "#{$Settings[:path_to_handbrake]} -i \"#{@src}\" -o \"#{target_file.path}\" -e x264 -X 480 -q 30 -B 160 --optimize --verbose 0 >/dev/null 2>&1"
result = system cmd
@log.debug(result)
unless $Settings[:path_to_faststart].nil?
@log.debug("Running qtfaststart")
cmd = "#{$Settings[:path_to_faststart]} \"#{target_file.path}\" \"#{target_file.path}\" >/dev/null 2>&1"
result = system cmd
@log.debug(result)
end
return target_file
end
def type
'video/mp4'
end
def thumbnail(src)
length = 30
dimensions = "320x240"
unless $Settings[:path_to_ffprobe].nil?
cmd = "#{$Settings[:path_to_ffprobe]} -v quiet -print_format json -show_streams \"#{src.path}\""
json_result = `#{cmd}`
video_data = JSON.load(json_result)
if video_data.has_key? 'streams'
unless video_data['streams'].empty?
length = video_data['streams'][0]['duration'].to_f
dimensions = "#{video_data['streams'][0]['width']}x#{video_data['streams'][0]['height']}"
end
end
end
mark = (length / 2).to_i
unless $Settings[:path_to_ffmpeg].nil?
target_file = Tempfile.new([File.basename(src.path), '.jpg'])
cmd = "#{$Settings[:path_to_ffmpeg]} -i \"#{src.path}\" -vframes 1 -an -s #{dimensions} -ss #{mark} -y #{target_file.path} -loglevel 0"
result = system cmd
return target_file
end
nil
end
end
# Base class for uploaders
class AmvUploaderBase
# Takes the uploader params: username, password, whatever
def initialize(params)
@params = params
@log = Logger.new(STDOUT)
end
# Good to go?
def ready
false
end
# Setup logger
def logger(log_obj)
@log = log_obj
end
# Put a file
def put(src)
false
end
# Get URL
def get_url(obj)
obj.to_s
end
end
# Uploader to AWS
class AwsUploader < AmvUploaderBase
alias_method :initialize_super, :initialize
def initialize(params)
initialize_super(params)
AWS.config(
:s3_endpoint => @params[:endpoint],
:access_key_id => @params[:key],
:secret_access_key => @params[:secret]
)
end
def ready
true
end
def put(src, type, filename = nil)
if filename.nil?
filename = File.basename(src.path)
end
@log.debug("Uploading #{src} to AWS as #{filename}")
s3 = AWS::S3.new
bucket = s3.buckets[@params[:bucket]]
obj = bucket.objects.create(filename, {
:data => src,
:acl => :public_read,
:content_type => type
})
@log.debug(obj.key)
obj
end
def get_url(obj)
obj.public_url
end
end
# Uploader container
class AmvUploader
attr_accessor :src, :dst, :remote, :thumbnail
# Sets up src and other stuff
def initialize(params)
@errors = []
@src = params[:src]
@log = Logger.new(STDOUT)
@type = nil
end
# Set logger
def logger(log_obj)
@log = log_obj
end
# Convert and upload
def process(renderClass, uploadClass, uploadParams)
@dst = render(renderClass)
if @dst != false
@remote = upload(uploadClass, uploadParams)
return @remote
end
false
end
# Convert
def render(className)
cls = Object.const_get(className)
if cls < AmvRendererBase
@renderer = cls.new(@src)
unless @renderer.ready == true
@log.fatal("Renderer has not been configured correctly.")
return false
end
@renderer.logger(@log)
@type = @renderer.type
output = @renderer.render
@thumbnail = @renderer.thumbnail(output)
return output
end
false
end
# Upload
def upload(className, params)
if @dst == false
return false
end
cls = Object.const_get(className)
if cls < AmvUploaderBase
@uploader = cls.new(params)
unless @uploader.ready == true
@log.fatal("Uploader has not been configured correctly.")
return false
end
@uploader.logger(@log)
obj = @uploader.put(@dst, @type)
video_url = @uploader.get_url(obj)
@log.info("Uploaded video to #{video_url}")
unless @thumbnail.nil?
thumbnail_obj = @uploader.put(@thumbnail, 'image/jpeg', "#{obj.key}.jpg")
thumbnail_url = @uploader.get_url(thumbnail_obj)
@log.info("Uploaded thumbnail to #{thumbnail_url}")
end
return video_url
end
false
end
def errors
@errors
end
end