Skip to content

Instantly share code, notes, and snippets.

@Curdal
Created March 10, 2020 11:02
Show Gist options
  • Save Curdal/23fdd903320d634b94c6900a2b15ef3f to your computer and use it in GitHub Desktop.
Save Curdal/23fdd903320d634b94c6900a2b15ef3f to your computer and use it in GitHub Desktop.
Laravel 5+ Listener to convert audio files
<?php
namespace App\Listeners;
use Log, Storage;
use GuzzleHttp\Client as HttpClient;
use CloudConvert\Api as CloudConvert;
use App\Events\ConvertAudio;
use App\Enums\ConversionStatus;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class CloudConvertAudio implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ConvertAudio $event
* @return void
*/
public function handle(ConvertAudio $event)
{
if ( ! $this->isSupportedAudioFormat($event->format)) {
throw new \Exception('Unsupported audio format.');
} else if (mb_strtolower($event->media->extension) === mb_strtolower($event->format)) {
Log::info('Conversion not required :: ' . $event->media->id);
return;
}
$file_name = explode('.', $event->media->saved_as);
$payload = [
// Input Params
'input' => 'upload',
'file' => Storage::readStream("{$event->media->location}/{$event->media->saved_as}"),
// Conversion Params
'outputformat' => $event->format,
// Output Params
'wait' => true,
];
try {
$cc = new CloudConvert(config('services.cloudconvert.api_key'));
$cc_process = $cc->createProcess([
'inputformat' => last($file_name),
'outputformat' => $event->format,
]);
$result = $cc_process->start($payload);
// Update conversion status
$event->media->update(['conversion_status' => ConversionStatus::SUBMITTED]);
if (isset($result->output->url) && isset($result->output->filename)) {
$this->downloadFile(
$result->output->url,
$result->output->filename,
$event->media
);
}
} catch (\Exception $e) {
Log::error('Unable to convert file with CloudConvert. ' . $e->getMessage());
}
}
/**
* Download new file to disk.
*
* @param string $url
* @param string $filename
* @param string $location
* @return void
*/
private function downloadFile($url, $filename, $media)
{
try {
$http = new HttpClient;
$response = $http->get($url);
Storage::put("{$media->location}/{$filename}", $response->getBody()->getContents());
$media->update([
'saved_as' => $filename,
'conversion_status' => ConversionStatus::COMPLETED
]);
} catch (\Exception $e) {
Log::error('Unable to download file from CloudConvert. ' . $e->getMessage());
}
}
/**
* Check if the conversion type is supported
*
* @param string $format
* @return boolean
*/
private function isSupportedAudioFormat($format): bool
{
if ( ! in_array($format, [
'acc', 'ac3', 'aif', 'aifc', 'aiff', 'amr', 'au', 'caf', 'flac', 'm4a',
'm4b', 'mp3', 'oga', 'ogg', 'sf2', 'sfark', 'voc', 'wav', 'weba', 'wma',
])) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment