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
|
|
|
public function __construct(MediaToolsServiceInterface $mediaTools) |
23
|
|
|
{ |
24
|
|
|
$this->mediaTools = $mediaTools; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getVideoInfo(string $file): VideoInfoInterface |
28
|
|
|
{ |
29
|
|
|
if ($this->videoInfo === null) { |
30
|
|
|
$this->videoInfo = $this->mediaTools->getReader()->getInfo($file); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $this->videoInfo; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getParams(string $file, ?int $width = null, ?int $height = null): VideoConvertParams |
37
|
|
|
{ |
38
|
|
|
$info = $this->getVideoInfo($file); |
39
|
|
|
|
40
|
|
|
$firstAudio = $info->getAudioStreams()->getFirst(); |
41
|
|
|
$firstVideo = $info->getVideoStreams()->getFirst(); |
42
|
|
|
|
43
|
|
|
$filters = new VideoFilterChain(); |
44
|
|
|
|
45
|
|
|
if (($width !== null && $firstVideo->getWidth() !== $width) || |
46
|
|
|
($height !== null && $firstVideo->getHeight() !== $height)) { |
47
|
|
|
$filters->addFilter(new ScaleFilter($width, $height)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$params = (new VideoConvertParams()) |
51
|
|
|
->withVideoCodec('libx264') |
52
|
|
|
->withStreamable(true) |
53
|
|
|
->withVideoFilter($filters) |
54
|
|
|
->withOutputFormat('mp4'); |
55
|
|
|
|
56
|
|
|
if (mb_strtolower($firstAudio->getCodecName()) !== 'aac') { |
57
|
|
|
$params = $params->withAudioCodec('aac'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $params; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getFileExtension(): string |
64
|
|
|
{ |
65
|
|
|
return 'mp4'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|