-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetPostVideoConversions.php
63 lines (46 loc) · 1.6 KB
/
GetPostVideoConversions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace App\Actions;
use FFMpeg\FFMpeg;
class GetPostVideoConversions
{
const MAX_BITRATE = 5000;
const MAX_WIDTH = 1920;
const MAX_HEIGHT = 1080;
/**
* @param $mediaPath
*
* @return \Illuminate\Support\Collection|void
*/
public function handle($mediaPath)
{
$ffmpeg = FFMpeg::create([
'ffmpeg.binaries' => config('media-library.ffmpeg_path'),
'ffprobe.binaries' => config('media-library.ffprobe_path'),
'timeout' => 3600,
]);
$conversions = collect([]);
$videoFormat = $ffmpeg->getFFProbe()->format($mediaPath);
$stream = $ffmpeg->getFFProbe()
->streams($mediaPath)
->videos()
->first();
if (!$stream) {
return $conversions;
}
$dimensions = $stream->getDimensions();
if (strtolower($stream->get('codec_name')) != 'h264') {
$conversions->push(['name' => 'codec']);
}
if ($stream->get('bits_per_raw_sample') > 8) {
$conversions->push(['name' => 'bits_per_raw_sample']);
}
if ($dimensions->getWidth() > self::MAX_WIDTH || $dimensions->getHeight() > self::MAX_HEIGHT) {
$conversions->push(['name' => 'dimensions', 'width' => self::MAX_WIDTH, 'height' => self::MAX_HEIGHT]);
}
$bitRate = $videoFormat->get('bit_rate') / 1000;
if ($bitRate > self::MAX_BITRATE || $bitRate == 0) {
$conversions->push(['name' => 'bitrate', 'bitrate' => min(self::MAX_BITRATE, $bitRate)]);
}
return $conversions;
}
}