82 lines
2.5 KiB
PHP
82 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use RuntimeException;
|
|
use Symfony\Component\Process\ExecutableFinder;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class FfmpegFilmTranscoder implements FilmTranscoder
|
|
{
|
|
public function available(): bool
|
|
{
|
|
return (new ExecutableFinder)->find('ffmpeg') !== null;
|
|
}
|
|
|
|
public function derive(int $versionId, string $sourceRelative): array
|
|
{
|
|
$binary = (new ExecutableFinder)->find('ffmpeg');
|
|
|
|
if ($binary === null) {
|
|
return [];
|
|
}
|
|
|
|
$disk = Storage::disk('media_derived');
|
|
$source = $disk->path($sourceRelative);
|
|
$base = 'ableitungen/'.$versionId;
|
|
$rows = [];
|
|
|
|
foreach ([480, 720, 1080] as $height) {
|
|
$directory = $base.'/'.$height;
|
|
$disk->makeDirectory($directory);
|
|
$playlist = $disk->path($directory.'/index.m3u8');
|
|
$segment = $disk->path($directory.'/segment_%03d.m4s');
|
|
$process = new Process([
|
|
$binary,
|
|
'-y',
|
|
'-i',
|
|
$source,
|
|
'-vf',
|
|
'scale=-2:'.$height,
|
|
'-c:v',
|
|
'libx264',
|
|
'-preset',
|
|
'veryfast',
|
|
'-crf',
|
|
'23',
|
|
'-c:a',
|
|
'aac',
|
|
'-hls_time',
|
|
'6',
|
|
'-hls_playlist_type',
|
|
'vod',
|
|
'-hls_segment_filename',
|
|
$segment,
|
|
$playlist,
|
|
]);
|
|
$process->setTimeout(3600);
|
|
$process->run();
|
|
|
|
if (! $process->isSuccessful()) {
|
|
throw new RuntimeException('Die Qualitätsstufe '.$height.' ließ sich nicht ableiten.');
|
|
}
|
|
|
|
$rows[] = [
|
|
'role' => 'variant',
|
|
'label' => (string) $height,
|
|
'path' => $directory.'/index.m3u8',
|
|
];
|
|
}
|
|
|
|
$master = "#EXTM3U\n#EXT-X-VERSION:3\n"
|
|
."#EXT-X-STREAM-INF:BANDWIDTH=800000,RESOLUTION=854x480\n480/index.m3u8\n"
|
|
."#EXT-X-STREAM-INF:BANDWIDTH=2500000,RESOLUTION=1280x720\n720/index.m3u8\n"
|
|
."#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080\n1080/index.m3u8\n";
|
|
$disk->put($base.'/master.m3u8', $master);
|
|
$rows[] = ['role' => 'master', 'label' => 'master', 'path' => $base.'/master.m3u8'];
|
|
|
|
return $rows;
|
|
}
|
|
}
|