Created
October 20, 2025 22:49
-
-
Save NandoKstroNet/f02000046f11675b5da8397b3c38d867 to your computer and use it in GitHub Desktop.
GIST somente para registro - Processando videos com foco em Bitrates pra cada Dimensao Suportada a Partir de 480 a 1080
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Jobs; | |
| use App\Events\VideoEncodingFinished; | |
| use App\Events\VideoEncodingProgress; | |
| use App\Events\VideoEncodingStart; | |
| use App\Models\Video; | |
| use FFMpeg\FFProbe; | |
| use Illuminate\Bus\Batchable; | |
| use Illuminate\Contracts\Queue\ShouldQueue; | |
| use Illuminate\Foundation\Queue\Queueable; | |
| use Illuminate\Support\Facades\Storage; | |
| use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg; | |
| use FFMpeg\Format\Video\X264; | |
| use ProtoneMedia\LaravelFFMpeg\FFMpeg\VideoMedia; | |
| class VideoEncondingProcessJob implements ShouldQueue | |
| { | |
| use Queueable, Batchable; | |
| /** | |
| * Create a new job instance. | |
| */ | |
| public function __construct(private Video $video) {} | |
| /** | |
| * Execute the job. | |
| */ | |
| public function handle(): void | |
| { | |
| $videoNewName = str_replace(strrchr($this->video->video, '.'), '', $this->video->video); | |
| $lowBitrateFormat = (new X264)->setKiloBitrate(500); | |
| $midBitrateFormat = (new X264)->setKiloBitrate(1500); | |
| $highBitrateFormat = (new X264)->setKiloBitrate(3000); | |
| $ffmpeg = FFMpeg::fromDisk('videos') | |
| ->open($this->video->video); | |
| $heightVideo = (int) $ffmpeg->getVideoStream()->getDimensions()->getHeight(); | |
| $dimensions = $this->getDimensions($heightVideo); | |
| foreach ($dimensions as $dimension) { | |
| list($width, $height) = $dimension; | |
| event(new VideoEncodingStart($this->video, "{$width}x{$height}")); | |
| FFMpeg::fromDisk('videos') | |
| ->open($this->video->video) | |
| ->exportForHLS() | |
| ->addFormat($lowBitrateFormat, fn($media) => $media->scale($width, $height)) | |
| ->addFormat($midBitrateFormat, fn($media) => $media->scale($width, $height)) | |
| ->addFormat($highBitrateFormat, fn($media) => $media->scale($width, $height)) | |
| ->onProgress(function ($progress) { | |
| event(new VideoEncodingProgress($this->video, $progress)); | |
| }) | |
| ->toDisk('videos_processed') | |
| ->save($this->video->code . '/' . $videoNewName . '_' . $width . '_' . $height . '.m3u8'); | |
| } | |
| Storage::disk('videos')->delete($this->video->video); | |
| $this->video->update([ | |
| 'video' => $videoNewName, | |
| 'is_processed' => true | |
| ]); | |
| event(new VideoEncodingFinished($this->video)); | |
| } | |
| protected function getDimensions(int $heightVideo): array | |
| { | |
| $dimensions = [ | |
| 1080 => ['1920', '1080'], | |
| 720 =>['1280', '720'], | |
| 480 => ['848', '480'], | |
| ]; | |
| $normalizedHeight = match(true) { | |
| $heightVideo < 1080 && $heightVideo >= 720 => 720, | |
| $heightVideo < 720 => 480, | |
| default => 1080, | |
| }; | |
| $shortHeights = array_keys($dimensions); | |
| $index = array_search($normalizedHeight, $shortHeights); | |
| return array_slice($dimensions, $index); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment