1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelFFMpeg\Filters; |
4
|
|
|
|
5
|
|
|
use FFMpeg\Filters\Video\WatermarkFilter as FFMpegWatermarkFilter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Partly based on this PR: |
9
|
|
|
* https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/754/files |
10
|
|
|
*/ |
11
|
|
|
class WatermarkFilter extends FFMpegWatermarkFilter |
12
|
|
|
{ |
13
|
|
|
protected $path; |
14
|
|
|
protected $wrapInParentheses = false; |
15
|
|
|
|
16
|
|
|
public function __construct($watermarkPath, array $coordinates = [], $priority = 0) |
17
|
|
|
{ |
18
|
|
|
parent::__construct($watermarkPath, $coordinates, $priority); |
19
|
|
|
|
20
|
|
|
$this->path = $watermarkPath; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function wrapInParentheses(): self |
24
|
|
|
{ |
25
|
|
|
$this->wrapInParentheses = true; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the commands from the base filter and normalizes the path. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
protected function getCommands() |
36
|
|
|
{ |
37
|
|
|
$commands = parent::getCommands(); |
38
|
|
|
|
39
|
|
|
$replace = static::normalizePath($this->path); |
40
|
|
|
|
41
|
|
|
if ($this->wrapInParentheses) { |
42
|
|
|
$replace = "'{$replace}'"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$commands[1] = str_replace($this->path, $replace, $commands[1]); |
46
|
|
|
|
47
|
|
|
return $commands; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Normalizes the path when running on Windows. |
52
|
|
|
* |
53
|
|
|
* @param string $path |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public static function normalizePath(string $path): string |
57
|
|
|
{ |
58
|
|
|
return windows_os() ? static::normalizeWindowsPath($path) : $path; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Replaces the slashes and escapes the colon. |
63
|
|
|
* |
64
|
|
|
* @param string $path |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function normalizeWindowsPath(string $path): string |
68
|
|
|
{ |
69
|
|
|
$path = str_replace('/', '\\\\', $path); |
70
|
|
|
$path = str_replace(':', '\\:', $path); |
71
|
|
|
|
72
|
|
|
return $path; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|