Skip to content

Commit

Permalink
Add ability to set input/output options for FFmpeg command
Browse files Browse the repository at this point in the history
  • Loading branch information
cwilby committed Mar 31, 2024
1 parent 1e1e554 commit 4b98884
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 26 deletions.
6 changes: 6 additions & 0 deletions lib/BackgroundJobs/BatchConvertMediaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class BatchConvertMediaJob extends QueuedJob {
private $outputExtension;
private $status;
private $additionalConversionFlags;
private $additionalInputConversionFlags;
private $additionalOutputConversionFlags;
private $sourceFolder;
private $postConversionSourceRule;
private $postConversionSourceRuleMoveFolder;
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 49 additions & 6 deletions lib/BackgroundJobs/ConvertMediaJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ConvertMediaJob extends QueuedJob {
private $postConversionOutputConflictRule;
private $postConversionOutputConflictRuleMoveFolder;
private $additionalConversionFlags;
private $additionalInputConversionFlags;
private $additionalOutputConversionFlags;
private $outputExtension;
private $sourceFile;
private $sourceFolder;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions lib/Operation/ConvertMediaOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
38 changes: 29 additions & 9 deletions src/components/ConvertMediaOperation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@
</div>
<PostConversionRules v-model="config" />
<div class="wmc-conversion-batch__FFmpeg">
<label>{{ t('workflow_media_converter', 'Additional FFmpeg flags') }}</label>
<div><input v-model="additionalConversionFlags" type="text"></div>
<label><strong>{{ t('workflow_media_converter', 'Additional FFmpeg flags') }}</strong></label>
<div class="grid">
<div class="column">
<label>{{ t('workflow_media_converter', 'Input flags') }}</label>
<input v-model="additionalInputConversionFlags" type="text">
</div>
<div class="column">
<label>{{ t('workflow_media_converter', 'Output flags') }}</label>
<input v-model="additionalOutputConversionFlags" type="text">
</div>
</div>
<input type="text" :value="commandString" style="background-color: #eee; color: #000" disabled>
</div>
<div><input type="text" :value="commandString" style="background-color: #eee"></div>
</div>
</template>

Expand All @@ -28,7 +37,8 @@ import PostConversionRules from './PostConversionRules.vue'
const defaultState = {
outputExtension: null,
additionalConversionFlags: '',
additionalInputConversionFlags: '',
additionalOutputConversionFlags: '',
postConversionSourceRule: 'keep',
postConversionSourceRuleMoveFolder: null,
postConversionOutputRule: 'keep',
Expand Down Expand Up @@ -82,21 +92,31 @@ 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 }
},
},
commandString() {
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(' ')
},
Expand Down
34 changes: 26 additions & 8 deletions src/components/CreateConversionBatch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@
</div>
</div>
<div class="wmc-conversion-batch__FFmpeg">
<label>{{ t('workflow_media_converter', 'Additional FFmpeg flags (optional, leave blank to use defaults)') }}</label>
<input v-model="additionalConversionFlags" type="text">
<input type="text" :value="commandString" style="background-color: #eee">
<label>{{ t('workflow_media_converter', 'Additional FFmpeg flags') }}</label>
<div class="grid">
<div class="column">
<label><small>{{ t('workflow_media_converter', 'Input flags') }}</small></label>
<input style="margin-bottom: 0.5em" v-model="additionalInputConversionFlags" type="text">
</div>
<div class="column">
<label><small>{{ t('workflow_media_converter', 'Output flags') }}</small></label>
<input style="margin-bottom: 0.5em" v-model="additionalOutputConversionFlags" type="text">
</div>
</div>
<input type="text" :value="commandString" style="background-color: #eee; color: #000" disabled>
</div>
<div class="wmc-conversion-batch__actions">
<button v-if="!conversionBatch.id" class="save" @click="$emit('save')">
Expand Down Expand Up @@ -149,20 +158,29 @@ export default {
this.commit(change)
},
},
additionalConversionFlags: {
additionalInputConversionFlags: {
get() {
return this.conversionBatch.additionalInputConversionFlags
},
set(additionalInputConversionFlags) {
this.commit({ additionalInputConversionFlags })
},
},
additionalOutputConversionFlags: {
get() {
return this.conversionBatch.additionalConversionFlags
return this.conversionBatch.additionalOutputConversionFlags
},
set(additionalConversionFlags) {
this.commit({ additionalConversionFlags })
set(additionalOutputConversionFlags) {
this.commit({ additionalOutputConversionFlags })
},
},
commandString() {
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(' ')
},
Expand Down
3 changes: 2 additions & 1 deletion src/views/PersonalSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export default {
sourceFolder: null,
sourceExtension: null,
outputExtension: null,
additionalConversionFlags: null,
additionalInputConversionFlags: null,
additionalOutputConversionFlags: null,
postConversionSourceRule: 'keep',
postConversionSourceRuleMoveFolder: null,
postConversionOutputRule: 'keep',
Expand Down

0 comments on commit 4b98884

Please sign in to comment.