laravel-medialibrary icon indicating copy to clipboard operation
laravel-medialibrary copied to clipboard

How to create a conversion to transform videos to WebM?

Open AlejandroAkbal opened this issue 2 years ago • 1 comments

Discussed in https://github.com/spatie/laravel-medialibrary/discussions/2917

Originally posted by AlejandroAkbal May 16, 2022 Hi, I'm trying to optimize all videos that are added to my Post Model. I try to accomplish this goal by converting videos to WebM format.

This is my Post Model:


class Post extends Model implements HasMedia
{
    use InteractsWithMedia;

    public bool $registerMediaConversionsUsingModelInstance = true;

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('default')
            ->singleFile()
            ->acceptsFile(function (File $file): bool {
                return
                    Str::startsWith($file->mimeType, 'image/')
                    ||
                    Str::startsWith($file->mimeType, 'video/');
            });
    }

    /**
     * @throws InvalidManipulation
     */
    public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
    {
        if (!$media) {
            return;
        }

        switch ($media->type === 'video') {

            case true:
                $this->addMediaConversion('default')
                    ->extractVideoFrameAtSecond(1)
                    ->quality(90)
                    ->withResponsiveImages();
                break;

            case false:
                $this->addMediaConversion('default')
                    ->quality(90)
                    ->format('webp')
                    ->withResponsiveImages();
                break;
        }
    }
}

I currently transform the videos by listening to the MediaHasBeenAdded event

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        MediaHasBeenAdded::class => [
            ProcessVideoFromMedia::class,
        ],
}

And then running some chained jobs. In that ProcessVideoFromMedia class I replace the current Post Media with a WebM version by adding it again to the Media collection E.g.

        $media->model
            ->addMedia($webmMediaPath)
            ->toMediaCollection();

While this works, I'm creating two Models in the database and wasting some CDN data and queries by uploading the content twice.

What I would like to do is create a Media Manipulation/Conversion that transforms any video to WebM.

Ideally like this:

public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
    {
        $this->addMediaConversion('default')
                    ->format('webm') // Important bit
                    ->optimize();
    }
    

I've seen some solutions like https://github.com/spatie/laravel-medialibrary/pull/1279 and https://github.com/spatie/laravel-medialibrary/issues/1277, but I cannot get them to work.

Can someone help me?

AlejandroAkbal avatar May 20 '22 14:05 AlejandroAkbal

Bump

AlejandroAkbal avatar May 30 '22 21:05 AlejandroAkbal

I would love to have an example for this as as well

modrink avatar Sep 30 '22 08:09 modrink

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

spatie-bot avatar Jan 31 '23 11:01 spatie-bot