Passed
Push — master ( c9bc4c...55b9e7 )
by Sébastien
04:18 queued 22s
created

ConvertDirCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools-cli for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2019 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools-cli/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Cli\Command;
13
14
use Soluble\MediaTools\Cli\FileSystem\DirectoryScanner;
15
use Soluble\MediaTools\Cli\Media\FileExtensions;
16
use Soluble\MediaTools\Cli\Service\MediaToolsServiceInterface;
17
use Soluble\MediaTools\Common\Exception\ProcessException;
18
use Soluble\MediaTools\Preset\MP4\StreamableH264Preset;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Helper\ProgressBar;
21
use Symfony\Component\Console\Input\InputDefinition;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class ConvertDirCommand extends Command
27
{
28
    /** @var MediaToolsServiceInterface */
29
    private $mediaTools;
30
31
    /** @var string[] */
32
    protected $supportedVideoExtensions;
33
34 3
    public function __construct(MediaToolsServiceInterface $mediaTools)
35
    {
36 3
        $this->mediaTools               = $mediaTools;
37 3
        $this->supportedVideoExtensions = (new FileExtensions())->getMediaExtensions();
38 3
        parent::__construct();
39 3
    }
40
41
    /**
42
     * Configures the command.
43
     */
44 3
    protected function configure(): void
45
    {
46
        $this
47 3
            ->setName('convert:directory')
48 3
            ->setDescription('Convert, transcode all media files in a directory')
49 3
            ->setDefinition(
50 3
                new InputDefinition([
51 3
                    new InputOption('dir', 'd', InputOption::VALUE_REQUIRED),
52
                ])
53
            );
54 3
    }
55
56 3
    protected function execute(InputInterface $input, OutputInterface $output): int
57
    {
58 3
        if (!$input->hasOption('dir')) {
59
            throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>');
60
        }
61 3
        $directory = $input->hasOption('dir') ? $input->getOption('dir') : null;
62 3
        if (!is_string($directory) || !is_dir($directory)) {
63 2
            throw new \InvalidArgumentException(sprintf(
64 2
                'Directory %s does not exists',
65 2
                is_string($directory) ? $directory : json_encode($directory)
66
            ));
67
        }
68
69 1
        $output->writeln(sprintf('* Scanning %s for media files...', $directory));
70
71
        // Get the videos in path
72 1
        $files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions);
73
74 1
        $output->writeln('* Reading metadata...');
75
76 1
        $progressBar = new ProgressBar($output, count($files));
77
        //$progressBar->start();
78
79 1
        $preset = new StreamableH264Preset($this->mediaTools);
80
81
        /** @var \SplFileInfo $file */
82 1
        foreach ($files as $file) {
83
            try {
84
                //$preset->convert($file->getPathname());
85 1
                $params = $preset->getParams($file->getPathname());
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
86
                //var_dump($params->toArray());
87
88 1
                $output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file));
89
            } catch (ProcessException $e) {
90
                $output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file));
91
            }
92
93
            //$progressBar->advance();
94
        }
95
96 1
        $progressBar->finish();
97 1
        $output->writeln('');
98
99 1
        return 0;
100
    }
101
}
102