1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Exporters; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Format\FormatInterface; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
8
|
|
|
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
9
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Disk; |
10
|
|
|
use ProtoneMedia\LaravelFFMpeg\MediaOpener; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @mixin \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg |
14
|
|
|
*/ |
15
|
|
|
class MediaExporter |
16
|
|
|
{ |
17
|
|
|
use ForwardsCalls, |
18
|
|
|
HandlesAdvancedMedia, |
19
|
|
|
HandlesConcatenation, |
20
|
|
|
HandlesFrames, |
21
|
|
|
HandlesTimelapse, |
22
|
|
|
HasProgressListener; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg |
26
|
|
|
*/ |
27
|
|
|
protected $driver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \FFMpeg\Format\FormatInterface |
31
|
|
|
*/ |
32
|
|
|
private $format; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $visibility; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Disk |
41
|
|
|
*/ |
42
|
|
|
private $toDisk; |
43
|
|
|
|
44
|
|
|
public function __construct(PHPFFMpeg $driver) |
45
|
|
|
{ |
46
|
|
|
$this->driver = $driver; |
47
|
|
|
|
48
|
|
|
$this->maps = new Collection; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getDisk(): Disk |
52
|
|
|
{ |
53
|
|
|
if ($this->toDisk) { |
54
|
|
|
return $this->toDisk; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$media = $this->driver->getMediaCollection(); |
58
|
|
|
|
59
|
|
|
return $this->toDisk = $media->first()->getDisk(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function inFormat(FormatInterface $format): self |
63
|
|
|
{ |
64
|
|
|
$this->format = $format; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function toDisk($disk) |
70
|
|
|
{ |
71
|
|
|
$this->toDisk = Disk::make($disk); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withVisibility(string $visibility) |
77
|
|
|
{ |
78
|
|
|
$this->visibility = $visibility; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getCommand(string $path = null) |
84
|
|
|
{ |
85
|
|
|
$this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps); |
86
|
|
|
|
87
|
|
|
$this->maps->each->apply($this->driver->get()); |
88
|
|
|
|
89
|
|
|
return $this->driver->getFinalCommand( |
|
|
|
|
90
|
|
|
$this->format, |
91
|
|
|
$path ? $this->getDisk()->makeMedia($path)->getLocalPath() : null |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function dd(string $path = null) |
96
|
|
|
{ |
97
|
|
|
dd($this->getCommand($path)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function save(string $path = null) |
101
|
|
|
{ |
102
|
|
|
$outputMedia = $path ? $this->getDisk()->makeMedia($path) : null; |
103
|
|
|
|
104
|
|
|
if ($this->concatWithTranscoding && $outputMedia) { |
105
|
|
|
$this->addConcatFilterAndMapping($outputMedia); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($this->maps->isNotEmpty()) { |
109
|
|
|
return $this->saveWithMappings(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($this->format && $this->onProgressCallback) { |
113
|
|
|
$this->applyProgressListenerToFormat($this->format); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->timelapseFramerate > 0) { |
117
|
|
|
$this->addTimelapseParametersToFormat(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($this->driver->isConcat() && $outputMedia) { |
121
|
|
|
$this->driver->saveFromSameCodecs($outputMedia->getLocalPath()); |
|
|
|
|
122
|
|
|
} elseif ($this->driver->isFrame()) { |
123
|
|
|
$data = $this->driver->save( |
|
|
|
|
124
|
|
|
optional($outputMedia)->getLocalPath(), |
125
|
|
|
$this->getAccuracy(), |
126
|
|
|
$this->returnFrameContents |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
if ($this->returnFrameContents) { |
130
|
|
|
return $data; |
131
|
|
|
} |
132
|
|
|
} else { |
133
|
|
|
$this->driver->save($this->format, $outputMedia->getLocalPath()); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$outputMedia->copyAllFromTemporaryDirectory($this->visibility); |
137
|
|
|
$outputMedia->setVisibility($this->visibility); |
138
|
|
|
|
139
|
|
|
if ($this->onProgressCallback) { |
140
|
|
|
call_user_func($this->onProgressCallback, 100, 0, 0); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->getMediaOpener(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function saveWithMappings(): MediaOpener |
147
|
|
|
{ |
148
|
|
|
$this->driver->getPendingComplexFilters()->each->apply($this->driver, $this->maps); |
149
|
|
|
|
150
|
|
|
$this->maps->map->apply($this->driver->get()); |
151
|
|
|
|
152
|
|
|
if ($this->onProgressCallback) { |
153
|
|
|
$this->applyProgressListenerToFormat($this->maps->last()->getFormat()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->driver->save(); |
157
|
|
|
|
158
|
|
|
if ($this->onProgressCallback) { |
159
|
|
|
call_user_func($this->onProgressCallback, 100); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->maps->map->getOutputMedia()->each->copyAllFromTemporaryDirectory($this->visibility); |
163
|
|
|
|
164
|
|
|
return $this->getMediaOpener(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
protected function getMediaOpener(): MediaOpener |
168
|
|
|
{ |
169
|
|
|
return new MediaOpener( |
170
|
|
|
$this->driver->getMediaCollection()->last()->getDisk(), |
|
|
|
|
171
|
|
|
$this->driver, |
172
|
|
|
$this->driver->getMediaCollection() |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Forwards the call to the driver object and returns the result |
178
|
|
|
* if it's something different than the driver object itself. |
179
|
|
|
*/ |
180
|
|
|
public function __call($method, $arguments) |
181
|
|
|
{ |
182
|
|
|
$result = $this->forwardCallTo($driver = $this->driver, $method, $arguments); |
183
|
|
|
|
184
|
|
|
return ($result === $driver) ? $this : $result; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|