1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soluble\MediaTools; |
6
|
|
|
|
7
|
|
|
use Soluble\MediaTools\Config\FFMpegConfig; |
8
|
|
|
use Soluble\MediaTools\Exception\FileNotFoundException; |
9
|
|
|
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait; |
10
|
|
|
use Soluble\MediaTools\Video\Filter\EmptyVideoFilter; |
11
|
|
|
use Soluble\MediaTools\Video\Filter\VideoFilterInterface; |
12
|
|
|
use Soluble\MediaTools\Video\ThumbServiceInterface; |
13
|
|
|
|
14
|
|
|
class VideoThumbService implements ThumbServiceInterface |
15
|
|
|
{ |
16
|
|
|
use PathAssertionsTrait; |
17
|
|
|
|
18
|
|
|
/** @var FFMpegConfig */ |
19
|
|
|
protected $ffmpegConfig; |
20
|
|
|
|
21
|
2 |
|
public function __construct(FFMpegConfig $ffmpegConfig) |
22
|
|
|
{ |
23
|
2 |
|
$this->ffmpegConfig = $ffmpegConfig; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param null|VideoFilterInterface $videoFilter |
28
|
|
|
* |
29
|
|
|
* @throws FileNotFoundException |
30
|
|
|
*/ |
31
|
2 |
|
public function makeThumbnail(string $videoFile, string $outputFile, float $time = 0.0, ?VideoFilterInterface $videoFilter = null): void |
32
|
|
|
{ |
33
|
2 |
|
$this->ensureFileExists($videoFile); |
34
|
|
|
|
35
|
1 |
|
if ($videoFilter === null) { |
36
|
1 |
|
$videoFilter = new EmptyVideoFilter(); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$process = $this->ffmpegConfig->getProcess(); |
40
|
|
|
|
41
|
1 |
|
$ffmpegCmd = $process->buildCommand( |
42
|
|
|
[ |
43
|
1 |
|
($time > 0.0) ? sprintf('-ss %s', $time) : '', // putting time in front is much more efficient |
44
|
1 |
|
sprintf('-i %s', escapeshellarg($videoFile)), // input filename |
45
|
1 |
|
$videoFilter->getFFMpegCLIArgument(), // add -vf yadif,nlmeans |
46
|
1 |
|
'-frames:v 1', |
47
|
1 |
|
'-q:v 2', |
48
|
1 |
|
'-y', // tell to overwrite |
49
|
1 |
|
sprintf('%s', escapeshellarg($outputFile)), |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
|
53
|
1 |
|
$process->runCommand($ffmpegCmd); |
54
|
1 |
|
} |
55
|
|
|
} |
56
|
|
|
|