This listener will convert your .mov
, .avi
and a lot of others video files to .mp4
after adding media with https://github.com/spatie/laravel-medialibrary
Feel free ask anything what you want.
- Laravel 5.*
- https://github.com/PHP-FFMpeg/PHP-FFMpeg
- Installed FFMpeg on your server
In EventServiceProvider
add your event listener for spatie/laravel-medialibrary
protected $listen = [
'Spatie\MediaLibrary\Events\MediaHasBeenAdded' => [
'App\Listeners\MediaVideoConverterListener'
],
Want use other audio codec? Rewrite it by yourself in the code or just add this in the medialibrary.php
config file with your codec.
'audio_codec' => 'libvo_aacenc',
Configure supervisord with following instructions:
[program:laravel_queue]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /home/vagrant/Code/laravel/artisan queue:work --timeout=0 --memory=512 --tries=3
autostart=true
autorestart=true
user=vagrant
numprocs=3
redirect_stderr=true
stdout_logfile=/home/vagrant/Code/laravel/storage/logs/default_queue.log
numprocs
<- How many queues will be work on your server. Depends on your memory, CPU, etc.
This is how many files will processing in one time. By default 3 video files will be in processing.
--memory=512
<- by default it's 128 Mb
This is just an example of how to proceed with the media conversion on upload. For better performance and handling more options, I suggest using a separate service layer in your application and have parameters set in the listener to your service. If you are using your solution to store the media data you can create your own events and listen to it. eg. in my current solutions I have separated servers with the same Redis queue. When a new video added I triggering the job "ConvertMedia" and listen to it on transcoding microservices and then converting it. I can pass parameters to the job if it is necessary.