1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class validate input assets and get metadata about them |
5
|
|
|
* It makes sure the input files to be transcoded exists and is valid. |
6
|
|
|
* Based on the input file type we lunch the proper transcoder |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
require_once __DIR__.'/BasicActivity.php'; |
10
|
|
|
|
11
|
|
|
use Guzzle\Http\EntityBody; |
12
|
|
|
use SA\CpeSdk; |
13
|
|
|
|
14
|
|
|
class ValidateAssetActivity extends BasicActivity |
15
|
|
|
{ |
16
|
|
|
/** @var \finfo */ |
17
|
|
|
private $finfo; |
18
|
|
|
|
19
|
|
|
public function __construct($params, $debug, $cpeLogger = null) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($params, $debug, $cpeLogger); |
22
|
|
|
$this->finfo = new \finfo(FILEINFO_MIME_TYPE); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Perform the activity |
26
|
|
|
public function do_activity($task) |
27
|
|
|
{ |
28
|
|
|
$this->cpeLogger->log_out( |
29
|
|
|
"INFO", |
30
|
|
|
basename(__FILE__), |
31
|
|
|
"Preparing Asset validation ...", |
32
|
|
|
$this->activityLogKey |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
// Call parent do_activity: |
36
|
|
|
// It download the input file we will process. |
37
|
|
|
parent::do_activity($task); |
38
|
|
|
|
39
|
|
|
// Fetch first 1 KiB of file |
40
|
|
|
$this->send_heartbeat($task); |
41
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'ct'); |
42
|
|
|
$obj = $this->s3->getObject([ |
43
|
|
|
'Bucket' => $this->input->{'input_asset'}->{'bucket'}, |
44
|
|
|
'Key' => $this->input->{'input_asset'}->{'file'}, |
45
|
|
|
'Range' => 'bytes=0-1024' |
46
|
|
|
]); |
47
|
|
|
$this->send_heartbeat($task); |
48
|
|
|
|
49
|
|
|
// Determine file type |
50
|
|
|
file_put_contents($tmpFile, (string) $obj['Body']); |
51
|
|
|
$mime = trim((new CommandExecuter($this->cpeLogger))->execute( |
52
|
|
|
'file -b --mime-type ' . escapeshellarg($tmpFile))['out']); |
53
|
|
|
$type = substr($mime, 0, strpos($mime, '/')); |
54
|
|
|
|
55
|
|
|
$this->cpeLogger->log_out( |
56
|
|
|
"DEBUG", |
57
|
|
|
basename(__FILE__), |
58
|
|
|
"File meta information gathered. Mime: $mime | Type: $type", |
59
|
|
|
$this->activityLogKey |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
// Load the right transcoder base on input_type |
63
|
|
|
// Get asset detailed info |
64
|
|
|
switch ($type) |
65
|
|
|
{ |
66
|
|
|
case 'audio': |
67
|
|
|
case 'video': |
68
|
|
|
case 'image': |
69
|
|
|
default: |
70
|
|
|
require_once __DIR__.'/transcoders/VideoTranscoder.php'; |
71
|
|
|
|
72
|
|
|
// Initiate transcoder obj |
73
|
|
|
$videoTranscoder = new VideoTranscoder($this, $task); |
74
|
|
|
// Get input video information |
75
|
|
|
$assetInfo = $videoTranscoder->get_asset_info($this->pathToInputFile); |
76
|
|
|
|
77
|
|
|
// Liberate memory |
78
|
|
|
unset($videoTranscoder); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($mime === 'application/octet-stream' && isset($asset['streams'])) { |
|
|
|
|
82
|
|
|
// Check all stream types |
83
|
|
|
foreach ($asset['streams'] as $stream) { |
84
|
|
|
if ($stream['codec_type'] === 'video') { |
85
|
|
|
// For a video type, set type to video and break |
86
|
|
|
$type = 'video'; |
87
|
|
|
break; |
88
|
|
|
} elseif ($stream['codec_type'] === 'audio') { |
89
|
|
|
// For an audio type, set to audio, but don't break |
90
|
|
|
// in case there's a video stream later |
91
|
|
|
$type = 'audio'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$assetInfo->mime = $mime; |
97
|
|
|
$assetInfo->type = $type; |
98
|
|
|
|
99
|
|
|
return $assetInfo; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.