|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Drivers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use FFMpeg\Coordinate\TimeCode; |
|
7
|
|
|
use FFMpeg\FFMpeg; |
|
8
|
|
|
use FFMpeg\Media\AbstractMediaType; |
|
9
|
|
|
use FFMpeg\Media\AdvancedMedia as BaseAdvancedMedia; |
|
10
|
|
|
use FFMpeg\Media\Concat; |
|
11
|
|
|
use FFMpeg\Media\Frame; |
|
12
|
|
|
use FFMpeg\Media\Video; |
|
13
|
|
|
use Illuminate\Support\Arr; |
|
14
|
|
|
use Illuminate\Support\Collection; |
|
15
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
|
16
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\AdvancedMedia; |
|
17
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\AudioMedia; |
|
18
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\FFProbe; |
|
19
|
|
|
use ProtoneMedia\LaravelFFMpeg\FFMpeg\VideoMedia; |
|
20
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @mixin \FFMpeg\Media\AbstractMediaType |
|
24
|
|
|
*/ |
|
25
|
|
|
class PHPFFMpeg |
|
26
|
|
|
{ |
|
27
|
|
|
use ForwardsCalls; |
|
28
|
|
|
use InteractsWithFilters; |
|
29
|
|
|
use InteractsWithMediaStreams; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \FFMpeg\FFMpeg |
|
33
|
|
|
*/ |
|
34
|
|
|
private $ffmpeg; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection |
|
38
|
|
|
*/ |
|
39
|
|
|
private $mediaCollection; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $forceAdvanced = false; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \FFMpeg\Media\AbstractMediaType |
|
48
|
|
|
*/ |
|
49
|
|
|
private $media; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(FFMpeg $ffmpeg) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->ffmpeg = $ffmpeg; |
|
54
|
|
|
$this->pendingComplexFilters = new Collection; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns a fresh instance of itself with only the underlying FFMpeg instance. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function fresh(): self |
|
61
|
|
|
{ |
|
62
|
|
|
return new static($this->ffmpeg); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function get(): AbstractMediaType |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->media; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function isAdvancedMedia(): bool |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
return $this->get() instanceof BaseAdvancedMedia; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function isFrame(): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->get() instanceof Frame; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function isConcat(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->get() instanceof Concat; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function isVideo(): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->get() instanceof Video; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getMediaCollection(): MediaCollection |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->mediaCollection; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Opens the MediaCollection if it's not been instanciated yet. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function open(MediaCollection $mediaCollection): self |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->media) { |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->mediaCollection = $mediaCollection; |
|
105
|
|
|
|
|
106
|
|
|
if ($mediaCollection->count() === 1 && !$this->forceAdvanced) { |
|
107
|
|
|
$media = Arr::first($mediaCollection->collection()); |
|
108
|
|
|
|
|
109
|
|
|
$this->ffmpeg->setFFProbe( |
|
110
|
|
|
FFProbe::make($this->ffmpeg->getFFProbe())->setMedia($media) |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$ffmpegMedia = $this->ffmpeg->open($media->getLocalPath()); |
|
114
|
|
|
|
|
115
|
|
|
$this->media = $ffmpegMedia instanceof Video |
|
116
|
|
|
? VideoMedia::make($ffmpegMedia) |
|
117
|
|
|
: AudioMedia::make($ffmpegMedia); |
|
118
|
|
|
|
|
119
|
|
|
$this->media->setHeaders(Arr::first($mediaCollection->getHeaders()) ?: []); |
|
120
|
|
|
} else { |
|
121
|
|
|
$ffmpegMedia = $this->ffmpeg->openAdvanced($mediaCollection->getLocalPaths()); |
|
122
|
|
|
|
|
123
|
|
|
$this->media = AdvancedMedia::make($ffmpegMedia) |
|
124
|
|
|
->setHeaders($mediaCollection->getHeaders()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function frame(TimeCode $timecode) |
|
131
|
|
|
{ |
|
132
|
|
|
if (!$this->isVideo()) { |
|
133
|
|
|
throw new Exception('Opened media is not a video file.'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->media = $this->media->frame($timecode); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function concatWithoutTranscoding() |
|
142
|
|
|
{ |
|
143
|
|
|
$localPaths = $this->mediaCollection->getLocalPaths(); |
|
144
|
|
|
|
|
145
|
|
|
$this->media = $this->ffmpeg->open(Arr::first($localPaths)) |
|
146
|
|
|
->concat($localPaths); |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Force 'openAdvanced' when opening the MediaCollection |
|
153
|
|
|
*/ |
|
154
|
|
|
public function openAdvanced(MediaCollection $mediaCollection): self |
|
155
|
|
|
{ |
|
156
|
|
|
$this->forceAdvanced = true; |
|
157
|
|
|
|
|
158
|
|
|
return $this->open($mediaCollection); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Returns the underlying media object itself. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function __invoke(): AbstractMediaType |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->get(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Forwards the call to the underling media object and returns the result |
|
171
|
|
|
* if it's something different than the media object itself. |
|
172
|
|
|
*/ |
|
173
|
|
|
public function __call($method, $arguments) |
|
174
|
|
|
{ |
|
175
|
|
|
$result = $this->forwardCallTo($media = $this->get(), $method, $arguments); |
|
176
|
|
|
|
|
177
|
|
|
return ($result === $media) ? $this : $result; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.