Completed
Push — master ( e0e3f0...3077c0 )
by Sébastien
04:19
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\FFMpegConfigInterface;
8
use Soluble\MediaTools\Exception\FileNotFoundException;
9
use Soluble\MediaTools\Exception\UnsupportedParamException;
10
use Soluble\MediaTools\Exception\UnsupportedParamValueException;
11
use Soluble\MediaTools\Util\Assert\PathAssertionsTrait;
12
use Soluble\MediaTools\Util\PlatformNullFile;
13
use Soluble\MediaTools\Video\Converter\FFMpegAdapter;
14
use Soluble\MediaTools\Video\Exception\DetectionExceptionInterface;
15
use Soluble\MediaTools\Video\Exception\DetectionProcessExceptionInterface;
16
use Soluble\MediaTools\Video\Exception\InvalidParamException;
17
use Soluble\MediaTools\Video\Exception\MissingInputFileException;
18
use Soluble\MediaTools\Video\Exception\ProcessFailedException;
19
use Soluble\MediaTools\Video\Exception\RuntimeException;
20
use Soluble\MediaTools\Video\Filter\IdetVideoFilter;
21
use Soluble\MediaTools\VideoConversionParams;
22
use Symfony\Component\Process\Exception as SPException;
23
use Symfony\Component\Process\Process;
24
25
class InterlaceDetect
26
{
27
    use PathAssertionsTrait;
28
29
    public const DEFAULT_INTERLACE_MAX_FRAMES = 1000;
30
31
    /** @var FFMpegConfigInterface */
32
    protected $ffmpegConfig;
33
34
    /** @var FFMpegAdapter */
35
    protected $adapter;
36
37 2
    public function __construct(FFMpegConfigInterface $ffmpegConfig)
38
    {
39 2
        $this->ffmpegConfig = $ffmpegConfig;
40 2
        $this->adapter      = new FFMpegAdapter($ffmpegConfig);
41 2
    }
42
43
    /**
44
     * @throws DetectionExceptionInterface
45
     * @throws DetectionProcessExceptionInterface
46
     * @throws ProcessFailedException
47
     * @throws MissingInputFileException
48
     * @throws RuntimeException
49
     */
50 2
    public function guessInterlacing(string $file, int $maxFramesToAnalyze = self::DEFAULT_INTERLACE_MAX_FRAMES): InterlaceDetectGuess
51
    {
52 2
        $params = (new VideoConversionParams())
53 2
            ->withVideoFilter(new IdetVideoFilter()) // detect interlaced frames :)
54 2
            ->withVideoFrames($maxFramesToAnalyze)
55 2
            ->withNoAudio() // speed up the thing
56 2
            ->withOutputFormat('rawvideo')
57 2
            ->withOverwrite();
58
59
        try {
60 2
            $this->ensureFileExists($file);
61
62 1
            $arguments = $this->adapter->getMappedConversionParams($params);
63 1
            $ffmpegCmd = $this->adapter->getCliCommand($arguments, $file, new PlatformNullFile());
64
65 1
            $process = new Process($ffmpegCmd);
66 1
            $process->mustRun();
67 1
        } catch (FileNotFoundException $e) {
68 1
            throw new MissingInputFileException($e->getMessage());
69
        } catch (UnsupportedParamValueException | UnsupportedParamException $e) {
70
            throw new InvalidParamException($e->getMessage());
71
        } catch (SPException\ProcessFailedException | SPException\ProcessTimedOutException | SPException\ProcessSignaledException $e) {
72
            throw new ProcessFailedException($e->getProcess(), $e);
73
        } catch (SPException\RuntimeException $e) {
74
            throw new RuntimeException($e->getMessage());
75
        }
76
77 1
        $stdErr = preg_split("/(\r\n|\n|\r)/", $process->getErrorOutput());
78
79
        // Counted frames
80 1
        $interlaced_tff = 0;
81 1
        $interlaced_bff = 0;
82 1
        $progressive    = 0;
83 1
        $undetermined   = 0;
84 1
        $total_frames   = 0;
85
86 1
        if ($stdErr !== false) {
87 1
            foreach ($stdErr as $line) {
88 1
                if (mb_substr($line, 0, 12) !== '[Parsed_idet') {
89 1
                    continue;
90
                }
91
92 1
                $unspaced = preg_replace('/( )+/', '', $line);
93 1
                $matches  = [];
94 1
                if (preg_match_all('/TFF:(\d+)BFF:(\d+)Progressive:(\d+)Undetermined:(\d+)/i', $unspaced, $matches) < 1) {
95 1
                    continue;
96
                }
97
98
                //$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...
99 1
                $interlaced_tff += (int) $matches[1][0];
100 1
                $interlaced_bff += (int) $matches[2][0];
101 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...
102 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...
103 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...
104
            }
105
        }
106
107 1
        return new InterlaceDetectGuess($interlaced_tff, $interlaced_bff, $progressive, $undetermined);
108
    }
109
}
110