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 SA\CpeSdk; |
12
|
|
|
|
13
|
|
|
class ValidateAssetActivity extends BasicActivity |
14
|
|
|
{ |
15
|
|
|
/** @var \finfo */ |
16
|
|
|
private $finfo; |
17
|
|
|
|
18
|
|
|
public function __construct($params, $debug, $cpeLogger = null) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($params, $debug, $cpeLogger); |
21
|
|
|
$this->finfo = new \finfo(FILEINFO_MIME_TYPE); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// Perform the activity |
25
|
|
|
public function do_activity($task) |
26
|
|
|
{ |
27
|
|
|
$this->cpeLogger->log_out( |
28
|
|
|
"INFO", |
29
|
|
|
basename(__FILE__), |
30
|
|
|
"Preparing Asset validation ...", |
31
|
|
|
$this->activityLogKey |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
// Call parent do_activity: |
35
|
|
|
// It download the input file we will process. |
36
|
|
|
parent::do_activity($task); |
37
|
|
|
|
38
|
|
|
// Fetch first 1 KiB of file |
39
|
|
|
$this->send_heartbeat($task); |
40
|
|
|
$obj = $this->s3->getObject([ |
41
|
|
|
'Bucket' => $this->input->{'input_asset'}->{'bucket'}, |
42
|
|
|
'Key' => $this->input->{'input_asset'}->{'file'}, |
43
|
|
|
'Range' => '0-10240' |
44
|
|
|
]); |
45
|
|
|
$this->send_heartbeat($task); |
46
|
|
|
|
47
|
|
|
// Determine file type |
48
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'ct'); |
49
|
|
|
file_put_contents($tmpFile, $obj['Body']); |
50
|
|
|
$mime = trim((new CommandExecuter($this->cpeLogger))->execute( |
51
|
|
|
'file -b --mime-type ' . escapeshellarg($tmpFile))['out']); |
52
|
|
|
$type = substr($mime, 0, strpos($mime, '/')); |
53
|
|
|
|
54
|
|
|
$this->cpeLogger->log_out( |
55
|
|
|
"DEBUG", |
56
|
|
|
basename(__FILE__), |
57
|
|
|
"File meta information gathered. Mime: $mime | Type: $type", |
58
|
|
|
$this->activityLogKey |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
// Load the right transcoder base on input_type |
62
|
|
|
// Get asset detailed info |
63
|
|
|
switch ($type) |
64
|
|
|
{ |
65
|
|
|
case 'audio': |
66
|
|
|
case 'video': |
67
|
|
|
case 'image': |
68
|
|
|
default: |
69
|
|
|
require_once __DIR__.'/transcoders/VideoTranscoder.php'; |
70
|
|
|
|
71
|
|
|
// Initiate transcoder obj |
72
|
|
|
$videoTranscoder = new VideoTranscoder($this, $task); |
73
|
|
|
// Get input video information |
74
|
|
|
$assetInfo = $videoTranscoder->get_asset_info($this->pathToInputFile); |
75
|
|
|
$assetInfo->mime = $mime; |
76
|
|
|
$assetInfo->type = $type; |
77
|
|
|
|
78
|
|
|
// Liberate memory |
79
|
|
|
unset($videoTranscoder); |
80
|
|
|
|
81
|
|
|
return $assetInfo; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|