Skip to content

Commit 5b4d3c7

Browse files
committed
Adding setting for running audio transcode synchrounously
1 parent 9d70fef commit 5b4d3c7

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/models/Settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class Settings extends Model
199199
'audioBitRate' => '128k',
200200
'audioSampleRate' => '44100',
201201
'audioChannels' => '2',
202+
'synchronous' => false
202203
];
203204
/**
204205
* Default options for encoded GIF

src/services/Transcode.php

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ public function getVideoThumbnailUrl($filePath, $thumbnailOptions, $generate = t
322322
*/
323323
public function getAudioUrl($filePath, $audioOptions): string
324324
{
325-
Craft::info('testing123', __METHOD__);
326325
$result = '';
327326
$settings = Transcoder::$plugin->getSettings();
328327
$subfolder = '';
@@ -388,39 +387,43 @@ public function getAudioUrl($filePath, $audioOptions): string
388387

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

410410
// If the audio file already exists and hasn't been modified, return it. Otherwise, start it transcoding
411411
if (file_exists($destAudioPath) && (@filemtime($destAudioPath) >= @filemtime($filePath))) {
412412
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
413413
$result = Craft::getAlias($url).$destAudioFile;
414414
} else {
415415
// Kick off the transcoding
416-
Craft::info($ffmpegCmd, __METHOD__);
417416
$this->executeShellCommand($ffmpegCmd);
418-
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
419-
$result = Craft::getAlias($url).$destAudioFile;
420-
Craft::info($result, __METHOD__);
421-
422-
// Create a lockfile in tmp
423-
// file_put_contents($lockFile, $pid);
417+
418+
if ($audioOptions['synchronous']) {
419+
Craft::info($ffmpegCmd, __METHOD__);
420+
$url = $settings['transcoderUrls']['audio'] . $subfolder ?? $settings['transcoderUrls']['default'];
421+
$result = Craft::getAlias($url).$destAudioFile;
422+
} else {
423+
Craft::info($ffmpegCmd."\nffmpeg PID: ".$pid, __METHOD__);
424+
// Create a lockfile in tmp
425+
file_put_contents($lockFile, $pid);
426+
}
424427
}
425428
}
426429

0 commit comments

Comments
 (0)