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 $format; |
14
|
|
|
|
15
|
|
|
protected $saveMethod = 'saveAudioOrVideo'; |
16
|
|
|
|
17
|
|
|
public function __construct(Media $media) |
18
|
|
|
{ |
19
|
|
|
$this->media = $media; |
20
|
|
|
|
21
|
|
|
$this->disk = $media->getFile()->getDisk(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function getFormat(): FormatInterface |
25
|
|
|
{ |
26
|
|
|
return $this->format; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function inFormat(FormatInterface $format): MediaExporter |
30
|
|
|
{ |
31
|
|
|
$this->format = $format; |
32
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getDisk(): Disk |
37
|
|
|
{ |
38
|
|
|
return $this->disk; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function toDisk($diskOrName): MediaExporter |
42
|
|
|
{ |
43
|
|
|
if ($diskOrName instanceof Disk) { |
44
|
|
|
$this->disk = $diskOrName; |
45
|
|
|
} else { |
46
|
|
|
$this->disk = Disk::fromName($diskOrName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function save(string $path): Media |
53
|
|
|
{ |
54
|
|
|
$file = $this->getDisk()->newFile($path); |
55
|
|
|
|
56
|
|
|
$destinationPath = $this->getDestinationPathForSaving($file); |
57
|
|
|
|
58
|
|
|
$this->createDestinationPathForSaving($file); |
59
|
|
|
|
60
|
|
|
$this->{$this->saveMethod}($destinationPath); |
61
|
|
|
|
62
|
|
|
if (!$this->getDisk()->isLocal()) { |
63
|
|
|
$this->moveSavedFileToRemoteDisk($destinationPath, $file); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->media; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function moveSavedFileToRemoteDisk($localSourcePath, File $fileOnRemoteDisk): bool |
70
|
|
|
{ |
71
|
|
|
return $fileOnRemoteDisk->put($localSourcePath) && @unlink($localSourcePath); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function getDestinationPathForSaving(File $file): string |
75
|
|
|
{ |
76
|
|
|
if (!$file->getDisk()->isLocal()) { |
77
|
|
|
$tempName = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg'); |
78
|
|
|
|
79
|
|
|
return $tempName . '.' . $file->getExtension(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $file->getFullPath(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function createDestinationPathForSaving(File $file) |
86
|
|
|
{ |
87
|
|
|
if (!$file->getDisk()->isLocal()) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$directory = pathinfo($file->getPath())['dirname']; |
92
|
|
|
|
93
|
|
|
return $file->getDisk()->createDirectory($directory); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function saveAudioOrVideo(string $fullPath): MediaExporter |
97
|
|
|
{ |
98
|
|
|
$this->media->save($this->getFormat(), $fullPath); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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: