Passed
Push — master ( d1942c...ad5186 )
by Sébastien
03:08
created

InterlaceDetect   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 7
eloc 39
dl 0
loc 73
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B guessInterlacing() 0 55 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Detection;
6
7
use Soluble\MediaTools\Config\FFMpegConfig;
8
use Soluble\MediaTools\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
10
use Symfony\Component\Process\Exception\RuntimeException as SPRuntimeException;
11
use Symfony\Component\Process\Process;
12
13
class InterlaceDetect
14
{
15
    use PathAssertionsTrait;
16
17
    public const DEFAULT_INTERLACE_MAX_FRAMES = 1000;
18
19
    /** @var FFMpegConfig */
20
    protected $ffmpegConfig;
21
22 1
    public function __construct(FFMpegConfig $ffmpegConfig)
23
    {
24 1
        $this->ffmpegConfig = $ffmpegConfig;
25 1
    }
26
27
    /**
28
     * @throws SPRuntimeException
29
     * @throws FileNotFoundException
30
     */
31 1
    public function guessInterlacing(string $file, int $maxFramesToAnalyze = self::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceGuess
32
    {
33 1
        $this->ensureFileExists($file);
34
35 1
        $ffmpegProcess = $this->ffmpegConfig->getProcess();
36
37 1
        $ffmpegCmd = $ffmpegProcess->buildCommand(
38
            [
39 1
                sprintf('-i %s', escapeshellarg($file)),
40 1
                '-filter idet',
41 1
                sprintf('-frames:v %d', $maxFramesToAnalyze),
42 1
                '-an', // audio can be discarded
43 1
                '-f rawvideo', // output in raw
44 1
                '-y /dev/null', // discard the output
45
            ]
46
        );
47
48
        try {
49 1
            $process = new Process($ffmpegCmd);
50 1
            $process->mustRun();
51
        } catch (SPRuntimeException $e) {
52
            throw $e;
53
        }
54
55 1
        $stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
56
57
        // Counted frames
58 1
        $interlaced_tff = 0;
59 1
        $interlaced_bff = 0;
60 1
        $progressive    = 0;
61 1
        $undetermined   = 0;
62 1
        $total_frames   = 0;
63
64 1
        if ($stdErr !== false) {
65 1
            foreach ($stdErr as $line) {
66 1
                if (mb_substr($line, 0, 12) !== '[Parsed_idet') {
67 1
                    continue;
68
                }
69
70 1
                $unspaced = preg_replace('/( )+/', '', $line);
71 1
                $matches  = [];
72 1
                if (preg_match_all('/TFF:(\d+)BFF:(\d+)Progressive:(\d+)Undetermined:(\d+)/i', $unspaced, $matches) < 1) {
73 1
                    continue;
74
                }
75
76
                //$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...
77 1
                $interlaced_tff += (int) $matches[1][0];
78 1
                $interlaced_bff += (int) $matches[2][0];
79 1
                $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...
80 1
                $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...
81 1
                $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...
82
            }
83
        }
84
85 1
        return new InterlaceGuess($interlaced_tff, $interlaced_bff, $progressive, $undetermined);
86
    }
87
}
88