-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to build my own Conversion? #1277
Comments
Hello @pedrofurtado, the process to create your own media generator is described in the documentation: https://docs.spatie.be/laravel-medialibrary/v7/converting-other-file-types/creating-a-custom-image-generator You can take a look at the base Video generator that takes a video file in input to create a preview image in output using ffmpeg: https://github.com/spatie/laravel-medialibrary/blob/master/src/ImageGenerators/FileTypes/Video.php |
I've created the generator with code below: <?php
namespace App\Spatie\MediaLibrary\Generators;
use Illuminate\Support\Collection;
use Spatie\MediaLibrary\Conversion\Conversion;
use Spatie\MediaLibrary\ImageGenerators\BaseGenerator;
class Wav extends BaseGenerator
{
public function convert(string $file, Conversion $conversion = null) : string
{
$pathToWavFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.wav';
$pathToM3u8File = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.m3u8';
$pathToTsFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'-%05d.ts';
exec("ffmpeg -i {$pathToWavFile} -vn -ac 2 -acodec aac -f segment -segment_format mpegts -segment_time 100 -segment_list {$pathToM3u8File} {$pathToTsFile}");
return $pathToM3u8File;
}
public function requirementsAreInstalled() : bool
{
return true;
}
public function supportedExtensions() : Collection
{
return collect(['wav', 'wave']);
}
public function supportedMimeTypes() : Collection
{
return collect([
'audio/wav',
'audio/wave',
'audio/x-wav',
'audio/x-pn-wav'
]);
}
} with config file like this: <?php
return [
# ...
'image_generators' => [
# ...
App\Spatie\MediaLibrary\Generators\Wav::class
],
]; and model like this: <?php
class Audio extends Model implements HasMedia
{
public function registerMediaConversions(Media $media = null)
{
$this->addMediaConversion('transcode')
->nonQueued()
->performOnCollections('file');
}
public function registerMediaCollections()
{
$this->addMediaCollection('file')->singleFile();
}
} But when I execute the snippet (for example): <?php
App\MyModel::first()->addMedia('path/to/file.wav')->toMediaCollection('file') Appears the following error: Can I return, from my custom generator, a path to a file that is not a image (.m3u8, .ts, for example)? |
Hello @pedrofurtado, the media generator feature was designed to convert any type of file to an image, like PDF or video previews. It should be possible to use it to convert files to other formats just like your use case, I opened a PR to explore a solution to this issue #1279. |
@nicolasbeauvais That's a great news! Enabling the devs to create conversions to any type of file (respecting, of course, the default to behavior to the images) will be awesome for the package! In my case, for example, I already use all the conversions and manipulations already coded in media-library for images (jpg, png, and so on). |
We'll continue the conversion in #1279 |
I want to build my own conversion, that converts ".wav" files in HLS. How are the steps to build and register my conversion that integrates with the package (the structure of files and the main classes involved)?
Anyway, thanks for the package! It is awesome! 👍
The text was updated successfully, but these errors were encountered: