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