|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pbmedia\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\Traits\ForwardsCalls; |
|
10
|
|
|
use Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
|
11
|
|
|
use Pbmedia\LaravelFFMpeg\Exporters\HLSExporter; |
|
12
|
|
|
use Pbmedia\LaravelFFMpeg\Exporters\MediaExporter; |
|
13
|
|
|
use Pbmedia\LaravelFFMpeg\Filesystem\Disk; |
|
14
|
|
|
use Pbmedia\LaravelFFMpeg\Filesystem\Media; |
|
15
|
|
|
use Pbmedia\LaravelFFMpeg\Filesystem\MediaCollection; |
|
16
|
|
|
use Pbmedia\LaravelFFMpeg\Filesystem\TemporaryDirectories; |
|
17
|
|
|
|
|
18
|
|
|
class MediaOpener |
|
19
|
|
|
{ |
|
20
|
|
|
use ForwardsCalls; |
|
21
|
|
|
|
|
22
|
|
|
private Disk $disk; |
|
23
|
|
|
private PHPFFMpeg $driver; |
|
24
|
|
|
private MediaCollection $collection; |
|
25
|
|
|
private ?TimeCode $timecode = null; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct($disk = null, PHPFFMpeg $driver = null, MediaCollection $mediaCollection = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->disk = Disk::make($disk ?: config('filesystems.default')); |
|
30
|
|
|
|
|
31
|
|
|
$this->driver = $driver ?: app(PHPFFMpeg::class); |
|
32
|
|
|
|
|
33
|
|
|
$this->collection = $mediaCollection ?: new MediaCollection; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function fromDisk($disk): self |
|
37
|
|
|
{ |
|
38
|
|
|
$this->disk = Disk::make($disk); |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function fromFilesystem(Filesystem $filesystem): self |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->fromDisk($filesystem); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function open($path): self |
|
49
|
|
|
{ |
|
50
|
|
|
$paths = Arr::wrap($path); |
|
51
|
|
|
|
|
52
|
|
|
foreach ($paths as $path) { |
|
|
|
|
|
|
53
|
|
|
$this->collection->push(Media::make($this->disk, $path)); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function get(): MediaCollection |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->collection; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getDriver(): PHPFFMpeg |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->driver->open($this->collection); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getAdvancedDriver(): PHPFFMpeg |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->driver->openAdvanced($this->collection); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getFrameFromString(string $timecode): self |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->getFrameFromTimecode( |
|
77
|
|
|
TimeCode::fromString($timecode) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getFrameFromSeconds(float $quantity): self |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->getFrameFromTimecode( |
|
84
|
|
|
TimeCode::fromSeconds($quantity) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getFrameFromTimecode(TimeCode $timecode): self |
|
89
|
|
|
{ |
|
90
|
|
|
$this->timecode = $timecode; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function export(): MediaExporter |
|
96
|
|
|
{ |
|
97
|
|
|
return tap(new MediaExporter($this->getDriver()), function (MediaExporter $mediaExporter) { |
|
98
|
|
|
if ($this->timecode) { |
|
99
|
|
|
$mediaExporter->frame($this->timecode); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
}); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function exportForHLS(): HLSExporter |
|
105
|
|
|
{ |
|
106
|
|
|
return new HLSExporter($this->getAdvancedDriver()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function cleanupTemporaryFiles(): self |
|
110
|
|
|
{ |
|
111
|
|
|
TemporaryDirectories::deleteAll(); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function __invoke(): AbstractMediaType |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->getDriver()->get(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function __call($method, $arguments) |
|
122
|
|
|
{ |
|
123
|
|
|
$result = $this->forwardCallTo($driver = $this->getDriver(), $method, $arguments); |
|
124
|
|
|
|
|
125
|
|
|
return ($result === $driver) ? $this : $result; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|