|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
|
4
|
|
|
|
|
5
|
|
|
use FFMpeg\Format\FormatInterface; |
|
6
|
|
|
|
|
7
|
|
|
class MediaExporter |
|
8
|
|
|
{ |
|
9
|
|
|
protected $media; |
|
10
|
|
|
|
|
11
|
|
|
protected $disk; |
|
12
|
|
|
|
|
13
|
|
|
protected $frameMustBeAccurate = false; |
|
14
|
|
|
|
|
15
|
|
|
protected $format; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Media $media) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->media = $media; |
|
20
|
|
|
$this->disk = $media->getFile()->getDisk(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function getFormat(): FormatInterface |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->format; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function inFormat(FormatInterface $format): self |
|
29
|
|
|
{ |
|
30
|
|
|
$this->format = $format; |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getDisk(): Disk |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->disk; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function toDisk(string $diskName): self |
|
41
|
|
|
{ |
|
42
|
|
|
$this->disk = Disk::fromName($diskName); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function accurate(): self |
|
48
|
|
|
{ |
|
49
|
|
|
$this->frameMustBeAccurate = true; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function unaccurate(): self |
|
55
|
|
|
{ |
|
56
|
|
|
$this->frameMustBeAccurate = false; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getAccuracy(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->frameMustBeAccurate; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function save(string $path): Media |
|
67
|
|
|
{ |
|
68
|
|
|
$file = $this->getDisk()->newFile($path); |
|
69
|
|
|
|
|
70
|
|
|
$destinationPath = $this->getDestinationPathForSaving($file); |
|
71
|
|
|
|
|
72
|
|
|
$saveMethod = $this->media->isFrame() ? 'saveFrame' : 'saveAudioOrVideo'; |
|
73
|
|
|
|
|
74
|
|
|
$this->{$saveMethod}($destinationPath); |
|
75
|
|
|
|
|
76
|
|
|
if (!$this->getDisk()->isLocal()) { |
|
77
|
|
|
$this->moveSavedFileToRemoteDisk($destinationPath, $file); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->media; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool |
|
84
|
|
|
{ |
|
85
|
|
|
$resource = fopen($localSourcePath, 'r'); |
|
86
|
|
|
|
|
87
|
|
|
return $fileOnRemoteDisk->put($resource) && unlink($localSourcePath); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function getDestinationPathForSaving(File $file): string |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$file->getDisk()->isLocal()) { |
|
93
|
|
|
$tempName = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg'); |
|
94
|
|
|
|
|
95
|
|
|
return $tempName . '.' . $file->getExtension(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $file->getFullPath(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function saveFrame(string $fullPath): self |
|
102
|
|
|
{ |
|
103
|
|
|
$this->media->save($fullPath, $this->getAccuracy()); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function saveAudioOrVideo(string $fullPath): self |
|
109
|
|
|
{ |
|
110
|
|
|
$this->media->save($this->getFormat(), $fullPath); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: