Passed
Push — master ( 20660a...ab558f )
by Amin
02:37
created

FFMpeg   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 45
dl 0
loc 178
rs 10
c 1
b 1
f 1
wmc 17

10 Methods

Rating   Name   Duplication   Size   Complexity  
A fromS3() 0 10 1
A __construct() 0 3 1
A openFromCloud() 0 15 5
A create() 0 3 1
A fromMAS() 0 9 1
A isTmp() 0 10 2
A open() 0 10 3
A fromGCS() 0 9 1
A fromURL() 0 11 1
A __call() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Streaming;
13
14
use FFMpeg\Exception\ExceptionInterface;
15
use FFMpeg\FFMpeg as BFFMpeg;
16
use FFMpeg\FFProbe;
17
use Psr\Log\LoggerInterface;
18
use Streaming\Clouds\AWS;
19
use Streaming\Clouds\Cloud;
20
use Streaming\Clouds\CloudInterface;
21
use Streaming\Clouds\GoogleCloudStorage;
22
use Streaming\Clouds\MicrosoftAzure;
23
use Streaming\Exception\Exception;
24
use Streaming\Exception\InvalidArgumentException;
25
use Streaming\Exception\RuntimeException;
26
27
class FFMpeg
28
{
29
    /** @var BFFMpeg */
30
    protected $ffmpeg;
31
32
    /**
33
     * @param $ffmpeg
34
     */
35
    public function __construct(BFFMpeg $ffmpeg)
36
    {
37
        $this->ffmpeg = $ffmpeg;
38
    }
39
40
    /**
41
     * @param string $path
42
     * @param bool $is_tmp
43
     * @return Media
44
     */
45
    public function open(string $path, bool $is_tmp = false): Media
46
    {
47
        if (!is_file($path)) {
48
            throw new InvalidArgumentException("There is no file in this path: " . $path);
49
        }
50
51
        try {
52
            return new Media($this->ffmpeg->open($path), $path, $is_tmp);
53
        } catch (ExceptionInterface $e) {
54
            throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e);
55
        }
56
    }
57
58
    /**
59
     * @param array $cloud
60
     * @param string|null $save_to
61
     * @return Media
62
     * @throws Exception
63
     */
64
    public function openFromCloud(array $cloud, string $save_to = null): Media
65
    {
66
        list($is_tmp, $save_to) = $this->isTmp($save_to);
67
68
        if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
69
            $cloud_obj = $cloud['cloud'];
70
            $options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
71
72
            $cloud_obj->download($save_to, $options);
73
        } else {
74
            throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method. 
75
                    and the cloud must be instance of CloudInterface');
76
        }
77
78
        return $this->open($save_to, $is_tmp);
79
    }
80
81
    /**
82
     * @param string $url
83
     * @param string|null $save_to
84
     * @param string $method
85
     * @param $request_options
86
     * @return Media
87
     * @throws Exception
88
     * @deprecated this method is deprecated
89
     */
90
    // @TODO: should be removed in the next releases.
91
    public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media
92
    {
93
        @trigger_error('fromURL method is deprecated and will be removed in a future release. Use Cloud instead', E_USER_DEPRECATED);
94
95
        Helper::isURL($url);
0 ignored issues
show
Deprecated Code introduced by
The function Streaming\Helper::isURL() has been deprecated: this method is deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
        /** @scrutinizer ignore-deprecated */ Helper::isURL($url);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
96
        list($is_tmp, $save_to) = $this->isTmp($save_to);
97
98
        $cloud = new Cloud($url, $method, $request_options);
99
        $cloud->download($save_to);
100
101
        return $this->open($save_to, $is_tmp);
102
    }
103
104
    /**
105
     * @param array $config
106
     * @param string $bucket
107
     * @param string $key
108
     * @param string|null $save_to
109
     * @return Media
110
     * @throws Exception
111
     * @deprecated this method is deprecated
112
     */
113
    // @TODO: should be removed in the next releases.
114
    public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media
115
    {
116
        @trigger_error('fromS3 method is deprecated and will be removed in a future release. Use AWS instead', E_USER_DEPRECATED);
117
118
        list($is_tmp, $save_to) = $this->isTmp($save_to);
119
120
        $aws = new AWS($config);
121
        $aws->download($save_to, ['bucket' => $bucket, 'key' => $key]);
122
123
        return $this->open($save_to, $is_tmp);
124
    }
125
126
    /**
127
     * @param array $config
128
     * @param string $bucket
129
     * @param string $name
130
     * @param string|null $save_to
131
     * @param bool $userProject
132
     * @return Media
133
     * @throws Exception
134
     * @deprecated this method is deprecated
135
     */
136
    // @TODO: should be removed in the next releases.
137
    public function fromGCS(array $config, string $bucket, string $name, string $save_to = null, $userProject = false): Media
138
    {
139
        @trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED);
140
        list($is_tmp, $save_to) = $this->isTmp($save_to);
141
142
        $google_cloud = new GoogleCloudStorage($config, $bucket, $userProject);
143
        $google_cloud->download($save_to, ['name' => $name]);
144
145
        return $this->open($save_to, $is_tmp);
146
    }
147
148
    /**
149
     * @param string $connectionString
150
     * @param string $container
151
     * @param string $blob
152
     * @param string|null $save_to
153
     * @return Media
154
     * @throws Exception
155
     * @deprecated this method is deprecated
156
     */
157
    // @TODO: should be removed in the next releases.
158
    public function fromMAS(string $connectionString, string $container, string $blob, string $save_to = null): Media
159
    {
160
        @trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED);
161
        list($is_tmp, $save_to) = $this->isTmp($save_to);
162
163
        $google_cloud = new MicrosoftAzure($connectionString);
164
        $google_cloud->download($save_to, ['container' => $container, 'blob' => $blob]);
165
166
        return $this->open($save_to, $is_tmp);
167
    }
168
169
    /**
170
     * @param $path
171
     * @return array
172
     * @throws Exception
173
     */
174
    private function isTmp($path)
175
    {
176
        $is_tmp = false;
177
178
        if (null === $path) {
179
            $is_tmp = true;
180
            $path = FileManager::tmpFile();
181
        }
182
183
        return [$is_tmp, $path];
184
    }
185
186
    /**
187
     * @param $method
188
     * @param $parameters
189
     * @return mixed
190
     */
191
    public function __call($method, $parameters)
192
    {
193
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
194
    }
195
196
    /**
197
     * @param array $config
198
     * @param LoggerInterface $logger
199
     * @param FFProbe|null $probe
200
     * @return FFMpeg
201
     */
202
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
203
    {
204
        return new static(BFFMpeg::create($config, $logger, $probe));
205
    }
206
}