Skip to content

Instantly share code, notes, and snippets.

@aten040791
Created September 10, 2019 08:10
Show Gist options
  • Select an option

  • Save aten040791/a5ecd2285c84f116e66c5f5bac34b6cd to your computer and use it in GitHub Desktop.

Select an option

Save aten040791/a5ecd2285c84f116e66c5f5bac34b6cd to your computer and use it in GitHub Desktop.
ConvertToMp4Job.php
<?php
namespace App\Jobs;
use FFMpeg\FFMpeg;
use FFMpeg\Format\Video\X264;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Storage;
class ConvertMediaToMp4Job implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $uploadedFileName;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($uploadedFileName)
{
$this->uploadedFileName = $uploadedFileName;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$ffmpeg = FFMpeg::create();
// In this example we have a path for the videos
$mediaDirectory = Storage::disk('media')->getAdapter()->getPathPrefix();
// Open the video providing the absolute path
$video = $ffmpeg->open($mediaDirectory.$this->uploadedFileName);
// Fix for error "Encoding failed : Can't save to X264"
// See: https://github.com/PHP-FFMpeg/PHP-FFMpeg/issues/310
$mp4Format = new X264();
$mp4Format->setAudioCodec("libmp3lame");
$uploadedFileNameWithoutExtension = pathinfo($this->uploadedFileName, PATHINFO_FILENAME);
// Save the video in the same directory with the new format
$video->save($mp4Format, $mediaDirectory. "{$uploadedFileNameWithoutExtension}.mp4");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment