From efb7991e5728f2613f7dbdfb4dc35caa06776892 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Wed, 30 Sep 2020 13:49:48 +0200 Subject: [PATCH] Check if result file exists, and provide force option --- cutvideo | 1 + src/Command.php | 8 ++++++++ src/Cutting.php | 4 ++-- tests/Unit/CuttingTest.php | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cutvideo b/cutvideo index ee10eeb..a3d4ed9 100755 --- a/cutvideo +++ b/cutvideo @@ -16,6 +16,7 @@ use Symfony\Component\Console\SingleCommandApplication; ->addArgument('file', InputArgument::REQUIRED, 'The video file to cut.') ->addArgument('start', InputArgument::REQUIRED, 'The starting point of the video') ->addArgument('end', InputArgument::REQUIRED, 'The end point of the video') + ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force generation, even if target already exists.') ->addOption('ad', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Start and end of ads, separate start and end via "/".', []) ->setCode(new Command()) ->run(); diff --git a/src/Command.php b/src/Command.php index a494ee8..d4818d8 100644 --- a/src/Command.php +++ b/src/Command.php @@ -44,6 +44,14 @@ class Command $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); $this->io->title('Generating temp cutted video files.'); diff --git a/src/Cutting.php b/src/Cutting.php index ba70263..3988c09 100644 --- a/src/Cutting.php +++ b/src/Cutting.php @@ -65,7 +65,7 @@ class Cutting public function getCommandForGeneratingMetadata(): Process { $command = sprintf( - 'ffmpeg -i %s -f ffmetadata %s', + 'ffmpeg -y -i %s -f ffmetadata %s', escapeshellarg($this->video->getOriginalFilename()), escapeshellarg($this->getMetadataFilename()) ); @@ -94,7 +94,7 @@ class Cutting public function getCommandForGeneratingVideo(): Process { $command = sprintf( - 'ffmpeg -f concat -safe 0 -i %s -c copy' + 'ffmpeg -y -f concat -safe 0 -i %s -c copy' . ' -f ffmetadata -i %s -c copy -map_metadata 1' // . ' -metadata Title="$episodeTitle"' . ' %s', diff --git a/tests/Unit/CuttingTest.php b/tests/Unit/CuttingTest.php index 8a85605..a36b0f7 100644 --- a/tests/Unit/CuttingTest.php +++ b/tests/Unit/CuttingTest.php @@ -132,7 +132,7 @@ class CuttingTest extends TestCase $command = $subject->getCommandForGeneratingMetadata(); static::assertSame( - "ffmpeg -i 'Some-Video.mp4' -f ffmetadata '/tmp/Some-Video-metadata.txt'", + "ffmpeg -y -i 'Some-Video.mp4' -f ffmetadata '/tmp/Some-Video-metadata.txt'", $command->getCommandLine() ); } @@ -236,7 +236,7 @@ class CuttingTest extends TestCase $command = $subject->getCommandForGeneratingVideo(); static::assertSame( - 'ffmpeg -f concat -safe 0 -i \'/tmp/Series-Name_Series-01_episode-title-01-concat.txt\' -c copy' + 'ffmpeg -y -f concat -safe 0 -i \'/tmp/Series-Name_Series-01_episode-title-01-concat.txt\' -c copy' . ' -f ffmetadata -i \'/tmp/Series-Name_Series-01_episode-title-01-metadata.txt\' -c copy -map_metadata 1' // . ' -metadata Title="$episodeTitle"' . ' \'Series-Name/Series-01/10-episode title.mp4\'',