Completed
Branch master (78c2af)
by Pierre-Henry
52:17 queued 17:37
created

_protected/framework/Video/Video.class.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @title            Video Class
4
 * @desc             Class is used to create/manipulate videos using FFmpeg.
5
 *
6
 * @author           Pierre-Henry Soria <[email protected]>
7
 * @copyright        (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
8
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
9
 * @package          PH7 / Framework / Video
10
 * @link             http://ph7cms.com
11
 */
12
13
namespace PH7\Framework\Video;
14
15
defined('PH7') or exit('Restricted access');
16
17
use PH7\Framework\Date\Various;
18
use PH7\Framework\Config\Config;
19
use PH7\Framework\File\File;
20
use PH7\Framework\File\Upload;
21
use PH7\Framework\File\TooLargeException;
22
use PH7\Framework\File\MissingProgramException;
23
24
class Video extends Upload
25
{
26
    private $oFile, $sType, $sFfmpegPath, $aFile;
27
28
    /**
29
     * @var $aAllowedTypes File formats supported.
30
     */
31
    private $aAllowedTypes = [
32
        'video/mov',
33
        'video/avi',
34
        'video/flv',
35
        'video/mp4',
36
        'video/mpg',
37
        'video/mpeg',
38
        'video/wmv',
39
        'video/ogg',
40
        'video/ogv',
41
        'video/webm',
42
        'video/mkv'
43
    ];
44
45
    /**
46
     * @param array $aFile Example: $_FILES['video']
47
     *
48
     * @throws MissingProgramException If FFmpeg is not installed.
49
     */
50
    public function __construct($aFile)
51
    {
52
        $this->oFile = new File;
53
        $this->sFfmpegPath = Config::getInstance()->values['video']['handle.ffmpeg_path'];
54
55
        if (!file_exists($this->sFfmpegPath))
56
        {
57
            $sMsg = t('FFmpeg is not installed on the server or the path cannot be found. Please install and configure the path in "~/YOUR-PROTECTED-FOLDER/app/configs/config.ini" or contact the administrator of the site/server or Web host by saying the problem.');
58
            throw new MissingProgramException($sMsg);
59
        }
60
61
        $this->aFile = $aFile;
62
        $this->sType = $this->aFile['type'];
63
64
        /** Attributes from "Upload" abstract class **/
65
        $this->sMaxSize = Config::getInstance()->values['video']['upload.max_size'];
66
        $this->iFileSize = (int) $this->aFile['size'];
67
    }
68
69
    /**
70
     * @throws TooLargeException If the video file is not found.
71
     *
72
     * @return bool
73
     */
74
    public function validate()
75
    {
76
        if (!is_uploaded_file($this->aFile['tmp_name'])) {
77
            if (!isDebug()) {
78
                return false;
79
            } else {
80
                throw new TooLargeException('The file could not be uploaded. Possibly too large.');
81
            }
82
        } else {
83
            return in_array($this->sType, $this->aAllowedTypes);
84
        }
85
    }
86
87
    /**
88
     * @param string $sFile
89
     *
90
     * @return bool
91
     */
92
    public function save($sFile)
93
    {
94
        return move_uploaded_file($this->aFile['tmp_name'], $sFile);
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getFileName()
101
    {
102
        return $this->aFile['name'];
103
    }
104
105
    /**
106
     * Convert video file and the extension video type.
107
     *
108
     * @param string $sFile.
0 ignored issues
show
There is no parameter named $sFile.. Did you maybe mean $sFile?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
109
     *
110
     * @return string The new name that you entered in the parameter of this method.
111
     */
112
    public function rename($sFile)
113
    {
114
        $sParams = ''; // By default, we don't use parameter
115
116
        $sType = $this->oFile->getFileExt($sFile); // Get the new format
117
        if ($sType == 'mp4')
118
            $sParams = '-c copy -copyts';
119
120
        exec("$this->sFfmpegPath -i {$this->aFile['tmp_name']} $sParams $sFile");
121
        return $sFile;
122
    }
123
124
    /**
125
     * Generate a thumbnail with FFmpeg.
126
     *
127
     * @param string $sPicturePath
128
     * @param integer $iWidth
129
     * @param integer $iHeight
130
     *
131
     * @return string The thumbnail file that you entered in the parameter of this method.
132
     */
133
    public function thumbnail($sPicturePath, $iSeconds, $iWidth, $iHeight)
134
    {
135
        exec($this->sFfmpegPath . ' -itsoffset -' . $iSeconds . ' -i ' . $this->aFile['tmp_name'] . '  -vcodec mjpeg -vframes 1 -an -f rawvideo -s ' . $iWidth . 'x' . $iHeight . ' ' . $sPicturePath);
136
        return $sPicturePath;
137
    }
138
139
    /**
140
     * Gets video duration.
141
     *
142
     * @return integer Seconds.
143
     */
144
    public function getDuration()
145
    {
146
         $sTime = exec($this->sFfmpegPath . ' -i ' . $this->aFile['tmp_name'] . ' 2>&1 | grep "Duration" | cut -d \' \' -f 4 | sed s/,//');
147
         return Various::timeToSec($sTime);
148
     }
149
150
    /**
151
     * Get Type Video File.
152
     *
153
     * @return string The extension of the video without the dot.
154
     */
155
    public function getExt()
156
    {
157
        return $this->sType;
158
    }
159
160
    /**
161
     * Destruction of attributes and temporary file.
162
     */
163
    public function __destruct()
164
    {
165
        // If it exists, delete the temporary video
166
        $this->oFile->deleteFile($this->aFile['tmp_name']);
167
168
        unset(
169
            $this->oFile,
170
            $this->sType,
171
            $this->sFfmpegPath,
172
            $this->aFile
173
        );
174
    }
175
}
176