Skip to content

Commit 1bccb7a

Browse files
authored
Merge pull request #22 from peirix/v1
Added trimming to audio transcoding
2 parents 647bc44 + 97f526a commit 1bccb7a

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/models/Settings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ class Settings extends Model
199199
'audioBitRate' => '128k',
200200
'audioSampleRate' => '44100',
201201
'audioChannels' => '2',
202+
'synchronous' => false,
203+
'stripMetadata' => false
202204
];
203205
/**
204206
* Default options for encoded GIF

src/services/Transcode.php

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ public function getAudioUrl($filePath, $audioOptions): string
366366
}
367367
$ffmpegCmd .= ' '.$thisEncoder['audioCodecOptions'];
368368

369+
if (!empty($audioOptions['timeInSecs'])) {
370+
$ffmpegCmd .= ' -t '.$audioOptions['timeInSecs'];
371+
}
369372

370373
// Create the directory if it isn't there already
371374
if (!is_dir($destAudioPath)) {
@@ -383,22 +386,29 @@ public function getAudioUrl($filePath, $audioOptions): string
383386

384387
// Assemble the destination path and final ffmpeg command
385388
$destAudioPath .= $destAudioFile;
389+
390+
if ($audioOptions['stripMetadata']) {
391+
$ffmpegCmd .= ' -map_metadata -1 ';
392+
}
393+
386394
$ffmpegCmd .= ' -f '
387395
.$thisEncoder['fileFormat']
388-
.' -y '.escapeshellarg($destAudioPath)
389-
.' 1> '.$progressFile.' 2>&1 & echo $!';
390-
391-
// Make sure there isn't a lockfile for this audio file already
392-
$lockFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destAudioFile.'.lock';
393-
$oldPid = @file_get_contents($lockFile);
394-
if ($oldPid !== false) {
395-
exec("ps $oldPid", $ProcessState);
396-
if (\count($ProcessState) >= 2) {
397-
return $result;
396+
.' -y '.escapeshellarg($destAudioPath);
397+
398+
if (!$audioOptions['synchronous']) {
399+
$ffmpegCmd .=' 1> '.$progressFile.' 2>&1 & echo $!';
400+
// Make sure there isn't a lockfile for this audio file already
401+
$lockFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$destAudioFile.'.lock';
402+
$oldPid = @file_get_contents($lockFile);
403+
if ($oldPid !== false) {
404+
exec("ps $oldPid", $ProcessState);
405+
if (\count($ProcessState) >= 2) {
406+
return $result;
407+
}
408+
// It's finished transcoding, so delete the lockfile and progress file
409+
@unlink($lockFile);
410+
@unlink($progressFile);
398411
}
399-
// It's finished transcoding, so delete the lockfile and progress file
400-
@unlink($lockFile);
401-
@unlink($progressFile);
402412
}
403413

404414
// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
@@ -407,11 +417,17 @@ public function getAudioUrl($filePath, $audioOptions): string
407417
$result = Craft::getAlias($url).$destAudioFile;
408418
} else {
409419
// Kick off the transcoding
410-
$pid = $this->executeShellCommand($ffmpegCmd);
411-
Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__);
412-
413-
// Create a lockfile in tmp
414-
file_put_contents($lockFile, $pid);
420+
$this->executeShellCommand($ffmpegCmd);
421+
422+
if ($audioOptions['synchronous']) {
423+
Craft::info($ffmpegCmd, __METHOD__);
424+
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
425+
$result = Craft::getAlias($url).$destAudioFile;
426+
} else {
427+
Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__);
428+
// Create a lockfile in tmp
429+
file_put_contents($lockFile, $pid);
430+
}
415431
}
416432
}
417433

0 commit comments

Comments
 (0)