forked from Sammaye/aws_worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.php
More file actions
361 lines (292 loc) · 12 KB
/
encoder.php
File metadata and controls
361 lines (292 loc) · 12 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
require_once 'aws/sdk.class.php';
define('ROOT', dirname(__FILE__));
global $s3;
global $sqs;
global $args;
$args = getArgs($_SERVER['argv']);
$s3 = new AmazonS3(array(
'key' => $args['aws_key'],
'secret' => $args['aws_secret']
));
$sqs = new AmazonSQS(array(
'key' => $args['aws_key'],
'secret' => $args['aws_secret']
));
$input_file_name = ROOT."/input_file";
if($args['output_format'] == 'mp4'){
$output_file_name = ROOT."/randomoutput.mp4";
$output_temp_file = ROOT."/random_output_tmp.mp4";
}else{
$output_file_name = ROOT."/randomoutput.ogv";
}
$output_thumbnail_name = ROOT."/image_output_file.png";
cURL_file($s3->get_object_url($args['bucket'], $args['input_file']));
if((!file_exists($input_file_name)) || filesize($input_file_name) <= 1){
/*
* If the file does not exist or is only 1 byte long (long enough for headers but not much else) then die
*/
send_SQS(false, array('reason' => 'Failed to download correctly'));
}
exec("ffprobe -show_format -show_streams $input_file_name", $output); // Let's butt probe this file to find out if it's valid
//print_r($output);
// Now lets get the info we need to validate and get duration
if(!empty($output)){
$stringify_output = implode('\n', $output); // Lets join the output together with \n so we know the difference in lines
$stream_section = magic_substr($stringify_output, '[STREAM]', '[/STREAM]'); // Lets get A STREAM section (notice the capital A)
$format_section = magic_substr($stringify_output, '[FORMAT]', '[/FORMAT]'); // Lets extract a FORMAT section, if one
/*
* If we don't have a FORMAT or STREAM section something must be terribly wrong
*/
if(strlen($format_section) <= 0 || strlen($stream_section) <= 0){
send_SQS(false, array('reason' => 'Failed pre encoding checks: malformed ffprobe output'));
}
$duration = 0;
if(preg_match('/duration=[0-9]+\.[0-9]+/', $format_section, $matches) > 0){ // Look for a duration within it all
if(sizeof($matches) <= 0)
send_SQS(false, array('reason' => 'Failed pre encoding checks: duration')); // No duration is another problem
$duration = preg_replace('/duration=/', '', $matches[0]); // Let's get the actual duration (i.e. 5.0998)
if($duration <= 0 || preg_match('/[^0-9\.]+/', $duration) > 0){ // If duration is less than or equal to 0 or it contains anyhting but numbers and dots (i.e. 9.5432)
send_SQS(false, array('reason' => 'Failed pre encoding checks: duration'));
}
}
}else{
send_SQS(false, array('reason' => 'Failed pre encoding checks: no ffmpeg output'));
}
/**
* OK so the file passed initial tests
*
* LETS ENCODE!!!!
*/
if($args['output_format'] == 'mp4'){
$command = "ffmpeg -i $input_file_name -vcodec libx264 -r 100 -bt 300k -ac 2 -ar 48000 -ab 192k -strict -2 -y $output_temp_file 2>&1";
}elseif($args['output_format'] == 'ogv'){
$command = "ffmpeg -i $input_file_name -s 640:480 -acodec libvorbis -vcodec libtheora -aspect 4:3 -r 20 -qscale 6 -ac 2 -ab 80k -ar 44100 -y $output_file_name 2>&1";
}
exec($command, $encoding_output); //-s 640:480 -aspect 4:3 -r 65535/2733 -qscale 5 -ac 2 -ar 48000 -ab 192k
if($args['output_format'] == 'mp4')
exec("qt-faststart $output_temp_file $output_file_name");
echo "The command ran was: ".$command;
//var_dump($encoding_output);
$encoding_output_string = $encoding_output;
if(is_array($encoding_output)){
$encoding_output_string = implode('\n', $encoding_output);
}
if(preg_match('/Error while opening encoder/', $encoding_output_string) > 0){
send_SQS(false, array('reason' => 'Error While trying to encode', 'output' => $encoding_output_string)); // It means in undeniable error happened in encoding
}
/**
* Now lets see if it validates and if it does lets put the finishing touches on
*/
if(validate_video($output_file_name)){
/*
* From our duration we got earlier lets get a random second between 0 and max second without rounding and check the image file is real by chekcing its size is greater
* than 0
*/
preg_match('/^[0-9]+/', $duration, $matches); // Lets get a strict int of the duration
// Lets try it 5 times else we will just get first frame else die mofo
for($i=0;$i<5;$i++){
if($i < 4){
/*
* For the first 4 tries lets get a random image
*/
$int_duration = rand(0, $matches[0] > 600 ? 600 : $matches[0]); // Let's limit the thumb time span by 10 mins cos else it takes a while
exec("ffmpeg -itsoffset -$int_duration -i $input_file_name -r 100 -vcodec png -vframes 1 -an -f rawvideo -s 640x480 -y $output_thumbnail_name");
if(file_exists($output_thumbnail_name) && filesize($output_thumbnail_name) > 0){ break; } // If we've got our image lets carry on
}else{
/*
* Last ditch attempt, get the first second
*/
exec("ffmpeg -itsoffset -1 -i $input_file_name -r 100 -vcodec png -vframes 1 -an -f rawvideo -s 640x480 -y $output_thumbnail_name");
if(!file_exists($output_thumbnail_name) || filesize($output_thumbnail_name) <= 0){
echo "died on images";
send_SQS(false, array('reason' => 'Could not ascertain an image')); // We couldn't seem to get an image for this
}else{
break;
}
}
}
/*
* Now lets recursively upload the video and its thumbnail back to S3 like good boys
*/
$v_upload_response = $s3->create_object($args['bucket'], pathinfo($output_file_name, PATHINFO_BASENAME), array(
'acl' => AmazonS3::ACL_PUBLIC,
'storage' => AmazonS3::STORAGE_REDUCED,
'fileUpload' => $output_file_name
));
$failed = false;
if($args['output_format'] == 'mp4'){
$img_upload_response = $s3->create_object($args['bucket'], pathinfo($output_thumbnail_name, PATHINFO_BASENAME), array(
'acl' => AmazonS3::ACL_PUBLIC,
'storage' => AmazonS3::STORAGE_REDUCED,
'fileUpload' => $output_thumbnail_name
));
if($v_upload_response->isOK() && $img_upload_response->isOK()){ }else{
$failed = true;
}
}else{
if($v_upload_response->isOK()){ }else{
$failed = true;
}
}
// If they uploaded fine lets cURL a success containing the possible URLs etc
if(!$failed){
echo "everything went ok";
send_SQS(true, array(
'url' => $s3->get_object_url($args['bucket'], pathinfo($output_file_name, PATHINFO_BASENAME)),
'thumbnail' => $args['output_format'] == 'mp4' ? $s3->get_object_url($args['bucket'], pathinfo($output_thumbnail_name, PATHINFO_BASENAME)) : '',
'duration' => (int)($duration*1000)
));
}else{
echo "Shit coluldn't upload";
send_SQS(false, array('reason' => 'Could not upload', 'v_upload' => $v_upload_response->isOK(),
'img_upload' => $args['output_format'] == 'mp4' ? $img_upload_response->isOK() : true)); /* FAIL */
}
}else{
send_SQS(false, array('reason' => 'Failed checks')); /* FAIL */
}
/**
* GLOBAL FUNCTIONS
*/
/**
* GETARGS
* @author Patrick Fisher <patrick@pwfisher.com>
*/
function getArgs($argv){
array_shift($argv);
$out = array();
foreach ($argv as $arg){
// --foo --bar=baz
if (substr($arg,0,2) == '--'){
$eqPos = strpos($arg,'=');
// --foo
if ($eqPos === false){
$key = substr($arg,2);
$value = isset($out[$key]) ? $out[$key] : true;
$out[$key] = $value;
}
// --bar=baz
else {
$key = substr($arg,2,$eqPos-2);
$value = substr($arg,$eqPos+1);
$out[$key] = $value;
}
}
// -k=value -abc
else if (substr($arg,0,1) == '-'){
// -k=value
if (substr($arg,2,1) == '='){
$key = substr($arg,1,1);
$value = substr($arg,3);
$out[$key] = $value;
}
// -abc
else {
$chars = str_split(substr($arg,1));
foreach ($chars as $char){
$key = $char;
$value = isset($out[$key]) ? $out[$key] : true;
$out[$key] = $value;
}
}
}
// plain-arg
else {
$value = $arg;
$out[] = $value;
}
}
return $out;
}
function validate_video($output_file_name, $output){
global $args;
if((!file_exists($output_file_name)) || filesize($output_file_name) <= 1){
/*
* If the file does not exist or is only 1 byte long (long enough for headers but not much else) then die
*/
send_SQS(false, array('reason' => 'Failed post encoding checks: File not exist'));
}
exec("ffprobe -show_format -show_streams $output_file_name", $ffprobe_output);
exec("ffmpeg -i $output_file_name 2>&1", $ffmpeg_output);
if(empty($ffprobe_output) || empty($ffmpeg_output)){
/*
* If the output is empty then it had problems opening the file for inspection
*/
send_SQS(false, array('reason' => 'Failed post encoding checks: No ffmpeg output'));
}
/*
* Now lets check for a STREAM (just one) and a FORMAT and check inside to see if they are OK
*/
$stringify_output = implode('\n', $ffprobe_output); // Lets join the output together with \n so we know the difference in lines
$stream_section = magic_substr($stringify_output, '[STREAM]', '[/STREAM]'); // Lets get A STREAM section (notice the capital A)
$format_section = magic_substr($stringify_output, '[FORMAT]', '[/FORMAT]'); // Lets extract a FORMAT section, if one
/*
* If we don't have a FORMAT or STREAM section something must be terribly wrong
*/
if(strlen($format_section) <= 0 || strlen($stream_section) <= 0){
send_SQS(false, array('reason' => 'Failed post encoding checks: Malformed ffprobe'));
}
// Now lets test for a duration to our file then we can finally test for the codecs used
$duration = 0;
if(preg_match('/duration=[0-9]+\.[0-9]+/', $format_section, $matches) > 0){ // Look for a duration within it all
if(sizeof($matches) <= 0)
send_SQS(false, array('reason' => 'Failed post encoding checks: Duration')); // No duration is another problem
$duration = preg_replace('/duration=/', '', $matches[0]); // Let's get the actual duration (i.e. 5.0998)
if($duration <= 0 || preg_match('/[^0-9\.]+/', $duration) > 0){ // If duration is less than 0 or it contains anyhting but numbers and dots (i.e. 9.5432)
send_SQS(false, array('reason' => 'Failed post encoding checks: Duration'));
}
/*
* It has got a duration obviously so lets check for codecs
*/
}
if($args['output_format'] == 'mp4'){
$audio_codec = 'aac';
$video_codec = 'h264';
}elseif($args['output_format'] == 'ogv'){
$audio_codec = 'theora';
$video_codec = 'vorbis';
}
if(preg_match("/codec_name=$audio_codec/", $stringify_output) > 0){ }else{ send_SQS(false, array('reason' => 'Failed post encoding checks: Codec')); }
if(preg_match("/codec_name=$video_codec/", $stringify_output) > 0){ }else{ send_SQS(false, array('reason' => 'Failed post encoding checks: Codec')); }
/*
* MY GOD! It might actually be a real file!
*/
return true;
}
function cURL_file($url){
set_time_limit(0);
$fp = fopen (dirname(__FILE__)."/input_file", 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 75);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
function magic_substr($string,$from,$to) {
if(preg_match("!".preg_quote($from)."(.*?)".preg_quote($to)."!",$string,$m)) {
return $m[1];
}
return '';
}
function send_SQS($success, $fields = array()){
global $sqs;
global $args;
$response = $sqs->send_message($args['output_queue'], json_encode(array_merge(array(
'id' => $args['id'],
'output_format' => $args['output_format'],
'input_file' => $args['input_file'],
'input_queue' => $args['input_queue'],
'bucket' => $args['bucket'],
'time_started' => $args['time_started'],
'time_taken' => microtime(true) - $args['time_started'],
'time_sent' => date('d-m-Y H:i:s'),
'success' => $success
), $fields)));
if($response->isOk()){}
exit(); // Send SQS is a one way ticket....a ticket to HELL
}
/*
* Now lets exit to ensure no further processing is completed (not really needed but meh)
*/
exit();