|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @see https://github.com/soluble-io/soluble-mediatools for the canonical repository |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude) |
|
9
|
|
|
* @license https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Soluble\MediaTools\Video; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\LogLevel; |
|
16
|
|
|
use Psr\Log\NullLogger; |
|
17
|
|
|
use Soluble\MediaTools\Common\Process\ProcessParamsInterface; |
|
18
|
|
|
use Soluble\MediaTools\Video\Config\FFMpegConfigInterface; |
|
19
|
|
|
use Soluble\MediaTools\Video\Detection\InterlaceDetect; |
|
20
|
|
|
use Soluble\MediaTools\Video\Detection\InterlaceDetectGuess; |
|
21
|
|
|
use Soluble\MediaTools\Video\Exception\AnalyzerExceptionInterface; |
|
22
|
|
|
use Soluble\MediaTools\Video\Exception\AnalyzerProcessExceptionInterface; |
|
23
|
|
|
use Soluble\MediaTools\Video\Exception\MissingInputFileException; |
|
24
|
|
|
use Soluble\MediaTools\Video\Exception\ProcessFailedException; |
|
25
|
|
|
use Soluble\MediaTools\Video\Exception\RuntimeReaderException; |
|
26
|
|
|
|
|
27
|
|
|
final class VideoAnalyzer implements VideoAnalyzerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var FFMpegConfigInterface */ |
|
30
|
|
|
private $ffmpegConfig; |
|
31
|
|
|
|
|
32
|
|
|
/** @var LoggerInterface */ |
|
33
|
|
|
private $logger; |
|
34
|
3 |
|
|
|
35
|
|
|
public function __construct(FFMpegConfigInterface $ffmpegConfig, ?LoggerInterface $logger = null) |
|
36
|
3 |
|
{ |
|
37
|
3 |
|
$this->ffmpegConfig = $ffmpegConfig; |
|
38
|
3 |
|
$this->logger = $logger ?? new NullLogger(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param int $maxFramesToAnalyze interlacement detection can be heavy, limit the number of frames to analyze |
|
43
|
|
|
* |
|
44
|
|
|
* @throws AnalyzerExceptionInterface |
|
45
|
|
|
* @throws AnalyzerProcessExceptionInterface |
|
46
|
|
|
* @throws ProcessFailedException |
|
47
|
|
|
* @throws MissingInputFileException |
|
48
|
|
|
* @throws RuntimeReaderException |
|
49
|
2 |
|
*/ |
|
50
|
|
|
public function detectInterlacement(string $file, int $maxFramesToAnalyze = InterlaceDetect::DEFAULT_INTERLACE_MAX_FRAMES, ?ProcessParamsInterface $processParams = null): InterlaceDetectGuess |
|
51
|
2 |
|
{ |
|
52
|
|
|
$interlaceDetect = new InterlaceDetect($this->ffmpegConfig); |
|
53
|
2 |
|
|
|
54
|
|
|
try { |
|
55
|
|
|
return $interlaceDetect->guessInterlacing($file, $maxFramesToAnalyze, $processParams); |
|
56
|
|
|
} catch (\Throwable $e) { |
|
57
|
|
|
$exceptionNs = explode('\\', get_class($e)); |
|
58
|
|
|
$this->logger->log( |
|
59
|
|
|
($e instanceof MissingInputFileException) ? LogLevel::WARNING : LogLevel::ERROR, |
|
60
|
|
|
sprintf( |
|
61
|
|
|
'VideoAnalyzer %s: \'%s\'. (%s)', |
|
62
|
|
|
$exceptionNs[count($exceptionNs) - 1], |
|
63
|
|
|
$file, |
|
64
|
|
|
$e->getMessage() |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
throw $e; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|