Completed
Push — master ( 99ade0...004bdb )
by Nicolas
04:25 queued 02:00
created

BasicTranscoder::get_asset_info()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 40
rs 8.5806
cc 4
eloc 26
nc 4
nop 1
1
<?php
2
3
/**
4
 * Interface for any transcoder
5
 * You must extend this class to create a new transcoder
6
 */
7
8
require_once __DIR__."/../../../vendor/autoload.php";
9
10
require_once __DIR__.'/../../utils/S3Utils.php';
11
12
use SA\CpeSdk;
13
14
class BasicTranscoder 
15
{
16
    public $activityLogKey; // Valling activity loggin key
17
    public $activityObj; // Calling activity object
18
    public $task; // Activity TASK
19
20
    public $cpeLogger; // Logger
21
    public $cpeSqsWriter; // SQS write for sending msgs to client
22
    public $cpeJsonValidator; // SQS write for sending msgs to client
23
    public $s3Utils; // Used to manipulate S3
24
    public $executer; // Executer obj
25
26
    const EXEC_VALIDATE_FAILED  = "EXEC_VALIDATE_FAILED";
27
    const TRANSCODE_FAIL        = "TRANSCODE_FAIL";
28
    
29
    // Types
30
    const VIDEO = "VIDEO";
31
    const THUMB = "THUMB";
32
    const AUDIO = "AUDIO";
33
    const DOC   = "DOC";
34
    const IMAGE = "IMAGE";
35
    
36
    public function __construct($activityObj, $task) 
37
    { 
38
        $this->activityObj      = $activityObj;
39
        $this->activityLogKey   = $activityObj->activityLogKey;
40
        $this->task             = $task;
41
42
        $this->cpeLogger        = $activityObj->cpeLogger;
43
        $this->cpeSqsWriter     = $activityObj->cpeSqsWriter;
44
        $this->cpeJsonValidator = $activityObj->cpeJsonValidator;
45
        $this->executer         = new CommandExecuter($activityObj->cpeLogger);
46
        $this->s3Utils          = new S3Utils($activityObj->cpeLogger);
47
    }
48
49
    public function is_dir_empty($dir)
50
    {
51
        if (!is_readable($dir)) return null; 
52
        $handle = opendir($dir);
53
        while (false !== ($entry = readdir($handle))) {
54
            if ($entry !== '.' && $entry !== '..') { 
55
                return false;
56
            }
57
        }
58
        closedir($handle); 
59
        return true;
60
    }
61
62
    
63
    /**************************************
64
     * GET ASSET METADATA INFO
65
     * The methods below are used to run ffprobe on assets
66
     * We capture as much info as possible on the input asset
67
     */
68
69
    // Execute FFPROBE to get asset information
70
    public function get_asset_info($pathToInputFile)
71
    {
72
        $pathToInputFile = escapeshellarg($pathToInputFile);
73
        $ffprobeCmd = "ffprobe -v quiet -of json -show_format -show_streams $pathToInputFile";
74
        try {
75
            // Execute FFMpeg to validate and get information about input video
76
            $out = $this->executer->execute(
77
                $ffprobeCmd,
78
                1, 
79
                array(
80
                    1 => array("pipe", "w"),
81
                    2 => array("pipe", "w")
82
                ),
83
                false, false, 
84
                false, 1
85
            );
86
        }
87
        catch (\Exception $e) {
88
            $this->cpeLogger->log_out(
89
                "ERROR", 
90
                basename(__FILE__), 
91
                "Execution of command '".$ffprobeCmd."' failed.",
92
                $this->activityLogKey
93
            );
94
            return false;
95
        }
96
        
97
        if (empty($out)) {
98
            throw new CpeSdk\CpeException("Unable to execute FFProbe to get information about '$pathToInputFile'!",
99
                self::EXEC_VALIDATE_FAILED);
100
        }
101
        
102
        // FFmpeg writes on STDERR ...
103
        if (!($assetInfo = json_decode($out['out']))) {
104
            throw new CpeSdk\CpeException("FFProbe returned invalid JSON!",
105
                self::EXEC_VALIDATE_FAILED);
106
        }
107
        
108
        return ($assetInfo);
109
    }
110
}