Skip to content
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

Closed
pedrofurtado opened this issue Oct 19, 2018 · 5 comments
Closed

How to build my own Conversion? #1277

pedrofurtado opened this issue Oct 19, 2018 · 5 comments

Comments

@pedrofurtado
Copy link

pedrofurtado commented Oct 19, 2018

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! 👍

@nicolasbeauvais
Copy link
Contributor

nicolasbeauvais commented Oct 20, 2018

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

@pedrofurtado
Copy link
Author

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: Intervention/Image/Exception/NotReadableException with message 'Unsupported image type. GD driver is only able to decode JPG, PNG, GIF or WebP files.'

Can I return, from my custom generator, a path to a file that is not a image (.m3u8, .ts, for example)?

@nicolasbeauvais
Copy link
Contributor

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.

@pedrofurtado
Copy link
Author

@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).
But I also upload some binary files, like audios (mp3, wav, for example), videos and documents (excel, word, ...).
The flexibility proposed in this PR is great for this! 👍 🤝

@freekmurze
Copy link
Member

We'll continue the conversion in #1279

freekmurze pushed a commit that referenced this issue Nov 19, 2018
* Fix for issue #1277

* Fix CS

* Refactor fix for issue #1277

* Add missing return types

* Add withoutManipulations() helper method for Conversion.php

* Add test for the withoutManipulations method

* Add test for performManipulations early return

* Fix style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants