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 Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
class FFMpeg |
11
|
|
|
{ |
12
|
|
|
protected static $filesystems; |
13
|
|
|
|
14
|
|
|
private static $temporaryFiles = []; |
15
|
|
|
|
16
|
|
|
protected $disk; |
17
|
|
|
|
18
|
|
|
protected $ffmpeg; |
19
|
|
|
|
20
|
|
|
public function __construct(Filesystems $filesystems, ConfigRepository $config, LoggerInterface $logger) |
21
|
|
|
{ |
22
|
|
|
static::$filesystems = $filesystems; |
23
|
|
|
|
24
|
|
|
$ffmpegConfig = $config->get('laravel-ffmpeg'); |
25
|
|
|
|
26
|
|
|
$this->ffmpeg = BaseFFMpeg::create($ffmpegConfig, $logger); |
27
|
|
|
$this->fromDisk($ffmpegConfig['default_disk'] ?? $config->get('filesystems.default')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getFilesystems(): Filesystems |
31
|
|
|
{ |
32
|
|
|
return static::$filesystems; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function newTemporaryFile(): string |
36
|
|
|
{ |
37
|
|
|
return static::$temporaryFiles[] = tempnam(sys_get_temp_dir(), 'laravel-ffmpeg'); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function cleanupTemporaryFiles() |
41
|
|
|
{ |
42
|
|
|
foreach (static::$temporaryFiles as $path) { |
|
|
|
|
43
|
|
|
@unlink($path); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function fromDisk(string $diskName): FFMpeg |
48
|
|
|
{ |
49
|
|
|
$filesystem = static::getFilesystems()->disk($diskName); |
50
|
|
|
$this->disk = new Disk($filesystem); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function open($path): Media |
56
|
|
|
{ |
57
|
|
|
$file = $this->disk->newFile($path); |
58
|
|
|
|
59
|
|
|
if ($this->disk->isLocal()) { |
60
|
|
|
$ffmpegPathFile = $file->getFullPath(); |
61
|
|
|
} else { |
62
|
|
|
$ffmpegPathFile = static::newTemporaryFile(); |
63
|
|
|
file_put_contents($ffmpegPathFile, $this->disk->read($path)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$ffmpegMedia = $this->ffmpeg->open($ffmpegPathFile); |
67
|
|
|
|
68
|
|
|
return new Media($file, $ffmpegMedia); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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: