Failed Conditions
Push — master ( 874a26...b85ace )
by Sébastien
01:57
created

ConvertDirCommand::execute()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 44
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.1624

Importance

Changes 0
Metric Value
cc 8
eloc 22
nc 13
nop 2
dl 0
loc 44
ccs 19
cts 22
cp 0.8636
crap 8.1624
rs 8.4444
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\Service\MediaToolsServiceInterface;
16
use Soluble\MediaTools\Common\Exception\ProcessException;
17
use Soluble\MediaTools\Preset\MP4\StreamableH264Preset;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\ProgressBar;
20
use Symfony\Component\Console\Input\InputDefinition;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
class ConvertDirCommand extends Command
26
{
27
    /** @var MediaToolsServiceInterface */
28
    private $mediaTools;
29
30
    /** @var string[] */
31
    protected $supportedVideoExtensions = [
32
        'mov',
33
        'mp4',
34
        'm4v',
35
        'avi',
36
        'mkv',
37
        'flv',
38
        'webm',
39
    ];
40
41 3
    public function __construct(MediaToolsServiceInterface $mediaTools)
42
    {
43 3
        $this->mediaTools = $mediaTools;
44 3
        parent::__construct();
45 3
    }
46
47
    /**
48
     * Configures the command.
49
     */
50 3
    protected function configure(): void
51
    {
52
        $this
53 3
            ->setName('convert:directory')
54 3
            ->setDescription('Convert, transcode all media files in a directory')
55 3
            ->setDefinition(
56 3
                new InputDefinition([
57 3
                    new InputOption('dir', 'd', InputOption::VALUE_REQUIRED),
58
                ])
59
            );
60 3
    }
61
62 3
    protected function execute(InputInterface $input, OutputInterface $output): int
63
    {
64 3
        if (!$input->hasOption('dir')) {
65
            throw new \InvalidArgumentException('Missing dir argument, use <command> <dir>');
66
        }
67 3
        $directory = $input->hasOption('dir') ? $input->getOption('dir') : null;
68 3
        if (!is_string($directory) || !is_dir($directory)) {
69 2
            throw new \InvalidArgumentException(sprintf(
70 2
                'Directory %s does not exists',
71 2
                is_string($directory) ? $directory : json_encode($directory)
72
            ));
73
        }
74
75 1
        $output->writeln(sprintf('* Scanning %s for media files...', $directory));
76
77
        // Get the videos in path
78 1
        $files = (new DirectoryScanner())->findFiles($directory, $this->supportedVideoExtensions);
79
80 1
        $output->writeln('* Reading metadata...');
81
82 1
        $progressBar = new ProgressBar($output, count($files));
83
        //$progressBar->start();
84
85 1
        $preset = new StreamableH264Preset($this->mediaTools);
86
87
        /** @var \SplFileInfo $file */
88 1
        foreach ($files as $file) {
89
            try {
90
                //$preset->convert($file->getPathname());
91 1
                $params = $preset->getParams($file->getPathname());
92 1
                var_dump($params->toArray());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($params->toArray()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
93
94 1
                $output->writeln(sprintf('<fg=green>- Converted:</> %s.', $file));
95
            } catch (ProcessException $e) {
96
                $output->writeln(sprintf('<fg=red>- Skipped:</> %s : Not a valid media file.', $file));
97
            }
98
99
            //$progressBar->advance();
100
        }
101
102 1
        $progressBar->finish();
103 1
        $output->writeln('');
104
105 1
        return 0;
106
    }
107
}
108