1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP-FFmpeg-video-streaming package. |
5
|
|
|
* |
6
|
|
|
* (c) Amin Yazdanpanah <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace Streaming; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use FFMpeg\Filters\Audio\SimpleFilter; |
17
|
|
|
use FFMpeg\Format\VideoInterface; |
18
|
|
|
|
19
|
|
|
class CommandBuilder |
20
|
|
|
{ |
21
|
|
|
/** @var Media */ |
22
|
|
|
private $media; |
23
|
|
|
|
24
|
|
|
/** @var \FFMpeg\Filters\FiltersCollection */ |
25
|
|
|
private $filters; |
26
|
|
|
|
27
|
|
|
/** @var \FFMpeg\Driver\FFMpegDriver */ |
28
|
|
|
private $driver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* CommandBuilder constructor. |
32
|
|
|
* @param Media $media |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Media $media) |
35
|
|
|
{ |
36
|
|
|
$this->media = $media; |
37
|
|
|
$this->filters = clone $this->media->getFiltersCollection(); |
|
|
|
|
38
|
|
|
$this->driver = clone $this->media->getFFMpegDriver(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param VideoInterface $format |
43
|
|
|
* @param string $path |
44
|
|
|
* @return array |
45
|
|
|
* @TODO: optimize this function |
46
|
|
|
*/ |
47
|
|
|
public function build(VideoInterface $format, string $path): array |
48
|
|
|
{ |
49
|
|
|
$commands = []; |
50
|
|
|
$this->filters->add(new SimpleFilter($format->getExtraParams(), 10)); |
51
|
|
|
|
52
|
|
|
if ($this->driver->getConfiguration()->has('ffmpeg.threads')) { |
53
|
|
|
$this->filters->add(new SimpleFilter(['-threads', $this->driver->getConfiguration()->get('ffmpeg.threads')])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($this->filters as $filter) { |
57
|
|
|
$commands = array_merge($this->getInputOptions(), $filter->apply($this->media->baseMedia(), $format)); |
58
|
|
|
} |
59
|
|
|
array_push($commands, $path); |
60
|
|
|
|
61
|
|
|
return $commands; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
private function getInputOptions(): array |
68
|
|
|
{ |
69
|
|
|
$path = $this->media->getPathfile(); |
|
|
|
|
70
|
|
|
$input_options = Utiles::arrayToFFmpegOpt($this->media->getInputOptions()); |
71
|
|
|
|
72
|
|
|
return array_merge($input_options, ['-y', '-i', $path]); |
73
|
|
|
} |
74
|
|
|
} |