Passed
Push — master ( a2ab6f...bcce02 )
by Amin
09:38
created

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