I don’t use any package. I just call it directly using exec
. This was created prior to the release of Laravel’s Process
facade and before I knew about the File
facade (a simple wrapper around PHP’s file functions that can be used outside of your Storage disks).
<?php
namespace App;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class FfmpegHelper
{
public static function normalizeVolume(string $file, bool $overwrite = true): void
{
$timestamp = time();
$pathInfo = pathinfo($file);
$newFile = "{$pathInfo['dirname']}/{$pathInfo['filename']}.{$timestamp}.mp3";
$results = [];
exec("ffmpeg -i $file -filter:a loudnorm=I=-16:dual_mono=true:TP=-1.5 -b:a 48K -ar 24000 $newFile 2>&1", $results);
self::validateSuccess($results);
if ($overwrite) {
unlink($file);
rename($newFile, $file);
}
}
public static function concatAudio(Collection $filesToCombine, string $newFile): void
{
$allFiles = $filesToCombine->join('|');
$results = [];
exec("ffmpeg -i 'concat:{$allFiles}' -acodec copy {$newFile} 2>&1", $results);
self::validateSuccess($results);
}
protected static function validateSuccess(array $results)
{
if (! Str::contains(strtolower(end($results)), 'audio:')) {
throw new \Exception('ffmpeg exception: ' . implode('|', $results));
}
}
}