Leite Vorschaubilder und Filmstufen geschuetzt ab
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d6ff881e4e
commit
c997eb500c
32 files changed
+979
-68
No files matched your search
@@ -44,6 +44,38 @@ public function explanation(): string
|
||||
};
|
||||
}
|
||||
|
||||
public function deskLabel(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Kombination => 'Fachliche Kombination',
|
||||
self::Uebung => 'Modul',
|
||||
self::Kurs => 'Programm',
|
||||
};
|
||||
}
|
||||
|
||||
public function deskExplanation(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Kombination => $this->explanation(),
|
||||
self::Uebung => 'Ein Modul mit Lernziel, Zeit, Material und geordneten Bestandteilen. Dieselben Medien können in mehreren Modulen stecken.',
|
||||
self::Kurs => 'Ein Programm aus Modulen und weiteren Inhalten.',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function deskOptions(): array
|
||||
{
|
||||
$options = [];
|
||||
|
||||
foreach (self::cases() as $case) {
|
||||
$options[$case->value] = $case->deskLabel();
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
|
||||
@@ -13,6 +13,9 @@ class MediaDelivery extends Model
|
||||
'content_element_version_id',
|
||||
'action',
|
||||
'format',
|
||||
'copy_id',
|
||||
'checksum',
|
||||
'marking',
|
||||
'delivered_at',
|
||||
];
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
use App\Domain\Catalog\ArticleRelease;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use App\Domain\Catalog\LicenseArticle;
|
||||
use App\Domain\Entitlement\Grant;
|
||||
use App\Domain\Entitlement\MediaDelivery;
|
||||
|
||||
class DeliveryLog
|
||||
{
|
||||
public function write(
|
||||
Grant $grant,
|
||||
LicenseArticle $article,
|
||||
ArticleRelease $release,
|
||||
ContentElementVersion $version,
|
||||
string $action,
|
||||
string $format,
|
||||
?string $checksum,
|
||||
string $marking,
|
||||
): MediaDelivery {
|
||||
return MediaDelivery::query()->create([
|
||||
'grant_id' => $grant->id,
|
||||
'license_article_id' => $article->id,
|
||||
'article_release_id' => $release->id,
|
||||
'content_element_version_id' => $version->id,
|
||||
'action' => $action,
|
||||
'format' => $format,
|
||||
'copy_id' => 'LK-'.strtoupper(bin2hex(random_bytes(6))),
|
||||
'checksum' => $checksum,
|
||||
'marking' => $marking,
|
||||
'delivered_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
interface FilmTranscoder
|
||||
{
|
||||
public function available(): bool;
|
||||
|
||||
/**
|
||||
* @return list<array{role: string, label: string, path: string}>
|
||||
*/
|
||||
public function derive(int $versionId, string $sourceRelative): array;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
class HlsPlaylist
|
||||
{
|
||||
public function rewrite(string $playlist, string $playlistPath, callable $sign): string
|
||||
{
|
||||
$directory = str_replace('\\', '/', dirname($playlistPath));
|
||||
$lines = preg_split("/\r\n|\n|\r/", $playlist) ?: [];
|
||||
$rewritten = [];
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$value = trim($line);
|
||||
|
||||
if ($value === '' || str_starts_with($value, '#')) {
|
||||
$rewritten[] = $line;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$target = str_starts_with($value, 'ableitungen/')
|
||||
? $value
|
||||
: ($directory === '.' ? $value : $directory.'/'.$value);
|
||||
$rewritten[] = $sign($target);
|
||||
}
|
||||
|
||||
return implode("\n", $rewritten)."\n";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MediaRendition extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'content_element_version_id',
|
||||
'role',
|
||||
'label',
|
||||
'path',
|
||||
];
|
||||
|
||||
public static function masterPath(int $versionId): ?string
|
||||
{
|
||||
$path = static::query()
|
||||
->where('content_element_version_id', $versionId)
|
||||
->where('role', 'master')
|
||||
->value('path');
|
||||
|
||||
return is_string($path) && $path !== '' ? $path : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Media;
|
||||
|
||||
use App\Domain\Catalog\Composition;
|
||||
use App\Domain\Catalog\ContentElement;
|
||||
use App\Domain\Catalog\ContentElementVersion;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\Process\ExecutableFinder;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class ThumbnailDeriver
|
||||
{
|
||||
public function forElement(ContentElement $element): void
|
||||
{
|
||||
$version = $element->versions()->orderByDesc('number')->first();
|
||||
$file = is_array($version?->metadata) ? ($version->metadata['file'] ?? null) : null;
|
||||
|
||||
if (! $version instanceof ContentElementVersion || ! is_string($file) || $file === '') {
|
||||
$this->fail($element, 'Für dieses Medium liegt noch keine Datei vor.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (in_array($element->output_class, ['film', 'audio'], true)) {
|
||||
$this->frame($element, $file);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->still($element, Storage::disk('media_derived'), $file, 'element-'.$element->id);
|
||||
}
|
||||
|
||||
public function forCompositionUpload(Composition $composition, string $incoming): void
|
||||
{
|
||||
if (! str_starts_with($incoming, 'vorschau/') || str_contains($incoming, '..')) {
|
||||
$this->failComposition($composition, 'Das Vorschaubild wurde nicht übernommen.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->still($composition, Storage::disk('cms_incoming'), $incoming, 'komposition-'.$composition->id);
|
||||
}
|
||||
|
||||
private function frame(ContentElement $element, string $file): void
|
||||
{
|
||||
$binary = (new ExecutableFinder)->find('ffmpeg');
|
||||
$disk = Storage::disk('media_derived');
|
||||
|
||||
if ($binary === null || ! $disk->exists($file)) {
|
||||
$this->fail($element, 'Für dieses Medium liegt noch kein abgeleitetes Vorschaubild vor.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$target = 'vorschau/element-'.$element->id.'.jpg';
|
||||
$disk->makeDirectory('vorschau');
|
||||
$process = new Process([
|
||||
$binary,
|
||||
'-y',
|
||||
'-ss',
|
||||
'0',
|
||||
'-i',
|
||||
$disk->path($file),
|
||||
'-frames:v',
|
||||
'1',
|
||||
$disk->path($target),
|
||||
]);
|
||||
$process->setTimeout(120);
|
||||
$process->run();
|
||||
|
||||
if (! $process->isSuccessful() || ! $disk->exists($target)) {
|
||||
$this->fail($element, 'Aus der Filmdatei ließ sich kein Vorschaubild erzeugen.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$element->thumbnail_path = $target;
|
||||
$element->thumbnail_alt = $element->working_title;
|
||||
$element->thumbnail_error = null;
|
||||
$element->save();
|
||||
}
|
||||
|
||||
private function still(ContentElement|Composition $subject, Filesystem $disk, string $file, string $name): void
|
||||
{
|
||||
if (! $disk->exists($file)) {
|
||||
$this->failSubject($subject, 'Die Bilddatei wurde nicht gefunden.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$image = @imagecreatefromstring($disk->get($file));
|
||||
|
||||
if ($image === false) {
|
||||
$this->failSubject($subject, 'Die Bilddatei ließ sich nicht lesen.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$width = imagesx($image);
|
||||
$height = imagesy($image);
|
||||
$targetWidth = 640;
|
||||
$targetHeight = (int) max(1, round($height * ($targetWidth / max(1, $width))));
|
||||
$canvas = imagecreatetruecolor($targetWidth, $targetHeight);
|
||||
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
|
||||
ob_start();
|
||||
imagejpeg($canvas, null, 82);
|
||||
$bytes = ob_get_clean();
|
||||
imagedestroy($image);
|
||||
imagedestroy($canvas);
|
||||
|
||||
if (! is_string($bytes) || $bytes === '') {
|
||||
$this->failSubject($subject, 'Das Vorschaubild ließ sich nicht schreiben.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$path = 'vorschau/'.$name.'.jpg';
|
||||
Storage::disk('media_derived')->put($path, $bytes);
|
||||
$subject->thumbnail_path = $path;
|
||||
$subject->thumbnail_alt = $subject instanceof Composition ? $subject->designation : $subject->working_title;
|
||||
$subject->thumbnail_error = null;
|
||||
$subject->save();
|
||||
}
|
||||
|
||||
private function fail(ContentElement $element, string $reason): void
|
||||
{
|
||||
$this->failSubject($element, $reason);
|
||||
}
|
||||
|
||||
private function failComposition(Composition $composition, string $reason): void
|
||||
{
|
||||
$this->failSubject($composition, $reason);
|
||||
}
|
||||
|
||||
private function failSubject(ContentElement|Composition $subject, string $reason): void
|
||||
{
|
||||
$subject->thumbnail_error = $reason;
|
||||
$subject->save();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user