VideoFrameExtractor::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 15
rs 9.9666
cc 4
nc 4
nop 2
1
<?php
2
3
4
namespace floor12\files\logic;
5
6
use ErrorException;
7
use Yii;
8
9
class VideoFrameExtractor
10
{
11
    /** @var string */
12
    private $ffmpegBin;
13
    /** @var string */
14
    private $videoSourceFilename;
15
    /** @var string */
16
    private $outputImageFilename;
17
18
    public function __construct(string $videoSourceFilename, string $outputImageFilename)
19
    {
20
        $this->ffmpegBin = Yii::$app->getModule('files')->ffmpeg;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getModule('files')->ffmpeg can also be of type object. However, the property $ffmpegBin is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
21
22
        if (!file_exists($this->ffmpegBin))
0 ignored issues
show
Bug introduced by
It seems like $this->ffmpegBin can also be of type null and object; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        if (!file_exists(/** @scrutinizer ignore-type */ $this->ffmpegBin))
Loading history...
23
            throw new ErrorException("ffmpeg is not found: {$this->ffmpegBin}");
24
25
        if (!is_executable($this->ffmpegBin))
0 ignored issues
show
Bug introduced by
It seems like $this->ffmpegBin can also be of type null and object; however, parameter $filename of is_executable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        if (!is_executable(/** @scrutinizer ignore-type */ $this->ffmpegBin))
Loading history...
26
            throw new ErrorException("ffmpeg is not executable: {$this->ffmpegBin}");
27
28
        if (!is_file($videoSourceFilename))
29
            throw new ErrorException('File not found on disk.');
30
31
        $this->videoSourceFilename = $videoSourceFilename;
32
        $this->outputImageFilename = $outputImageFilename;
33
    }
34
35
    /**
36
     * @throws ErrorException
37
     */
38
    public function extract()
39
    {
40
        $command = "{$this->ffmpegBin} -i {$this->videoSourceFilename}  -ss 00:00:05.000 -vframes 1  {$this->outputImageFilename}";
41
        exec($command, $out, $result);
42
        if (is_file($this->outputImageFilename)) {
43
            return true;
44
        }
45
46
        $command = "{$this->ffmpegBin} -i {$this->videoSourceFilename}  -ss 00:00:03.000 -vframes 1  {$this->outputImageFilename}";
47
        exec($command, $out, $result);
48
        if (is_file($this->outputImageFilename)) {
49
            return true;
50
        }
51
52
53
           $command = "{$this->ffmpegBin} -i {$this->videoSourceFilename}  -ss 00:00:01.000 -vframes 1  {$this->outputImageFilename}";
54
        exec($command, $out, $result);
55
        if (is_file($this->outputImageFilename)) {
56
            return true;
57
        }
58
59
        throw new ErrorException('FFmpeg frame extracting fail:' . print_r($out, true));
0 ignored issues
show
Bug introduced by
Are you sure print_r($out, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        throw new ErrorException('FFmpeg frame extracting fail:' . /** @scrutinizer ignore-type */ print_r($out, true));
Loading history...
60
    }
61
62
}
63