1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Format\FormatInterface; |
6
|
|
|
use FFMpeg\Format\Video\DefaultVideo; |
7
|
|
|
use FFMpeg\Media\AdvancedMedia; |
|
|
|
|
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSVideoFilters; |
10
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class is basically a wrapper around the map() method |
14
|
|
|
* of a the \FFMpeg\Media\AdvancedMedia class. |
15
|
|
|
*/ |
16
|
|
|
class AdvancedOutputMapping |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $outs; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \FFMpeg\Format\FormatInterface |
25
|
|
|
*/ |
26
|
|
|
private $format; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Media |
30
|
|
|
*/ |
31
|
|
|
private $output; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
private $forceDisableAudio = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
private $forceDisableVideo = false; |
42
|
|
|
|
43
|
|
|
public function __construct(array $outs, FormatInterface $format, Media $output, bool $forceDisableAudio = false, bool $forceDisableVideo = false) |
44
|
|
|
{ |
45
|
|
|
$this->outs = $outs; |
46
|
|
|
$this->format = $format; |
47
|
|
|
$this->output = $output; |
48
|
|
|
$this->forceDisableAudio = $forceDisableAudio; |
49
|
|
|
$this->forceDisableVideo = $forceDisableVideo; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Applies the attributes to the format and specifies the video |
54
|
|
|
* bitrate if it's missing. |
55
|
|
|
*/ |
56
|
|
|
public function apply(AdvancedMedia $advancedMedia): void |
57
|
|
|
{ |
58
|
|
|
if ($this->format instanceof DefaultVideo) { |
59
|
|
|
$parameters = $this->format->getAdditionalParameters() ?: []; |
60
|
|
|
|
61
|
|
|
if (!in_array('-b:v', $parameters) && $this->format->getKiloBitrate() !== 0) { |
62
|
|
|
$parameters = array_merge(['-b:v', $this->format->getKiloBitrate() . 'k'], $parameters); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->format->setAdditionalParameters($parameters); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$advancedMedia->map($this->outs, $this->format, $this->output->getLocalPath(), $this->forceDisableAudio, $this->forceDisableVideo); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getFormat(): FormatInterface |
72
|
|
|
{ |
73
|
|
|
return $this->format; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getOutputMedia(): Media |
77
|
|
|
{ |
78
|
|
|
return $this->output; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function hasOut(string $out): bool |
82
|
|
|
{ |
83
|
|
|
return Collection::make($this->outs) |
84
|
|
|
->map(function ($out) { |
85
|
|
|
return HLSVideoFilters::beforeGlue($out); |
86
|
|
|
}) |
87
|
|
|
->contains(HLSVideoFilters::beforeGlue($out)); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: