MediaScanner   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 7
eloc 46
dl 0
loc 72
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

2 Methods

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