videocutting/src/Command.php
Daniel Siepmann 8124631d5d
Allow to define temp folder
Some systems might have limited temp folder sizes.
A new option is added to configure the folder to use for temp files.
2023-02-12 11:31:31 +01:00

142 lines
4 KiB
PHP

<?php
namespace DanielSiepmann\Videcutting;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class Command
{
/**
* @var SymfonyStyle
*/
private $io;
public function __invoke(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
$videoInfo = new VideoInfo(
$input->getArgument('file'),
$input->getArgument('start'),
$input->getArgument('end'),
$input->getOption('ad')
);
if (
file_exists($videoInfo->getTargetFilePath())
&& $input->getOption('force') === false
) {
$this->io->error('Target file already exist, "-f" to force generation.');
return 1;
}
$cutting = new Cutting($videoInfo, $input->getOption('tmp'));
$this->executeStep('Generating temp cutted video files.');
foreach ($cutting->getCommandsForCutting() as $command) {
$this->runCommand($command);
}
$this->executeStep(
'Generating Metadata',
$cutting->getCommandForGeneratingMetadata()
);
$this->executeStep(
'Generating Concat input file',
$cutting->getCommandForGeneratingConcatInputFile()
);
$this->executeStep('Creating target folder');
$this->createTargetFolder($videoInfo);
$this->executeStep(
'Generating final video',
$cutting->getCommandForGeneratingVideo()
);
$this->executeStep(
'Cleanup',
$cutting->getCommandForCleanup()
);
$this->io->success(sprintf(
'Generated video file "%s".',
$videoInfo->getTargetFilePath()
));
if ($input->getOption('remove')) {
$this->removeInputFile($videoInfo);
}
return 0;
}
private function executeStep(string $name, Process $command = null): void
{
if ($this->io->isVerbose()) {
$this->io->writeln($name);
}
if ($command instanceof Process) {
$this->runCommand($command);
}
}
private function runCommand(Process $command): void
{
if ($this->io->isVeryVerbose()) {
$this->io->writeln('Executing command');
$this->io->writeln($command->getCommandLine());
$command->run(function ($type, $buffer) {
$this->io->writeln($buffer);
});
} else {
$command->run();
}
if (!$command->isSuccessful()) {
throw new ProcessFailedException($command);
}
}
private function createTargetFolder(VideoInfo $videoInfo): void
{
$target = dirname($videoInfo->getTargetFilePath());
if (file_exists($target)) {
return;
}
mkdir($target, 0775, true);
}
private function removeInputFile(VideoInfo $videoInfo)
{
$this->executeStep('Remove input file');
unlink($videoInfo->getOriginalFilename());
}
}