Failed Conditions
Push — master ( ce8891...5de0ef )
by Sébastien
02:14
created

MediaScanner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
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\Cli\Media;
6
7
use ScriptFUSION\Byte\ByteFormatter;
8
use Soluble\MediaTools\Cli\Exception\MissingFFProbeBinaryException;
9
use Soluble\MediaTools\Video\Exception as VideoException;
10
use Soluble\MediaTools\Video\SeekTime;
11
use Soluble\MediaTools\Video\VideoInfoReaderInterface;
12
use SplFileInfo;
13
14
class MediaScanner
15
{
16
    /**
17
     * @var VideoInfoReaderInterface
18
     */
19
    private $reader;
20
21 2
    public function __construct(VideoInfoReaderInterface $videoInfoReader)
22
    {
23 2
        $this->reader = $videoInfoReader;
24 2
    }
25
26
    /**
27
     * @param SplFileInfo[] $files
28
     */
29 2
    public function getMedias(array $files, ?callable $callback): array
30
    {
31 2
        $bitRateFormatter = new ByteFormatter();
32 2
        $sizeFormatter    = new ByteFormatter();
33
34 2
        $rows      = [];
35 2
        $warnings  = [];
36 2
        $totalSize = 0;
37
38
        /** @var SplFileInfo $file */
39 2
        foreach ($files as $file) {
40 2
            $videoFile = $file->getPathname();
41
            try {
42 2
                $info     = $this->reader->getInfo($videoFile);
43 1
                $vStream  = $info->getVideoStreams()->getFirst();
44 1
                $aStream  = $info->getAudioStreams()->getFirst();
45 1
                $pixFmt   = $vStream->getPixFmt();
46 1
                $bitRate  = $vStream->getBitRate();
47 1
                $fileSize = $file->getSize();
48
49 1
                $fps = (string) ($vStream->getFps(0) ?? '');
50
51
                $row = [
52 1
                    'video'      => $file,
53 1
                    'duration'   => preg_replace('/\.([0-9])+$/', '', SeekTime::convertSecondsToHMSs(round($info->getDuration(), 1))),
54 1
                    'codec'      => sprintf('%s/%s', $vStream->getCodecName(), $aStream->getCodecName()),
55 1
                    'resolution' => sprintf(
56 1
                        '%sx%s',
57 1
                        $vStream->getWidth(),
58 1
                        $vStream->getHeight()
59
                    ),
60 1
                    'fps'     => $fps,
61 1
                    'bitrate' => ($bitRate > 0 ? $bitRateFormatter->format((int) $bitRate) . '/s' : ''),
62 1
                    'size'    => $sizeFormatter->format($fileSize),
63 1
                    'pixFmt'  => $pixFmt,
64
                ];
65 1
                $rows[] = $row;
66 1
                $totalSize += $fileSize;
67 2
            } catch (VideoException\MissingFFProbeBinaryException $e) {
68 1
                throw new MissingFFProbeBinaryException('Unable to run ffprobe binary, check your config and ensure it\'s properly installed');
69 1
            } catch (VideoException\InfoReaderExceptionInterface $e) {
70 1
                $warnings[] = [$videoFile];
71
            }
72
73 1
            if ($callback !== null) {
74 1
                $callback();
75
            }
76
        }
77
78
        return [
79 1
            'rows'      => $rows,
80 1
            'totalSize' => $totalSize,
81 1
            'warnings'  => $warnings
82
        ];
83
    }
84
}
85