From 4b98884a6667691393211f2f71aabadd3d7d44b2 Mon Sep 17 00:00:00 2001 From: Cameron Wilby Date: Sun, 31 Mar 2024 14:57:04 -0700 Subject: [PATCH] Add ability to set input/output options for FFmpeg command --- lib/BackgroundJobs/BatchConvertMediaJob.php | 6 +++ lib/BackgroundJobs/ConvertMediaJob.php | 55 ++++++++++++++++++--- lib/Operation/ConvertMediaOperation.php | 6 ++- src/components/ConvertMediaOperation.vue | 38 ++++++++++---- src/components/CreateConversionBatch.vue | 34 ++++++++++--- src/views/PersonalSettings.vue | 3 +- 6 files changed, 116 insertions(+), 26 deletions(-) diff --git a/lib/BackgroundJobs/BatchConvertMediaJob.php b/lib/BackgroundJobs/BatchConvertMediaJob.php index 447178d..6bd57f6 100644 --- a/lib/BackgroundJobs/BatchConvertMediaJob.php +++ b/lib/BackgroundJobs/BatchConvertMediaJob.php @@ -25,6 +25,8 @@ class BatchConvertMediaJob extends QueuedJob { private $outputExtension; private $status; private $additionalConversionFlags; + private $additionalInputConversionFlags; + private $additionalOutputConversionFlags; private $sourceFolder; private $postConversionSourceRule; private $postConversionSourceRuleMoveFolder; @@ -80,6 +82,8 @@ public function parseArguments($arguments) { $this->status = $arguments['status']; $this->userFolder = "/{$this->userId}/files"; $this->additionalConversionFlags = $arguments['additionalConversionFlags']; + $this->additionalInputConversionFlags = $arguments['additionalInputConversionFlags']; + $this->additionalOutputConversionFlags = $arguments['additionalOutputConversionFlags']; $this->sourceFolderPath = $this->prependUserFolder($arguments['sourceFolder']); $this->convertMediaInSubFolders = $arguments['convertMediaInSubFolders']; $this->sourceExtension = strtolower($arguments['sourceExtension']); @@ -137,6 +141,8 @@ public function queueUnconvertedMediaForConversion() { 'path' => $node->getPath(), 'outputExtension' => $this->outputExtension, 'additionalConversionFlags' => $this->additionalConversionFlags, + 'additionalInputConversionFlags' => $this->additionalInputConversionFlags, + 'additionalOutputConversionFlags' => $this->additionalOutputConversionFlags, 'postConversionSourceRule' => $this->postConversionSourceRule, 'postConversionSourceRuleMoveFolder' => $this->postConversionSourceRuleMoveFolder, 'postConversionOutputRule' => $this->postConversionOutputRule, diff --git a/lib/BackgroundJobs/ConvertMediaJob.php b/lib/BackgroundJobs/ConvertMediaJob.php index c8f160b..0e9dc93 100644 --- a/lib/BackgroundJobs/ConvertMediaJob.php +++ b/lib/BackgroundJobs/ConvertMediaJob.php @@ -33,6 +33,8 @@ class ConvertMediaJob extends QueuedJob { private $postConversionOutputConflictRule; private $postConversionOutputConflictRuleMoveFolder; private $additionalConversionFlags; + private $additionalInputConversionFlags; + private $additionalOutputConversionFlags; private $outputExtension; private $sourceFile; private $sourceFolder; @@ -101,6 +103,8 @@ public function parseArguments($arguments) { $this->outputExtension = (string)$arguments['outputExtension']; $this->convertMediaInParallel = isset($adminSettings) && isset($adminSettings['convertMediaInParallel']) ? (bool)$adminSettings['convertMediaInParallel'] : false; $this->additionalConversionFlags = (string)$arguments['additionalConversionFlags']; + $this->additionalInputConversionFlags = (string)$arguments['additionalInputConversionFlags']; + $this->additionalOutputConversionFlags = (string)$arguments['additionalOutputConversionFlags']; $this->sourceFile = $this->rootFolder->get($this->path); $this->sourceFolder = dirname($this->path); @@ -134,6 +138,8 @@ public function lockConversion() { 'batch_id' => $this->batchId, 'path' => $this->path, 'additionalConversionFlags' => $this->additionalConversionFlags, + 'additionalInputConversionFlags' => $this->additionalInputConversionFlags, + 'additionalOutputConversionFlags' => $this->additionalOutputConversionFlags, 'outputExtension' => $this->outputExtension, 'convertMediaInParallel' => $this->convertMediaInParallel, 'postConversionSourceRule' => $this->postConversionSourceRule, @@ -155,21 +161,58 @@ public function lockConversion() { public function convertMedia() { $threads = $this->configService->getAppConfigValue('threadLimit', 0); - $additionalConversionFlags = empty($this->additionalConversionFlags) ? '' : " {$this->additionalConversionFlags}"; - - $command = "ffmpeg -threads $threads {$additionalConversionFlags} -i {$this->tempSourcePath} {$this->tempOutputPath}"; - - $process = $this->processFactory->create($command); + $process = $this->processFactory->create( + command: $this->getConversionCommand(flagsBeforeInput: false) + ); $process->run(); if (!$process->isSuccessful()) { - throw new ProcessFailedException($process); + $process = $this->processFactory->create( + command: $this->getConversionCommand(flagsBeforeInput: true) + ); + + $process->run(); + + if (!$process->isSuccessful()) { + throw new ProcessFailedException($process); + } } return $this; } + private function getConversionCommand($flagsBeforeInput = false) + { + $additionalConversionFlags = empty($this->additionalConversionFlags) ? '' : " {$this->additionalConversionFlags}"; + $additionalInputConversionFlags = empty($this->additionalInputConversionFlags) ? '' : " {$this->additionalInputConversionFlags}"; + $additionalOutputConversionFlags = empty($this->additionalOutputConversionFlags) ? '' : " {$this->additionalOutputConversionFlags}"; + + $command = "ffmpeg -threads $threads"; + + if (!empty($additionalConversionFlags)) { + if ($flagsBeforeInput) { + $command .= " {$additionalConversionFlags} -i {$this->tempSourcePath}"; + } else { + $command .= " -i {$this->tempSourcePath} {$additionalConversionFlags}"; + } + } else { + if (!empty($additionalInputConversionFlags)) { + $command .= " {$additionalInputConversionFlags}"; + } + + $command .= " -i {$this->tempSourcePath}" + + if (!empty($additionalOutputConversionFlags)) { + $command .= " {$additionalOutputConversionFlags}"; + } + } + + $command .= " {$this->tempOutputPath}"; + + return $command; + } + public function handlePostConversion() { $this->writePostConversionOutputFile(); $this->handlePostConversionSourceFile(); diff --git a/lib/Operation/ConvertMediaOperation.php b/lib/Operation/ConvertMediaOperation.php index ae2b839..73fa85e 100644 --- a/lib/Operation/ConvertMediaOperation.php +++ b/lib/Operation/ConvertMediaOperation.php @@ -96,7 +96,8 @@ private function handleEvent(string $eventName, Event $event, IRuleMatcher $rule $postConversionOutputRuleMoveFolder = $config['postConversionOutputRuleMoveFolder']; $postConversionOutputConflictRule = $config['postConversionOutputConflictRule']; $postConversionOutputConflictRuleMoveFolder = $config['postConversionOutputConflictRuleMoveFolder']; - $additionalConversionFlags = $config['additionalConversionFlags']; + $additionalInputConversionFlags = $config['additionalInputConversionFlags']; + $additionalOutputConversionFlags = $config['additionalOutputConversionFlags']; if ($originalFileMode === 'keep' && $targetFileMode === 'preserve') { break; @@ -109,7 +110,8 @@ private function handleEvent(string $eventName, Event $event, IRuleMatcher $rule $this->jobList->add(ConvertMediaJob::class, [ 'path' => $path, 'outputExtension' => $outputExtension, - 'additionalConversionFlags' => $additionalConversionFlags, + 'additionalInputConversionFlags' => $additionalInputConversionFlags, + 'additionalOutputConversionFlags' => $additionalOutputConversionFlags, 'postConversionSourceRule' => $postConversionSourceRule, 'postConversionSourceRuleMoveFolder' => $postConversionSourceRuleMoveFolder, 'postConversionOutputRule' => $postConversionOutputRule, diff --git a/src/components/ConvertMediaOperation.vue b/src/components/ConvertMediaOperation.vue index 2cd45db..57c80b0 100644 --- a/src/components/ConvertMediaOperation.vue +++ b/src/components/ConvertMediaOperation.vue @@ -12,10 +12,19 @@
- -
+ +
+
+ + +
+
+ + +
+
+
-
@@ -28,7 +37,8 @@ import PostConversionRules from './PostConversionRules.vue' const defaultState = { outputExtension: null, - additionalConversionFlags: '', + additionalInputConversionFlags: '', + additionalOutputConversionFlags: '', postConversionSourceRule: 'keep', postConversionSourceRuleMoveFolder: null, postConversionOutputRule: 'keep', @@ -82,12 +92,21 @@ export default { }, }, - additionalConversionFlags: { + additionalInputConversionFlags: { get() { - return this.config.additionalConversionFlags + return this.config.additionalInputConversionFlags }, - set(additionalConversionFlags) { - this.config = { additionalConversionFlags } + set(additionalInputConversionFlags) { + this.config = { additionalInputConversionFlags } + }, + }, + + additionalOutputConversionFlags: { + get() { + return this.config.additionalOutputConversionFlags + }, + set(additionalOutputConversionFlags) { + this.config = { additionalOutputConversionFlags } }, }, @@ -95,8 +114,9 @@ export default { return [ 'ffmpeg', parseInt(this.threads) !== 0 ? `-threads ${this.threads}` : '', - this.additionalConversionFlags ? `${this.additionalConversionFlags}` : '', + this.additionalInputConversionFlags ? `${this.additionalInputConversionFlags}` : '', '-i {input}', + this.additionalOutputConversionFlags ? `${this.additionalOutputConversionFlags}` : '', '{output}', ].filter(Boolean).join(' ') }, diff --git a/src/components/CreateConversionBatch.vue b/src/components/CreateConversionBatch.vue index 2f0ce0f..cde6dbc 100644 --- a/src/components/CreateConversionBatch.vue +++ b/src/components/CreateConversionBatch.vue @@ -63,9 +63,18 @@
- - - + +
+
+ + +
+
+ + +
+
+