|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools\Preset\MP4; |
|
6
|
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Cli\Service\MediaToolsServiceInterface; |
|
8
|
|
|
use Soluble\MediaTools\Preset\PresetInterface; |
|
9
|
|
|
use Soluble\MediaTools\Video\Filter\ScaleFilter; |
|
10
|
|
|
use Soluble\MediaTools\Video\Filter\VideoFilterChain; |
|
11
|
|
|
use Soluble\MediaTools\Video\VideoConvertParams; |
|
12
|
|
|
use Soluble\MediaTools\Video\VideoInfoInterface; |
|
13
|
|
|
|
|
14
|
|
|
class StreamableH264Preset implements PresetInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var MediaToolsServiceInterface */ |
|
17
|
|
|
private $mediaTools; |
|
18
|
|
|
|
|
19
|
|
|
/** @var VideoInfoInterface */ |
|
20
|
|
|
private $videoInfo; |
|
21
|
|
|
|
|
22
|
1 |
|
public function __construct(MediaToolsServiceInterface $mediaTools) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->mediaTools = $mediaTools; |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getName(): string |
|
28
|
|
|
{ |
|
29
|
|
|
return __CLASS__; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
private function getVideoInfo(string $file): VideoInfoInterface |
|
33
|
|
|
{ |
|
34
|
1 |
|
if ($this->videoInfo === null) { |
|
35
|
1 |
|
$this->videoInfo = $this->mediaTools->getReader()->getInfo($file); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
return $this->videoInfo; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function getParams(string $file, ?int $width = null, ?int $height = null): VideoConvertParams |
|
42
|
|
|
{ |
|
43
|
1 |
|
$info = $this->getVideoInfo($file); |
|
44
|
|
|
|
|
45
|
1 |
|
$firstAudio = $info->getAudioStreams()->getFirst(); |
|
46
|
1 |
|
$firstVideo = $info->getVideoStreams()->getFirst(); |
|
47
|
|
|
|
|
48
|
1 |
|
$filters = new VideoFilterChain(); |
|
49
|
|
|
|
|
50
|
1 |
|
if (($width !== null && $firstVideo->getWidth() !== $width) || |
|
51
|
1 |
|
($height !== null && $firstVideo->getHeight() !== $height)) { |
|
52
|
|
|
$filters->addFilter(new ScaleFilter($width, $height)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
$params = (new VideoConvertParams()) |
|
56
|
1 |
|
->withVideoCodec('libx264') |
|
57
|
1 |
|
->withStreamable(true) |
|
58
|
1 |
|
->withVideoFilter($filters) |
|
59
|
1 |
|
->withOutputFormat('mp4'); |
|
60
|
|
|
|
|
61
|
1 |
|
if (mb_strtolower($firstAudio->getCodecName()) !== 'aac') { |
|
62
|
|
|
$params = $params->withAudioCodec('aac'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $params; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|