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->disk = Disk::make($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 MediaOnNetwork object for each given url. |
102
|
|
|
*/ |
103
|
|
|
public function openUrl($paths, array $headers = []): self |
104
|
|
|
{ |
105
|
|
|
foreach (Arr::wrap($paths) as $path) { |
106
|
|
|
$this->collection->push(MediaOnNetwork::make($path, $headers)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function get(): MediaCollection |
113
|
|
|
{ |
114
|
|
|
return $this->collection; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getDriver(): PHPFFMpeg |
118
|
|
|
{ |
119
|
|
|
return $this->driver->open($this->collection); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Forces the driver to open the collection with the `openAdvanced` method. |
124
|
|
|
*/ |
125
|
|
|
public function getAdvancedDriver(): PHPFFMpeg |
126
|
|
|
{ |
127
|
|
|
return $this->driver->openAdvanced($this->collection); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Shortcut to set the timecode by string. |
132
|
|
|
*/ |
133
|
|
|
public function getFrameFromString(string $timecode): self |
134
|
|
|
{ |
135
|
|
|
return $this->getFrameFromTimecode(TimeCode::fromString($timecode)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Shortcut to set the timecode by seconds. |
140
|
|
|
*/ |
141
|
|
|
public function getFrameFromSeconds(float $quantity): self |
142
|
|
|
{ |
143
|
|
|
return $this->getFrameFromTimecode(TimeCode::fromSeconds($quantity)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): self |
147
|
|
|
{ |
148
|
|
|
$this->timecode = $timecode; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns an instance of MediaExporter with the driver and timecode (if set). |
155
|
|
|
*/ |
156
|
|
|
public function export(): MediaExporter |
157
|
|
|
{ |
158
|
|
|
return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) { |
159
|
|
|
if ($this->timecode) { |
160
|
|
|
$mediaExporter->frame($this->timecode); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
}); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Returns an instance of HLSExporter with the driver forced to AdvancedMedia. |
167
|
|
|
*/ |
168
|
|
|
public function exportForHLS(): HLSExporter |
169
|
|
|
{ |
170
|
|
|
return new HLSExporter($this->getAdvancedDriver()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function cleanupTemporaryFiles(): self |
174
|
|
|
{ |
175
|
|
|
TemporaryDirectories::deleteAll(); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function each($items, callable $callback): self |
181
|
|
|
{ |
182
|
|
|
Collection::make($items)->each(function ($item, $key) use ($callback) { |
183
|
|
|
return $callback($this->clone(), $item, $key); |
184
|
|
|
}); |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns the Media object from the driver. |
191
|
|
|
*/ |
192
|
|
|
public function __invoke(): AbstractMediaType |
193
|
|
|
{ |
194
|
|
|
return $this->getDriver()->get(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Forwards all calls to the underlying driver. |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function __call($method, $arguments) |
202
|
|
|
{ |
203
|
|
|
$result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments); |
204
|
|
|
|
205
|
|
|
return ($result === $driver) ? $this : $result; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|