|
1
|
|
|
<?php |
|
2
|
|
|
namespace Tartana\Console\Command; |
|
3
|
|
|
use League\Flysystem\Adapter\Local; |
|
4
|
|
|
use Monolog\Logger; |
|
5
|
|
|
use Symfony\Component\Console\Command\Command as SymfonyCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
10
|
|
|
use Tartana\Component\Command\Command; |
|
11
|
|
|
use Tartana\Component\Command\Runner; |
|
12
|
|
|
use Tartana\Event\ProcessingCompletedEvent; |
|
13
|
|
|
use Tartana\Mixins\LoggerAwareTrait; |
|
14
|
|
|
use Tartana\Util; |
|
15
|
|
|
|
|
16
|
|
|
class ConvertSoundCommand extends SymfonyCommand |
|
17
|
|
|
{ |
|
18
|
|
|
use LoggerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
protected $dispatcher = null; |
|
21
|
|
|
|
|
22
|
|
|
protected $commandRunner = null; |
|
23
|
|
|
|
|
24
|
8 |
|
public function __construct (Runner $commandRunner, EventDispatcherInterface $dispatcher = null) |
|
25
|
|
|
{ |
|
26
|
8 |
|
parent::__construct('convert:sound'); |
|
27
|
|
|
|
|
28
|
8 |
|
$this->dispatcher = $dispatcher; |
|
29
|
8 |
|
$this->commandRunner = $commandRunner; |
|
30
|
8 |
|
} |
|
31
|
|
|
|
|
32
|
8 |
|
protected function configure () |
|
33
|
|
|
{ |
|
34
|
8 |
|
$this->setDescription('Converts mp4 files to mp3. This command is running in foreground!'); |
|
35
|
|
|
|
|
36
|
8 |
|
$this->addArgument('source', InputArgument::REQUIRED, 'The folder with files to convert.'); |
|
37
|
8 |
|
$this->addArgument('destination', InputArgument::REQUIRED, 'The folder to convert the files to.'); |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
6 |
|
protected function execute (InputInterface $input, OutputInterface $output) |
|
41
|
|
|
{ |
|
42
|
6 |
|
$source = Util::realPath($input->getArgument('source')); |
|
43
|
6 |
|
if (empty($source)) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->log('Source directory no found to convert.', Logger::ERROR); |
|
46
|
1 |
|
return; |
|
47
|
|
|
} |
|
48
|
5 |
|
$destination = Util::realPath($input->getArgument('destination')); |
|
49
|
5 |
|
if (empty($destination)) |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->log('Destination directory no found to convert.', Logger::ERROR); |
|
52
|
1 |
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
4 |
|
if (! $this->commandRunner->execute(new Command('which ffmpeg'))) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->log("FFmpeg is not on the path, can't convert video files to mp3", Logger::INFO); |
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
$destination = new Local($destination); |
|
62
|
3 |
|
$source = new Local($source); |
|
63
|
|
|
|
|
64
|
3 |
|
foreach ($source->listContents('', true) as $file) |
|
65
|
|
|
{ |
|
66
|
3 |
|
if (! Util::endsWith($file['path'], '.mp4')) |
|
67
|
|
|
{ |
|
68
|
2 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
$command = new Command('ffmpeg'); |
|
72
|
3 |
|
$command->setAsync(false); |
|
73
|
3 |
|
$command->addArgument('-i'); |
|
74
|
3 |
|
$command->addArgument($source->applyPathPrefix($file['path'])); |
|
75
|
3 |
|
$command->addArgument('-f mp3', false); |
|
76
|
3 |
|
$command->addArgument('-ab 192k', false); |
|
77
|
3 |
|
$command->addArgument('-y', false); |
|
78
|
3 |
|
$command->addArgument('-vn'); |
|
79
|
3 |
|
$command->addArgument(str_replace('.mp4', '.mp3', $destination->applyPathPrefix($file['path']))); |
|
80
|
|
|
|
|
81
|
3 |
|
$this->log('Running ffmpeg system command: ' . $command); |
|
82
|
|
|
|
|
83
|
3 |
|
$this->commandRunner->execute($command, function ($line) use ( $output) { |
|
84
|
2 |
|
$output->writeln($line); |
|
85
|
3 |
|
}); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
if ($this->dispatcher) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->dispatcher->dispatch('processing.completed', new ProcessingCompletedEvent($source, $destination, true)); |
|
91
|
|
|
} |
|
92
|
3 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|