Completed
Push — master ( 94616b...d1942c )
by Sébastien
03:09
created

InterlaceDetect::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
    /** @var FFMpegConfig */
18
    protected $ffmpegConfig;
19
20
    public function __construct(FFMpegConfig $ffmpegConfig)
21
    {
22
        $this->ffmpegConfig = $ffmpegConfig;
23
    }
24
25
    /**
26
     * @throws SPRuntimeException
27
     * @throws FileNotFoundException
28
     */
29
    public function guessInterlacing(string $file, int $maxFramesToAnalyze = 1000): InterlaceGuess
30
    {
31
        $this->ensureFileExists($file);
32
33
        $ffmpegProcess = $this->ffmpegConfig->getProcess();
34
35
        $ffmpegCmd = $ffmpegProcess->buildCommand(
36
            [
37
                sprintf('-i %s', escapeshellarg($file)),
38
                '-filter idet',
39
                sprintf('-frames:v %d', $maxFramesToAnalyze),
40
                '-an', // audio can be discarded
41
                '-f rawvideo', // output in raw
42
                '-y /dev/null', // discard the output
43
            ]
44
        );
45
46
        try {
47
            $process = new Process($ffmpegCmd);
48
            $process->mustRun();
49
        } catch (SPRuntimeException $e) {
50
            throw $e;
51
        }
52
53
        $stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
54
55
        // Counted frames
56
        $interlaced_tff = 0;
57
        $interlaced_bff = 0;
58
        $progressive    = 0;
59
        $undetermined   = 0;
60
        $total_frames   = 0;
61
62
        if ($stdErr !== false) {
63
            foreach ($stdErr as $line) {
64
                if (mb_substr($line, 0, 12) !== '[Parsed_idet') {
65
                    continue;
66
                }
67
68
                $unspaced = preg_replace('/( )+/', '', $line);
69
                $matches  = [];
70
                if (preg_match_all('/TFF:(\d+)BFF:(\d+)Progressive:(\d+)Undetermined:(\d+)/i', $unspaced, $matches) < 1) {
71
                    continue;
72
                }
73
74
                //$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...
75
                $interlaced_tff += (int) $matches[1][0];
76
                $interlaced_bff += (int) $matches[2][0];
77
                $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...
78
                $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...
79
                $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...
80
            }
81
        }
82
83
        return new InterlaceGuess($interlaced_tff, $interlaced_bff, $progressive, $undetermined);
84
    }
85
}
86