|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jackal\Giffhanger\FFMpeg\ext\Filters; |
|
4
|
|
|
|
|
5
|
|
|
use FFMpeg\Filters\Video\VideoFilterInterface; |
|
6
|
|
|
use FFMpeg\Format\VideoInterface; |
|
7
|
|
|
use FFMpeg\Media\Video; |
|
8
|
|
|
|
|
9
|
|
|
class CropCenterFilter implements VideoFilterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
protected $priority; |
|
12
|
|
|
|
|
13
|
|
|
protected $cropRatio; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($cropRatio, $priority = 0) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->priority = $priority; |
|
18
|
|
|
$this->cropRatio = $cropRatio; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns the priority of the filter. |
|
23
|
|
|
* |
|
24
|
|
|
* @return integer |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getPriority() |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->priority; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Applies the filter on the the Video media given an format. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Video $video |
|
35
|
|
|
* @param VideoInterface $format |
|
36
|
|
|
* |
|
37
|
|
|
* @return array An array of arguments |
|
38
|
|
|
*/ |
|
39
|
|
|
public function apply(Video $video, VideoInterface $format) |
|
40
|
|
|
{ |
|
41
|
|
|
$videoWidth = 0; |
|
42
|
|
|
$videoHeight = 0; |
|
43
|
|
|
$videoRatio = 0; |
|
44
|
|
|
|
|
45
|
|
|
foreach ($video->getStreams()->videos() as $stream) { |
|
46
|
|
|
if ($stream->has('width') && $stream->has('height')) { |
|
47
|
|
|
$videoWidth = $stream->get('width'); |
|
48
|
|
|
$videoHeight = $stream->get('height'); |
|
49
|
|
|
$videoRatio = $videoWidth / $videoHeight; |
|
50
|
|
|
|
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($this->cropRatio > $videoRatio) { |
|
56
|
|
|
$cropWidth = $videoWidth; |
|
57
|
|
|
$cropHeight = round($videoWidth / $this->cropRatio); |
|
58
|
|
|
} else { |
|
59
|
|
|
$cropHeight = $videoHeight; |
|
60
|
|
|
$cropWidth = round($videoHeight * $this->cropRatio); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$cropX = round(($videoWidth - $cropWidth) / 2); |
|
64
|
|
|
$cropY = round(($videoHeight - $cropHeight) / 2); |
|
65
|
|
|
|
|
66
|
|
|
foreach ($video->getStreams()->videos() as $stream) { |
|
67
|
|
|
if ($stream->has('width') && $stream->has('height')) { |
|
68
|
|
|
$stream->set('width', $cropWidth); |
|
69
|
|
|
$stream->set('height', $cropHeight); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
'-filter:v', |
|
75
|
|
|
'crop=' . |
|
76
|
|
|
$cropWidth . ':' . $cropHeight . ':' . $cropX . ':' . $cropY, |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|