1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\LaravelFFMpeg; |
4
|
|
|
|
5
|
|
|
use FFMpeg\FFMpeg as BaseFFMpeg; |
6
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
7
|
|
|
use Illuminate\Contracts\Filesystem\Factory as Filesystems; |
8
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
class FFMpeg |
12
|
|
|
{ |
13
|
|
|
protected static $filesystems; |
14
|
|
|
|
15
|
|
|
private static $temporaryFiles = []; |
16
|
|
|
|
17
|
|
|
protected $disk; |
18
|
|
|
|
19
|
|
|
protected $ffmpeg; |
20
|
|
|
|
21
|
|
|
public function __construct(Filesystems $filesystems, ConfigRepository $config, LoggerInterface $logger) |
22
|
|
|
{ |
23
|
|
|
static::$filesystems = $filesystems; |
24
|
|
|
|
25
|
|
|
$ffmpegConfig = $config->get('laravel-ffmpeg'); |
26
|
|
|
|
27
|
|
|
$this->ffmpeg = BaseFFMpeg::create($ffmpegConfig, $logger); |
28
|
|
|
$this->fromDisk($ffmpegConfig['default_disk'] ?? $config->get('filesystems.default')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function getFilesystems(): Filesystems |
32
|
|
|
{ |
33
|
|
|
return static::$filesystems; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function newTemporaryFile(): string |
37
|
|
|
{ |
38
|
|
|
return static::$temporaryFiles[] = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg'); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function cleanupTemporaryFiles() |
42
|
|
|
{ |
43
|
|
|
foreach (static::$temporaryFiles as $path) { |
|
|
|
|
44
|
|
|
@unlink($path); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function fromFilesystem(Filesystem $filesystem): FFMpeg |
49
|
|
|
{ |
50
|
|
|
$this->disk = new Disk($filesystem); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function fromDisk(string $diskName): FFMpeg |
56
|
|
|
{ |
57
|
|
|
$filesystem = static::getFilesystems()->disk($diskName); |
58
|
|
|
$this->disk = new Disk($filesystem); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function open($path): Media |
64
|
|
|
{ |
65
|
|
|
$file = $this->disk->newFile($path); |
66
|
|
|
|
67
|
|
|
if ($this->disk->isLocal()) { |
68
|
|
|
$ffmpegPathFile = $file->getFullPath(); |
69
|
|
|
} else { |
70
|
|
|
$ffmpegPathFile = static::newTemporaryFile(); |
71
|
|
|
file_put_contents($ffmpegPathFile, $this->disk->read($path)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$ffmpegMedia = $this->ffmpeg->open($ffmpegPathFile); |
75
|
|
|
|
76
|
|
|
return new Media($file, $ffmpegMedia); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: