1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Format\FormatInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method mixed save(FormatInterface $format, $outputPathfile) |
9
|
|
|
*/ |
10
|
|
|
class MediaExporter |
11
|
|
|
{ |
12
|
|
|
protected $media; |
13
|
|
|
|
14
|
|
|
protected $disk; |
15
|
|
|
|
16
|
|
|
protected $frameMustBeAccurate = false; |
17
|
|
|
|
18
|
|
|
protected $format; |
19
|
|
|
|
20
|
|
|
public function __construct(Media $media) |
21
|
|
|
{ |
22
|
|
|
$this->media = $media; |
23
|
|
|
$this->disk = $media->getFile()->getDisk(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function getFormat(): FormatInterface |
27
|
|
|
{ |
28
|
|
|
return $this->format; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function inFormat(FormatInterface $format): self |
32
|
|
|
{ |
33
|
|
|
$this->format = $format; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getDisk(): Disk |
39
|
|
|
{ |
40
|
|
|
return $this->disk; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function toDisk(string $diskName): self |
44
|
|
|
{ |
45
|
|
|
$this->disk = Disk::fromName($diskName); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function accurate(): self |
51
|
|
|
{ |
52
|
|
|
$this->frameMustBeAccurate = true; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function unaccurate(): self |
58
|
|
|
{ |
59
|
|
|
$this->frameMustBeAccurate = false; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getAccuracy(): bool |
65
|
|
|
{ |
66
|
|
|
return $this->frameMustBeAccurate; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function save(string $path): Media |
70
|
|
|
{ |
71
|
|
|
$disk = $this->getDisk(); |
72
|
|
|
$file = $disk->newFile($path); |
73
|
|
|
|
74
|
|
|
$destination = $file->getFullPath(); |
75
|
|
|
|
76
|
|
|
if (!$disk->isLocal()) { |
77
|
|
|
$destination = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->media->isFrame()) { |
81
|
|
|
$this->saveFrame($destination); |
82
|
|
|
} else { |
83
|
|
|
$this->saveAudioOrVideo($destination); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$disk->isLocal()) { |
87
|
|
|
$file->createFromTempPath($destination); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->media; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function saveFrame(string $fullPath): self |
94
|
|
|
{ |
95
|
|
|
$this->media->save($fullPath, $this->getAccuracy()); |
|
|
|
|
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function saveAudioOrVideo(string $fullPath): self |
101
|
|
|
{ |
102
|
|
|
$this->media->save($this->getFormat(), $fullPath); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: