|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Exporters; |
|
4
|
|
|
|
|
5
|
|
|
use FFMpeg\Coordinate\Dimension; |
|
6
|
|
|
use FFMpeg\Filters\AdvancedMedia\ComplexFilters; |
|
7
|
|
|
use FFMpeg\Filters\Video\ResizeFilter; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg; |
|
10
|
|
|
use ProtoneMedia\LaravelFFMpeg\Filters\WatermarkFactory; |
|
11
|
|
|
|
|
12
|
|
|
class HLSVideoFilters |
|
13
|
|
|
{ |
|
14
|
|
|
const MAPPING_GLUE = "_hls_"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg |
|
18
|
|
|
*/ |
|
19
|
|
|
private $driver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Key of the video in the HLS export. |
|
23
|
|
|
* |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $formatKey; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Number of filters added to this video. |
|
30
|
|
|
* |
|
31
|
|
|
* @var integer |
|
32
|
|
|
*/ |
|
33
|
|
|
private $filterCount = 0; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(PHPFFMpeg $driver, int $formatKey) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->driver = $driver; |
|
38
|
|
|
$this->formatKey = $formatKey; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function count(): int |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->filterCount; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function increaseFilterCount(): self |
|
47
|
|
|
{ |
|
48
|
|
|
$this->filterCount++; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Generates an input mapping for a new filter. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
private function input(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->filterCount ? static::glue($this->formatKey, $this->filterCount) : '[0]'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Generates an output mapping for a new filter. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
private function output(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return static::glue($this->formatKey, $this->filterCount + 1); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds a filter as a complex filter. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function addLegacyFilter(...$arguments): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->driver->addFilterAsComplexFilter($this->input(), $this->output(), ...$arguments); |
|
79
|
|
|
|
|
80
|
|
|
return $this->increaseFilterCount(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Shortcut for the ResizeFilter. |
|
85
|
|
|
* |
|
86
|
|
|
* @param int $width |
|
87
|
|
|
* @param int $height |
|
88
|
|
|
* @param string $mode |
|
89
|
|
|
* @param boolean $forceStandards |
|
90
|
|
|
* @return self |
|
91
|
|
|
*/ |
|
92
|
|
|
public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT, $forceStandards = true): self |
|
93
|
|
|
{ |
|
94
|
|
|
$dimension = new Dimension($width, $height); |
|
95
|
|
|
|
|
96
|
|
|
$filter = new ResizeFilter($dimension, $mode, $forceStandards); |
|
97
|
|
|
|
|
98
|
|
|
return $this->addLegacyFilter($filter); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Shortcut for the WatermarkFactory. |
|
103
|
|
|
* |
|
104
|
|
|
* @param callable $withWatermarkFactory |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
|
|
public function addWatermark(callable $withWatermarkFactory): self |
|
108
|
|
|
{ |
|
109
|
|
|
$withWatermarkFactory($watermarkFactory = new WatermarkFactory); |
|
110
|
|
|
|
|
111
|
|
|
return $this->addLegacyFilter($watermarkFactory->get()); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Adds a scale filter to the video, will be replaced in favor of resize(). |
|
116
|
|
|
* |
|
117
|
|
|
* @param int $width |
|
118
|
|
|
* @param int $height |
|
119
|
|
|
* @deprecated 7.4.0 |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
|
|
public function scale($width, $height): self |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->addFilter("scale={$width}:{$height}"); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Adds a filter object or a callable to the driver and automatically |
|
129
|
|
|
* chooses the right input and output mapping. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function addFilter(...$arguments): self |
|
132
|
|
|
{ |
|
133
|
|
|
if (count($arguments) === 1 && !is_callable($arguments[0])) { |
|
134
|
|
|
$this->driver->addFilter($this->input(), $arguments[0], $this->output()); |
|
135
|
|
|
} else { |
|
136
|
|
|
$this->driver->addFilter(function (ComplexFilters $filters) use ($arguments) { |
|
137
|
|
|
$arguments[0]($filters, $this->input(), $this->output()); |
|
138
|
|
|
}); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->increaseFilterCount(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public static function glue($format, $filter): string |
|
145
|
|
|
{ |
|
146
|
|
|
return "[v{$format}" . static::MAPPING_GLUE . "{$filter}]"; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function beforeGlue($input): string |
|
150
|
|
|
{ |
|
151
|
|
|
return Str::before($input, static::MAPPING_GLUE); |
|
152
|
|
|
} |
|
153
|
|
|
}; |
|
154
|
|
|
|