Completed
Push — master ( e1d839...586d5f )
by Sébastien
07:35 queued 05:11
created

InterlaceDetect   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 83
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B guessInterlacing() 0 58 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Detection;
6
7
use Soluble\MediaTools\Common\Assert\PathAssertionsTrait;
8
use Soluble\MediaTools\Common\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Common\Exception\UnsupportedParamException;
10
use Soluble\MediaTools\Common\Exception\UnsupportedParamValueException;
11
use Soluble\MediaTools\Common\IO\PlatformNullFile;
12
use Soluble\MediaTools\Common\Process\ProcessParamsInterface;
13
use Soluble\MediaTools\Config\FFMpegConfigInterface;
14
use Soluble\MediaTools\Video\Adapter\FFMpegAdapter;
15
use Soluble\MediaTools\Video\ConversionParams;
16
use Soluble\MediaTools\Video\Exception\DetectionExceptionInterface;
17
use Soluble\MediaTools\Video\Exception\DetectionProcessExceptionInterface;
18
use Soluble\MediaTools\Video\Exception\InvalidParamException;
19
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
20
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
21
use Soluble\MediaTools\Video\Exception\RuntimeException;
22
use Soluble\MediaTools\Video\Filter\IdetVideoFilter;
23
use Symfony\Component\Process\Exception as SPException;
24
use Symfony\Component\Process\Process;
25
26
class InterlaceDetect
27
{
28
    use PathAssertionsTrait;
29
30
    public const DEFAULT_INTERLACE_MAX_FRAMES = 1000;
31
32
    /** @var ProcessParamsInterface */
33
    protected $processParams;
34
35
    /** @var FFMpegAdapter */
36
    protected $adapter;
37
38
    public function __construct(FFMpegConfigInterface $ffmpegConfig)
39
    {
40
        $this->processParams = $ffmpegConfig;
41
        $this->adapter       = new FFMpegAdapter($ffmpegConfig);
42
    }
43
44
    /**
45
     * @throws DetectionExceptionInterface
46
     * @throws DetectionProcessExceptionInterface
47
     * @throws ProcessFailedException
48
     * @throws MissingInputFileException
49
     * @throws RuntimeException
50
     */
51
    public function guessInterlacing(string $file, int $maxFramesToAnalyze = self::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceDetectGuess
52
    {
53
        $params = (new ConversionParams())
54
            ->withVideoFilter(new IdetVideoFilter()) // detect interlaced frames :)
55
            ->withVideoFrames($maxFramesToAnalyze)
56
            ->withNoAudio() // speed up the thing
57
            ->withOutputFormat('rawvideo')
58
            ->withOverwrite();
59
60
        try {
61
            $this->ensureFileExists($file);
62
63
            $arguments = $this->adapter->getMappedConversionParams($params);
64
            $ffmpegCmd = $this->adapter->getCliCommand($arguments, $file, new PlatformNullFile());
65
66
            $process = new Process($ffmpegCmd);
67
            $process->mustRun();
68
        } catch (FileNotFoundException $e) {
69
            throw new MissingInputFileException($e->getMessage());
70
        } catch (UnsupportedParamValueException | UnsupportedParamException $e) {
71
            throw new InvalidParamException($e->getMessage());
72
        } catch (SPException\ProcessFailedException | SPException\ProcessTimedOutException | SPException\ProcessSignaledException $e) {
73
            throw new ProcessFailedException($e->getProcess(), $e);
74
        } catch (SPException\RuntimeException $e) {
75
            throw new RuntimeException($e->getMessage());
76
        }
77
78
        $stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
79
80
        // Counted frames
81
        $interlaced_tff = 0;
82
        $interlaced_bff = 0;
83
        $progressive    = 0;
84
        $undetermined   = 0;
85
        $total_frames   = 0;
86
87
        if ($stdErr !== false) {
88
            foreach ($stdErr as $line) {
89
                if (mb_substr($line, 0, 12) !== '[Parsed_idet') {
90
                    continue;
91
                }
92
93
                $unspaced = preg_replace('/( )+/', '', $line);
94
                $matches  = [];
95
                if (preg_match_all('/TFF:(\d+)BFF:(\d+)Progressive:(\d+)Undetermined:(\d+)/i', $unspaced, $matches) < 1) {
96
                    continue;
97
                }
98
99
                //$type = strpos(strtolower($unspaced), 'single') ? 'single' : 'multi';
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
                $interlaced_tff += (int) $matches[1][0];
101
                $interlaced_bff += (int) $matches[2][0];
102
                $progressive += (int) $matches[3][0];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
103
                $undetermined += (int) $matches[4][0];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104
                $total_frames += ((int) $matches[1][0] + (int) $matches[2][0] + (int) $matches[3][0] + (int) $matches[4][0]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
            }
106
        }
107
108
        return new InterlaceDetectGuess($interlaced_tff, $interlaced_bff, $progressive, $undetermined);
109
    }
110
}
111