Skip to content

Commit 251ba85

Browse files
committed
AULA 4: Encoding de Video
1 parent b68dff6 commit 251ba85

10 files changed

Lines changed: 818 additions & 5 deletions

File tree

app/Jobs/VideoEncodingJob.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Video;
6+
use FFMpeg\Format\Video\X264;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Queue\Queueable;
9+
use Illuminate\Support\Facades\Storage;
10+
use ProtoneMedia\LaravelFFMpeg\Support\FFMpeg;
11+
12+
class VideoEncodingJob implements ShouldQueue
13+
{
14+
use Queueable;
15+
16+
/**
17+
* Create a new job instance.
18+
*/
19+
public function __construct(private Video $video)
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Execute the job.
26+
*/
27+
public function handle(): void
28+
{
29+
$newVideoName = str_replace(strrchr($this->video->video, '.'), '', $this->video->video) . '.m3u8';
30+
31+
$low = (new X264())->setKiloBitrate(500);
32+
$mid = (new X264())->setKiloBitrate(1500);
33+
$high = (new X264())->setKiloBitrate(3000);
34+
35+
FFMpeg::fromDisk('videos')
36+
->open($this->video->video)
37+
->exportForHLS()
38+
->addFormat($low)
39+
->addFormat($mid)
40+
->addFormat($high)
41+
->onProgress(function($progress) use($newVideoName){
42+
//Disparar o evento com o progresso do encoding do video
43+
})
44+
->toDisk('videos_encoded')
45+
->save($this->video->code . '/' . $newVideoName);
46+
47+
Storage::disk('videos')->delete($this->video->video);
48+
49+
$this->video->update([
50+
'video' => $newVideoName,
51+
'is_processed' => true
52+
]);
53+
}
54+
}

app/Livewire/Media/VideoUpload.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Livewire\Media;
44

5+
use App\Jobs\VideoEncodingJob;
6+
use App\Models\Content;
57
use Livewire\{WithFileUploads, Component};
68

79
class VideoUpload extends Component
@@ -10,9 +12,29 @@ class VideoUpload extends Component
1012

1113
public $videos;
1214

15+
public Content $content;
16+
1317
public function storeVideos()
1418
{
15-
dd($this->videos);
19+
$this->validate();
20+
21+
foreach($this->videos as $video) {
22+
23+
$video = $this->content->videos()->create([
24+
'name' => $video->getClientOriginalName(),
25+
'video' => $video->store('', 'videos'),
26+
'code' => str()->uuid()
27+
]);
28+
29+
dispatch(new VideoEncodingJob($video));
30+
}
31+
}
32+
33+
protected function rules()
34+
{
35+
return [
36+
'videos.*' => 'file|mimetypes:video/mp4,video/mpeg,video/x-matroska,application/octet-stream'
37+
];
1638
}
1739

1840
public function render()

app/Models/Content.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
78

89
class Content extends Model
910
{
@@ -14,4 +15,9 @@ class Content extends Model
1415
'title','code', 'description',
1516
'body', 'slug', 'cover', 'status', 'type'
1617
];
18+
19+
public function videos(): HasMany
20+
{
21+
return $this->hasMany(Video::class);
22+
}
1723
}

app/Models/Video.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Traits\Sluggable;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Video extends Model
9+
{
10+
use Sluggable;
11+
12+
protected $slugColumnFrom = 'name';
13+
14+
protected $fillable = [
15+
'code', 'name', 'video', 'is_processed', 'thumb'
16+
];
17+
}

app/Traits/Sluggable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use Spatie\Sluggable\HasSlug;
6+
use Spatie\Sluggable\SlugOptions;
7+
8+
trait Sluggable
9+
{
10+
use HasSlug;
11+
12+
public function getSlugOptions(): SlugOptions
13+
{
14+
return SlugOptions::create()
15+
->generateSlugsFrom($this->slugColumnFrom)
16+
->saveSlugsTo('slug');
17+
}
18+
}

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"laravel/framework": "^12.0",
1515
"laravel/tinker": "^2.10.1",
1616
"livewire/flux": "^2.1.1",
17-
"livewire/volt": "^1.7.0"
17+
"livewire/volt": "^1.7.0",
18+
"pbmedia/laravel-ffmpeg": "^8.7",
19+
"spatie/laravel-sluggable": "^3.7"
1820
},
1921
"require-dev": {
2022
"fakerphp/faker": "^1.23",
@@ -79,4 +81,4 @@
7981
},
8082
"minimum-stability": "stable",
8183
"prefer-stable": true
82-
}
84+
}

0 commit comments

Comments
 (0)