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