Failed Conditions
Push — master ( 85c284...561f65 )
by Sébastien
02:44
created

InterlaceDetect::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 Soluble\MediaTools\Util\PlatformNullFile;
11
use Soluble\MediaTools\Video\Converter\FFMpegAdapter;
12
use Soluble\MediaTools\VideoConversionParams;
13
use Symfony\Component\Process\Exception\RuntimeException as SPRuntimeException;
14
use Symfony\Component\Process\Process;
15
16
class InterlaceDetect
17
{
18
    use PathAssertionsTrait;
19
20
    public const DEFAULT_INTERLACE_MAX_FRAMES = 1000;
21
22
    /** @var FFMpegConfig */
23
    protected $ffmpegConfig;
24
25
    /** @var FFMpegAdapter */
26
    protected $adapter;
27
28 2
    public function __construct(FFMpegConfig $ffmpegConfig)
29
    {
30 2
        $this->ffmpegConfig = $ffmpegConfig;
31 2
        $this->adapter      = new FFMpegAdapter($ffmpegConfig);
32 2
    }
33
34
    /**
35
     * @throws SPRuntimeException
36
     * @throws FileNotFoundException
37
     */
38 2
    public function guessInterlacing(string $file, int $maxFramesToAnalyze = self::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceDetectGuess
39
    {
40 2
        $this->ensureFileExists($file);
41
42 1
        $params = (new VideoConversionParams())
43 1
            ->withFilter('idet') // detect interlaced frames :)
44 1
            ->withVideoFrames($maxFramesToAnalyze)
45 1
            ->withNoAudio() // speed up the thing
46 1
            ->withOutputFormat('rawvideo')
47 1
            ->withOverwriteFile();
48
49 1
        $arguments = $this->adapter->getMappedConversionParams($params);
50 1
        $ffmpegCmd = $this->adapter->getCliCommand($arguments, $file, new PlatformNullFile());
51
52
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
53
        $ffmpegCmd = $ffmpegProcess->buildCommand(
54
            [
55
                sprintf('-i %s', escapeshellarg($file)),
56
                '-filter idet',
57
                sprintf('-frames:v %d', $maxFramesToAnalyze),
58
                '-an', // audio can be discarded
59
                '-f rawvideo', // tmp in raw
60
                '-y /dev/null', // discard the tmp
61
            ]
62
        );*/
63
64
        try {
65 1
            $process = new Process($ffmpegCmd);
66 1
            $process->mustRun();
67
        } catch (SPRuntimeException $e) {
68
            throw $e;
69
        }
70
71 1
        $stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
72
73
        // Counted frames
74 1
        $interlaced_tff = 0;
75 1
        $interlaced_bff = 0;
76 1
        $progressive    = 0;
77 1
        $undetermined   = 0;
78 1
        $total_frames   = 0;
79
80 1
        if ($stdErr !== false) {
81 1
            foreach ($stdErr as $line) {
82 1
                if (mb_substr($line, 0, 12) !== '[Parsed_idet') {
83 1
                    continue;
84
                }
85
86 1
                $unspaced = preg_replace('/( )+/', '', $line);
87 1
                $matches  = [];
88 1
                if (preg_match_all('/TFF:(\d+)BFF:(\d+)Progressive:(\d+)Undetermined:(\d+)/i', $unspaced, $matches) < 1) {
89 1
                    continue;
90
                }
91
92
                //$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...
93 1
                $interlaced_tff += (int) $matches[1][0];
94 1
                $interlaced_bff += (int) $matches[2][0];
95 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...
96 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...
97 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...
98
            }
99
        }
100
101 1
        return new InterlaceDetectGuess($interlaced_tff, $interlaced_bff, $progressive, $undetermined);
102
    }
103
}
104