Passed
Push — master ( a933aa...32f6b3 )
by Amin
04:30
created

Export::getAdditionalParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 Streaming\Clouds\Cloud;
16
use Streaming\Exception\InvalidArgumentException;
17
use Streaming\Exception\RuntimeException;
18
use Streaming\Filters\Filter;
19
use Streaming\Traits\Formats;
20
21
22
abstract class Export
23
{
24
    use Formats;
25
26
    /** @var object */
27
    protected $media;
28
29
    /** @var array */
30
    protected $path_info;
31
32
    /** @var string */
33
    protected $strict = "-2";
34
35
    /** @var string */
36
    protected $tmp_dir;
37
38
    /** @var array */
39
    private $additional_params = [];
40
41
    /**
42
     * Export constructor.
43
     * @param Media $media
44
     */
45
    public function __construct(Media $media)
46
    {
47
        $this->media = $media;
48
        $this->path_info = pathinfo($media->getPath());
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getAdditionalParams(): array
55
    {
56
        return $this->additional_params;
57
    }
58
59
    /**
60
     * @param array $additional_params
61
     * @return Export
62
     */
63
    public function setAdditionalParams(array $additional_params)
64
    {
65
        $this->additional_params = $additional_params;
66
        return $this;
67
    }
68
69
    /**
70
     * @return object|Media
71
     */
72
    public function getMedia(): Media
73
    {
74
        return $this->media;
75
    }
76
77
    /**
78
     * @param string $strict
79
     * @return Export
80
     */
81
    public function setStrict(string $strict): Export
82
    {
83
        $this->strict = $strict;
84
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getStrict(): string
91
    {
92
        return $this->strict;
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    public function isTmpDir(): bool
99
    {
100
        return (bool)$this->tmp_dir;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getPathInfo(): array
107
    {
108
        return $this->path_info;
109
    }
110
111
    /**
112
     * @param string|null $path
113
     */
114
    private function moveTmp(?string $path): void
115
    {
116
        if ($this->isTmpDir() && !is_null($path)) {
117
            File::moveDir($this->tmp_dir, dirname($path));
118
            $this->path_info = pathinfo($path);
119
            $this->tmp_dir = '';
120
        }
121
    }
122
123
    /**
124
     * @param array $clouds
125
     * @param string $path
126
     */
127
    private function clouds(array $clouds, ?string $path): void
128
    {
129
        if (!empty($clouds)) {
130
            Cloud::uploadDirectory($clouds, $this->tmp_dir);
131
            $this->moveTmp($path);
132
        }
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    protected function getFilePath(): string
139
    {
140
        return str_replace("\\", "/", $this->path_info["dirname"] . "/" . $this->path_info["filename"]);
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    abstract protected function getPath(): string;
147
148
    /**
149
     * @return Filter
150
     */
151
    abstract protected function getFilter(): Filter;
152
153
    /**
154
     * Run FFmpeg to package media content
155
     */
156
    private function run(): void
157
    {
158
        try {
159
            $this->media
160
                ->addFilter($this->getFilter())
161
                ->save($this->getFormat(), $this->getPath());
162
        } catch (ExceptionInterface $e) {
163
            throw new RuntimeException("An error occurred while saving files: " . $e->getMessage(), $e->getCode(), $e);
164
        }
165
    }
166
167
    /**
168
     * @param string|null $path
169
     */
170
    private function tmpDirectory(?string $path): void
171
    {
172
        $basename = $path ? basename($path) : $this->path_info['basename'];
173
174
        $this->tmp_dir = File::tmpDir();
175
        $this->path_info = pathinfo($this->tmp_dir . $basename);
176
    }
177
178
    /**
179
     * @param $path
180
     * @param $clouds
181
     */
182
    private function makePaths(?string $path, array $clouds): void
183
    {
184
        if (!empty($clouds)) {
185
            $this->tmpDirectory($path);
186
        } elseif (!is_null($path)) {
187
            if (strlen($path) > PHP_MAXPATHLEN) {
188
                throw new InvalidArgumentException("The path is too long");
189
            }
190
191
            File::makeDir(dirname($path));
192
            $this->path_info = pathinfo($path);
193
        } elseif ($this->media->isTmp()) {
194
            throw new InvalidArgumentException("You need to specify a path. It is not possible to save to a tmp directory");
195
        }
196
    }
197
198
    /**
199
     * @param string $path
200
     * @param array $clouds
201
     * @param bool $metadata
202
     * @return mixed
203
     */
204
    public function save(string $path = null, array $clouds = [], bool $metadata = true)
205
    {
206
        $this->makePaths($path, $clouds);
207
        $this->run();
208
        $this->clouds($clouds, $path);
209
210
        return $metadata ? (new Metadata($this))->extract() : $this;
211
    }
212
213
    /**
214
     * clear tmp files
215
     */
216
    public function __destruct()
217
    {
218
        sleep(1);
219
220
        if ($this->media->isTmp()) {
221
            File::remove($this->media->getPath());
222
        }
223
224
        if ($this->tmp_dir) {
225
            File::remove($this->tmp_dir);
226
        }
227
228
        if ($this instanceof HLS && $this->tmp_key_info_file) {
229
            File::remove($this->getHlsKeyInfoFile());
230
        }
231
    }
232
}