Passed
Push — master ( e379f9...359fe7 )
by Sébastien
04:39
created

ConvertDirCommand::execute()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.1429

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 6
nop 2
dl 0
loc 44
ccs 18
cts 21
cp 0.8571
crap 7.1429
rs 8.6506
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 3
                    new InputOption('preset', 'p', InputOption::VALUE_REQUIRED),
53
                ])
54
            );
55 3
    }
56
57 3
    protected function execute(InputInterface $input, OutputInterface $output): int
58
    {
59 3
        if (!$input->hasOption('dir')) {
60
            throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>');
61
        }
62 3
        $directory = $input->getOption('dir');
63 3
        if (!is_string($directory) || !is_dir($directory)) {
64 2
            throw new \InvalidArgumentException(sprintf(
65 2
                'Directory %s does not exists',
66 2
                is_string($directory) ? $directory : json_encode($directory)
67
            ));
68
        }
69
70 1
        $output->writeln(sprintf('* Scanning %s for media files...', $directory));
71
72
        // Get the videos in path
73 1
        $files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions);
74
75 1
        $output->writeln('* Reading metadata...');
76
77 1
        $progressBar = new ProgressBar($output, count($files));
78
        //$progressBar->start();
79
80 1
        $preset = new StreamableH264Preset($this->mediaTools);
81
82
        /** @var \SplFileInfo $file */
83 1
        foreach ($files as $file) {
84
            try {
85
                //$preset->convert($file->getPathname());
86 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...
87
                //var_dump($params->toArray());
88
89 1
                $output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file));
90
            } catch (ProcessException $e) {
91
                $output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file));
92
            }
93
94
            //$progressBar->advance();
95
        }
96
97 1
        $progressBar->finish();
98 1
        $output->writeln('');
99
100 1
        return 0;
101
    }
102
}
103