1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Coordinate\TimeCode; |
6
|
|
|
use FFMpeg\Media\AbstractMediaType; |
7
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
11
|
|
|
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
12
|
|
|
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSExporter; |
13
|
|
|
use ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter; |
14
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk; |
15
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media; |
16
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection; |
17
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaOnNetwork; |
18
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\TemporaryDirectories; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @mixin \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg |
22
|
|
|
*/ |
23
|
|
|
class MediaOpener |
24
|
|
|
{ |
25
|
|
|
use ForwardsCalls; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk |
29
|
|
|
*/ |
30
|
|
|
private $disk; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg |
34
|
|
|
*/ |
35
|
|
|
private $driver; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection |
39
|
|
|
*/ |
40
|
|
|
private $collection; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \FFMpeg\Coordinate\TimeCode |
44
|
|
|
*/ |
45
|
|
|
private $timecode; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Uses the 'filesystems.default' disk from the config if none is given. |
49
|
|
|
* Gets the underlying PHPFFMpeg instance from the container if none is given. |
50
|
|
|
* Instantiates a fresh MediaCollection if none is given. |
51
|
|
|
*/ |
52
|
|
|
public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollection $mediaCollection = null) |
53
|
|
|
{ |
54
|
|
|
$this->fromDisk($disk ?: config('filesystems.default')); |
55
|
|
|
|
56
|
|
|
$this->driver = ($driver ?: app(PHPFFMpeg::class))->fresh(); |
57
|
|
|
|
58
|
|
|
$this->collection = $mediaCollection ?: new MediaCollection; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function clone(): self |
62
|
|
|
{ |
63
|
|
|
return new MediaOpener( |
64
|
|
|
$this->disk, |
65
|
|
|
$this->driver, |
66
|
|
|
$this->collection |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set the disk to open files from. |
72
|
|
|
*/ |
73
|
|
|
public function fromDisk($disk): self |
74
|
|
|
{ |
75
|
|
|
$this->disk = Disk::make($disk); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Alias for 'fromDisk', mostly for backwards compatibility. |
82
|
|
|
*/ |
83
|
|
|
public function fromFilesystem(Filesystem $filesystem): self |
84
|
|
|
{ |
85
|
|
|
return $this->fromDisk($filesystem); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Instantiates a Media object for each given path. |
90
|
|
|
*/ |
91
|
|
|
public function open($paths): self |
92
|
|
|
{ |
93
|
|
|
foreach (Arr::wrap($paths) as $path) { |
94
|
|
|
$this->collection->push(Media::make($this->disk, $path)); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Instantiates a single Media object and sets the given options on the object. |
102
|
|
|
* |
103
|
|
|
* @param string $path |
104
|
|
|
* @param array $options |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public function openWithInputOptions(string $path, array $options = []): self |
108
|
|
|
{ |
109
|
|
|
$this->collection->push( |
110
|
|
|
Media::make($this->disk, $path)->setInputOptions($options) |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Instantiates a MediaOnNetwork object for each given url. |
118
|
|
|
*/ |
119
|
|
|
public function openUrl($paths, array $headers = []): self |
120
|
|
|
{ |
121
|
|
|
foreach (Arr::wrap($paths) as $path) { |
122
|
|
|
$this->collection->push(MediaOnNetwork::make($path, $headers)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function get(): MediaCollection |
129
|
|
|
{ |
130
|
|
|
return $this->collection; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getDriver(): PHPFFMpeg |
134
|
|
|
{ |
135
|
|
|
return $this->driver->open($this->collection); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Forces the driver to open the collection with the `openAdvanced` method. |
140
|
|
|
*/ |
141
|
|
|
public function getAdvancedDriver(): PHPFFMpeg |
142
|
|
|
{ |
143
|
|
|
return $this->driver->openAdvanced($this->collection); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Shortcut to set the timecode by string. |
148
|
|
|
*/ |
149
|
|
|
public function getFrameFromString(string $timecode): self |
150
|
|
|
{ |
151
|
|
|
return $this->getFrameFromTimecode(TimeCode::fromString($timecode)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Shortcut to set the timecode by seconds. |
156
|
|
|
*/ |
157
|
|
|
public function getFrameFromSeconds(float $seconds): self |
158
|
|
|
{ |
159
|
|
|
return $this->getFrameFromTimecode(TimeCode::fromSeconds($seconds)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): self |
163
|
|
|
{ |
164
|
|
|
$this->timecode = $timecode; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns an instance of MediaExporter with the driver and timecode (if set). |
171
|
|
|
*/ |
172
|
|
|
public function export(): MediaExporter |
173
|
|
|
{ |
174
|
|
|
return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) { |
175
|
|
|
if ($this->timecode) { |
176
|
|
|
$mediaExporter->frame($this->timecode); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
}); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns an instance of HLSExporter with the driver forced to AdvancedMedia. |
183
|
|
|
*/ |
184
|
|
|
public function exportForHLS(): HLSExporter |
185
|
|
|
{ |
186
|
|
|
return new HLSExporter($this->getAdvancedDriver()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function cleanupTemporaryFiles(): self |
190
|
|
|
{ |
191
|
|
|
app(TemporaryDirectories::class)->deleteAll(); |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function each($items, callable $callback): self |
197
|
|
|
{ |
198
|
|
|
Collection::make($items)->each(function ($item, $key) use ($callback) { |
199
|
|
|
return $callback($this->clone(), $item, $key); |
200
|
|
|
}); |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the Media object from the driver. |
207
|
|
|
*/ |
208
|
|
|
public function __invoke(): AbstractMediaType |
209
|
|
|
{ |
210
|
|
|
return $this->getDriver()->get(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Forwards all calls to the underlying driver. |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
public function __call($method, $arguments) |
218
|
|
|
{ |
219
|
|
|
$result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments); |
220
|
|
|
|
221
|
|
|
return ($result === $driver) ? $this : $result; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|