Completed
Push — master ( baa637...9b813d )
by Amin
04:08 queued 11s
created

FFMpeg   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 33
dl 0
loc 141
rs 10
c 1
b 1
f 1
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromS3() 0 8 1
A __construct() 0 3 1
A create() 0 3 1
A fromMAS() 0 8 1
A isTmp() 0 10 2
A open() 0 10 3
A fromGCS() 0 8 1
A fromURL() 0 9 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\GoogleCloudStorage;
21
use Streaming\Clouds\MicrosoftAzure;
22
use Streaming\Exception\Exception;
23
use Streaming\Exception\InvalidArgumentException;
24
use Streaming\Exception\RuntimeException;
25
26
class FFMpeg
27
{
28
    /** @var BFFMpeg */
29
    protected $ffmpeg;
30
31
    /**
32
     * @param $ffmpeg
33
     */
34
    public function __construct(BFFMpeg $ffmpeg)
35
    {
36
        $this->ffmpeg = $ffmpeg;
37
    }
38
39
    /**
40
     * @param string $path
41
     * @param bool $is_tmp
42
     * @return Media
43
     */
44
    public function open(string $path, bool $is_tmp = false): Media
45
    {
46
        if (!is_file($path)) {
47
            throw new InvalidArgumentException("There is no file in this path: " . $path);
48
        }
49
50
        try {
51
            return new Media($this->ffmpeg->open($path), $path, $is_tmp);
52
        } catch (ExceptionInterface $e) {
53
            throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e);
54
        }
55
    }
56
57
    /**
58
     * @param string $url
59
     * @param string|null $save_to
60
     * @param string $method
61
     * @param $request_options
62
     * @return Media
63
     * @throws Exception
64
     */
65
    public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media
66
    {
67
        Helper::isURL($url);
68
        list($is_tmp, $save_to) = $this->isTmp($save_to);
69
70
        $cloud = new Cloud($url, $method, $request_options);
71
        $cloud->download($save_to);
72
73
        return $this->open($save_to, $is_tmp);
74
    }
75
76
    /**
77
     * @param array $config
78
     * @param string $bucket
79
     * @param string $key
80
     * @param string|null $save_to
81
     * @return Media
82
     * @throws Exception
83
     */
84
    public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media
85
    {
86
        list($is_tmp, $save_to) = $this->isTmp($save_to);
87
88
        $aws = new AWS($config);
89
        $aws->download($save_to, ['bucket' => $bucket, 'key' => $key]);
90
91
        return $this->open($save_to, $is_tmp);
92
    }
93
94
    /**
95
     * @param array $config
96
     * @param string $bucket
97
     * @param string $name
98
     * @param string|null $save_to
99
     * @param bool $userProject
100
     * @return Media
101
     * @throws Exception
102
     */
103
    public function fromGCS(array $config, string $bucket, string $name, string $save_to = null, $userProject = false): Media
104
    {
105
        list($is_tmp, $save_to) = $this->isTmp($save_to);
106
107
        $google_cloud = new GoogleCloudStorage($config, $bucket, $userProject);
108
        $google_cloud->download($save_to, ['name' => $name]);
109
110
        return $this->open($save_to, $is_tmp);
111
    }
112
113
    /**
114
     * @param string $connectionString
115
     * @param string $container
116
     * @param string $blob
117
     * @param string|null $save_to
118
     * @return Media
119
     * @throws Exception
120
     */
121
    public function fromMAS(string $connectionString, string $container, string $blob, string $save_to = null): Media
122
    {
123
        list($is_tmp, $save_to) = $this->isTmp($save_to);
124
125
        $google_cloud = new MicrosoftAzure($connectionString);
126
        $google_cloud->download($save_to, ['container' => $container, 'blob' => $blob]);
127
128
        return $this->open($save_to, $is_tmp);
129
    }
130
131
    /**
132
     * @param $path
133
     * @return array
134
     * @throws Exception
135
     */
136
    private function isTmp($path)
137
    {
138
        $is_tmp = false;
139
140
        if (null === $path) {
141
            $is_tmp = true;
142
            $path = FileManager::tmpFile();
143
        }
144
145
        return [$is_tmp, $path];
146
    }
147
148
    /**
149
     * @param $method
150
     * @param $parameters
151
     * @return mixed
152
     */
153
    public function __call($method, $parameters)
154
    {
155
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
156
    }
157
158
    /**
159
     * @param array $config
160
     * @param LoggerInterface $logger
161
     * @param FFProbe|null $probe
162
     * @return FFMpeg
163
     */
164
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
165
    {
166
        return new static(BFFMpeg::create($config, $logger, $probe));
167
    }
168
}