BasicTranscoder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B isDirEmpty() 0 12 5
B getAssetInfo() 0 40 4
1
<?php
2
3
/*
4
 *   Base class for all transcoders. 
5
 *   You must extend this class to create a new transcoder
6
 *
7
 *   Copyright (C) 2016  BFan Sports - Sport Archive Inc.
8
 *
9
 *   This program is free software; you can redistribute it and/or modify
10
 *   it under the terms of the GNU General Public License as published by
11
 *   the Free Software Foundation; either version 2 of the License, or
12
 *   (at your option) any later version.
13
 *
14
 *   This program is distributed in the hope that it will be useful,
15
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *   GNU General Public License for more details.
18
 *
19
 *   You should have received a copy of the GNU General Public License along
20
 *   with this program; if not, write to the Free Software Foundation, Inc.,
21
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
 */
23
24
require_once __DIR__."/../../../vendor/autoload.php";
25
require_once __DIR__.'/../../utils/S3Utils.php';
26
27
use SA\CpeSdk;
28
29
class BasicTranscoder 
30
{
31
    public $activityObj; // Calling activity object
32
    public $task;        // Activity TASK
33
    public $logKey;      // Valling activity loggin key
34
35
    public $cpeLogger;   // Logger
36
    public $s3Utils;     // Used to manipulate S3
37
    public $executer;    // Executer obj
38
39
    const EXEC_VALIDATE_FAILED  = "EXEC_VALIDATE_FAILED";
40
    const TRANSCODE_FAIL        = "TRANSCODE_FAIL";
41
    
42
    // Types
43
    const VIDEO = "VIDEO";
44
    const THUMB = "THUMB";
45
    const AUDIO = "AUDIO";
46
    const DOC   = "DOC";
47
    const IMAGE = "IMAGE";
48
    
49
    public function __construct($activityObj, $task) 
50
    { 
51
        $this->activityObj      = $activityObj;
52
        $this->logKey           = $activityObj->logKey;
53
        $this->task             = $task;
54
55
        $this->cpeLogger        = $activityObj->cpeLogger;
56
        $this->executer         = new CommandExecuter($activityObj->cpeLogger, $this->logKey);
57
        $this->s3Utils          = new S3Utils($activityObj->cpeLogger);
58
    }
59
60
    public function isDirEmpty($dir)
61
    {
62
        if (!is_readable($dir)) return null; 
63
        $handle = opendir($dir);
64
        while (false !== ($entry = readdir($handle))) {
65
            if ($entry !== '.' && $entry !== '..') { 
66
                return false;
67
            }
68
        }
69
        closedir($handle); 
70
        return true;
71
    }
72
73
    
74
    /**************************************
75
     * GET ASSET METADATA INFO
76
     * The methods below are used to run ffprobe on assets
77
     * We capture as much info as possible on the input asset
78
     */
79
80
    // Execute FFPROBE to get asset information
81
    public function getAssetInfo($inputFilePath)
82
    {
83
        $inputFilePath = escapeshellarg($inputFilePath);
84
        $ffprobeCmd = "ffprobe -v quiet -of json -show_format -show_streams $inputFilePath";
85
        try {
86
            // Execute FFMpeg to validate and get information about input video
87
            $out = $this->executer->execute(
88
                $ffprobeCmd,
89
                1, 
90
                array(
91
                    1 => array("pipe", "w"),
92
                    2 => array("pipe", "w")
93
                ),
94
                false, false, 
95
                false, 1
96
            );
97
        }
98
        catch (\Exception $e) {
99
            $this->cpeLogger->logOut(
100
                "ERROR", 
101
                basename(__FILE__), 
102
                "Execution of command '".$ffprobeCmd."' failed.",
103
                $this->activityLogKey
0 ignored issues
show
Bug introduced by
The property activityLogKey does not seem to exist. Did you mean logKey?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
104
            );
105
            return false;
106
        }
107
        
108
        if (empty($out)) {
109
            throw new CpeSdk\CpeException("Unable to execute FFProbe to get information about '$inputFilePath'!",
110
                self::EXEC_VALIDATE_FAILED);
111
        }
112
        
113
        // FFmpeg writes on STDERR ...
114
        if (!($assetInfo = json_decode($out['out']))) {
115
            throw new CpeSdk\CpeException("FFProbe returned invalid JSON!",
116
                self::EXEC_VALIDATE_FAILED);
117
        }
118
        
119
        return ($assetInfo);
120
    }
121
}