Play videos on sprites, converted on the server with clips (#402)
game-maker / gate (push) Successful in 4m15s
game-maker / suite (push) Failing after 15m49s

A video becomes an asset like a sound: uploaded once, converted to one
format by a worker of its own, cut into clips in its tab, and worn by a
sprite through its Texture row. Only Chrome is a target, without alpha.

- Server: POST ?r=videos queues the upload under the hash of its bytes,
  so the MP4's name is known at once. The worker (VideoWorker over
  VideoFormat) writes H.264/AAC, yuv420p, constant frame rate, a
  keyframe every 15 frames, at most 1920x1080, with a poster and the
  details beside it. A failed job says why and is tried again on the
  next upload.
- An upload may be up to 2 GB. It is streamed from the request to disk
  and hashed there, so php-fpm holds none of it in memory. The other
  routes keep their 256 MB.
- The worker runs in the container game-maker-video on stein2, a
  compose project of its own (tools/video/), and in CI beside the PHP
  server. The CI image has ffmpeg for it.
- Model: asset type video with size, length, frame rate and sound, and
  clips like a sound's. The clip helpers and commands serve both.
- A sprite's Texture row offers videos and clips after the pictures.
  resolvePicture names a playing video by a key, and the texture library
  hands it to a VideoLibrary with one player per key.
- The editor shows a placed video as a quiet preview. Play starts it
  over at its clip's start and lets it be heard through the master bus,
  a pause holds it, Esc makes it quiet again.
- Video tab: the picture, a ruler, the clip strip and a track on the
  Sound tab's time axis. A drag spans a clip, a handle moves an edge,
  Play plays the picked clip.
- The picker shows a video on its way with its progress and adopts an
  upload from it once it is ready. Dropping a video file does the same.

Decided on my own:

- Two sprites wearing the same clip share one player and show the same
  picture. Control per instance belongs to the follow-up with nodes.
- A video plays in real time, the game's time scale does not slow it.
- The container may use 3 of the 4 cores of stein2.

Findings:

- The first real upload (279 MB) got 413: the API's own ceiling of
  256 MB for every request, not nginx or the Brandmauer.
- Chrome has no picture of a video that never played, and a Pixi
  VideoSource drawn before the first frame stays empty for good. The
  texture is made in the first frame callback (openVideo).
- A player nobody asks for in 2 s is let go. The inspector's PREVIEW
  asked only on an edit, so with the sprite not on screen its texture
  was destroyed under it and the batcher threw on every frame. The
  preview now redraws every frame while it shows a video, like a Time
  shader. A let-go texture keeps an empty source instead of none.
- VideoSource.load() reloads the element and cuts off an earlier
  play(). Playing starts after loadeddata.

Tests:

- PHP videos.test.php: 68 checks, with the real ffmpeg
- 300 MB upload through Brandmauer and nginx: 201 in 21 s, php-fpm at
  13.7 MB RSS at most
- Unit videos.test.ts: 20 tests
- e2e 66 (stein2, atx was off): ALL PASSED, 18 checks; e2e 04 with the
  new asset count green
- Gate (atx): 294 files, 4520 tests. serialize.test.ts used video as
  its example of an unknown type and now uses a made-up one.
- Counter-checks: a keyframe gap of 30 fails the PHP check, a key that
  keeps the video's end fails 2 unit tests, a clip without its end
  fails "plays only its stretch" in e2e 66

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-11 12:12:39 +02:00
1 parent 2f376262d3
commit d48f174caf
63 files changed
+3500 -103

No files matched your search

+52 -13
View File
@@ -63,6 +63,13 @@ declare(strict_types=1);
* POST ?r=sounds&name=x.wav raw sound bytes → {file,name,bytes}
* DELETE ?r=sounds/{file} → 204 | 403 | 404 | 409
*
* GET ?r=videos → [{file,name,status,…,owner,deletable}]
* POST ?r=videos&name=x.mov raw video bytes → {file,name,status,…}
* DELETE ?r=videos/{file} → 204 | 403 | 404 | 409
* (an upload is converted by the worker in `game-maker-video` (#402):
* `status` is queued, converting with `progress`, failed with `error`,
* or ready with width, height, duration, fps, audio and `poster`)
*
* Uploads land in the uploader's own folder, and `file` carries it: `{folder}/{sha256}.{ext}`
* (#344). A `file` without a folder belongs to nobody. A list shows the files of the one asking and
* nobody's, an administrator's every file; `owner` is the account a file belongs to (null for
@@ -78,8 +85,8 @@ declare(strict_types=1);
* empty when none is known. It is a caption, never a path: the stored file is always `{hash}.{ext}`
* in a folder.
*
* Stored images, fonts, sounds and cels are served straight from `data/textures/`, `data/fonts/`,
* `data/sounds/` and `data/pixels/` as static files; the API only writes them.
* Stored images, fonts, sounds, videos and cels are served straight from `data/textures/`, `data/fonts/`,
* `data/sounds/`, `data/videos/` and `data/pixels/` as static files; the API only writes them.
*/
require __DIR__ . '/lib/AccountRules.php';
@@ -99,8 +106,13 @@ require __DIR__ . '/lib/SoundStore.php';
require __DIR__ . '/lib/Store.php';
require __DIR__ . '/lib/TextureStore.php';
require __DIR__ . '/lib/UserStore.php';
require __DIR__ . '/lib/VideoQueue.php';
require __DIR__ . '/lib/VideoStore.php';
/** Has to clear the largest single upload below, which is a sound; the web server has its own say. */
/**
* Has to clear the largest upload held in memory, which is a sound; a video is streamed to disk and
* has its own ceiling. The web server and the proxy in front of it have their own say.
*/
const MAX_BODY_BYTES = 256 * 1024 * 1024;
/** Upload ceiling for a single image. The editor downscales larger pictures before sending. */
@@ -112,6 +124,12 @@ const MAX_FONT_BYTES = 4 * 1024 * 1024;
/** The same for a sound, far higher: a recorded take is plain WAV, about 46 minutes in mono. */
const MAX_SOUND_BYTES = 256 * 1024 * 1024;
/**
* A video goes up as it was recorded and is made smaller by the worker, not on the way. It is
* streamed to disk, so memory is no limit; nginx and the Brandmauer let 2 GB through (#402).
*/
const MAX_VIDEO_BYTES = 2048 * 1024 * 1024;
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
// PHP calls the handler for a call silenced with `@` as well, only with less reported: that
// warning was expected and is answered where it happens (an unreadable image is a 422).
@@ -514,12 +532,21 @@ function routeFiles(
Response::json($store->list($users->folderOf($user), $isAdmin, $users->folderOwners()));
}
if ($method === 'POST') {
$bytes = Response::rawBody();
if (strlen($bytes) > $maxBytes) {
Response::error("{$noun} must be at most " . ($maxBytes / 1024 / 1024) . ' MB', 413);
}
$tooLarge = "{$noun} must be at most " . ($maxBytes / 1024 / 1024) . ' MB';
$name = (string) ($_GET['name'] ?? '');
try {
Response::json($store->store($bytes, (string) ($_GET['name'] ?? ''), $users->folderOf($user, true)), 201);
// A video is too big for memory and goes from the request straight to disk.
if ($store instanceof VideoStore) {
$in = fopen('php://input', 'rb');
Response::json($store->storeStream($in, $maxBytes, $name, $users->folderOf($user, true)), 201);
}
$bytes = Response::rawBody();
if (strlen($bytes) > $maxBytes) {
Response::error($tooLarge, 413);
}
Response::json($store->store($bytes, $name, $users->folderOf($user, true)), 201);
} catch (VideoTooLargeException) {
Response::error($tooLarge, 413);
} catch (RuntimeException $e) {
if (!$e instanceof $invalid) {
throw $e;
@@ -600,19 +627,20 @@ function routePlugins(array $parts, string $method): never
}
try {
if ((int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > MAX_BODY_BYTES) {
Response::error('Request body too large', 413);
}
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$route = trim((string) ($_GET['r'] ?? ''), '/');
$parts = $route === '' ? [] : explode('/', $route);
$maxBody = ($parts[0] ?? '') === 'videos' ? MAX_VIDEO_BYTES : MAX_BODY_BYTES;
if ((int) ($_SERVER['CONTENT_LENGTH'] ?? 0) > $maxBody) {
Response::error('Request body too large', 413);
}
// A project reaches deepest, `projects/{id}/snapshots/{snapshotId}/restore`; a stored file is
// `textures/{folder}/{file}`; everything else stops at two parts.
$depth = match ($parts[0] ?? '') {
'projects' => 5,
'textures', 'fonts', 'sounds' => 3,
'textures', 'fonts', 'sounds', 'videos' => 3,
default => 2,
};
if (count($parts) < 1 || count($parts) > $depth) {
@@ -671,6 +699,17 @@ try {
'Sound',
InvalidSoundException::class,
),
'videos' => routeFiles(
$parts,
$method,
$user,
$isAdmin,
$users,
new VideoStore("{$data}/videos"),
MAX_VIDEO_BYTES,
'Video',
InvalidVideoException::class,
),
'pixels' => routePixels($parts, $method),
'plugins' => routePlugins($parts, $method),
default => Response::error('Not found', 404),
+216
View File
@@ -0,0 +1,216 @@
<?php
declare(strict_types=1);
/**
* The one format every uploaded video is turned into (#402), as plain rules: what ffprobe said in,
* the ffmpeg arguments out. Nothing here runs a process, so all of it is testable without ffmpeg.
*
* The target is MP4 with H.264 and AAC, `yuv420p`, at a constant frame rate and with a keyframe
* every 15 frames: the editor seeks to clip starts and loops back to them, and a seek can only land
* on a keyframe quickly. The picture fits into 1920 × 1080 (1080 × 1920 standing up) and is never
* made larger than it came.
*/
final class VideoFormat
{
public const MAX_LONG_SIDE = 1920;
public const MAX_SHORT_SIDE = 1080;
public const MAX_FPS = 60;
public const KEYFRAME_EVERY = 15;
/** Used when a file does not say its frame rate at all. */
private const FALLBACK_FPS = '30';
/**
* What the conversion makes of a probed file: the size it is shown at after rotation, scaled to
* fit, the frame rate as ffmpeg takes it, and whether there is sound to keep.
*
* @param array<string,mixed> $probe ffprobe's `-show_streams -show_format` as JSON
* @return array{width:int,height:int,fps:string,audio:bool,duration:float}
* @throws InvalidArgumentException when there is no picture in it
*/
public static function plan(array $probe): array
{
$streams = is_array($probe['streams'] ?? null) ? $probe['streams'] : [];
$video = null;
$audio = false;
foreach ($streams as $stream) {
$type = $stream['codec_type'] ?? '';
// A cover picture in a music file is a video stream as well, one that is a single frame.
$still = (int) (($stream['disposition'] ?? [])['attached_pic'] ?? 0) === 1;
if ($type === 'video' && $video === null && !$still) {
$video = $stream;
}
$audio = $audio || $type === 'audio';
}
if ($video === null || (int) ($video['width'] ?? 0) <= 0 || (int) ($video['height'] ?? 0) <= 0) {
throw new InvalidArgumentException('There is no picture in this file');
}
[$width, $height] = [(int) $video['width'], (int) $video['height']];
if (abs(self::rotation($video)) % 180 === 90) {
[$width, $height] = [$height, $width];
}
[$width, $height] = self::fit($width, $height);
return [
'width' => $width,
'height' => $height,
'fps' => self::fps($video),
'audio' => $audio,
'duration' => self::duration($probe, $video),
];
}
/**
* The size inside the box, keeping the shape, both sides even (H.264 in `yuv420p` needs that).
*
* @return array{0:int,1:int}
*/
public static function fit(int $width, int $height): array
{
[$boxW, $boxH] = $width >= $height
? [self::MAX_LONG_SIDE, self::MAX_SHORT_SIDE]
: [self::MAX_SHORT_SIDE, self::MAX_LONG_SIDE];
$scale = min(1.0, $boxW / $width, $boxH / $height);
$even = static fn(float $side): int => max(2, 2 * (int) floor(round($side, 3) / 2));
return [$even($width * $scale), $even($height * $scale)];
}
/**
* The frame rate the file is written at: its own average, unless that is above 60 or unknown.
* Kept as ffmpeg's fraction, so 29.97 stays 30000/1001 and frames do not drift against the time.
*
* @param array<string,mixed> $video
*/
public static function fps(array $video): string
{
foreach (['avg_frame_rate', 'r_frame_rate'] as $key) {
$rate = self::rate((string) ($video[$key] ?? ''));
if ($rate !== null) {
return $rate[1] > self::MAX_FPS ? (string) self::MAX_FPS : $rate[0];
}
}
return self::FALLBACK_FPS;
}
/** A fraction such as `30000/1001` as a number of frames per second. */
public static function fpsValue(string $fps): float
{
$rate = self::rate($fps);
return $rate === null ? 0.0 : $rate[1];
}
/**
* @param array{width:int,height:int,fps:string,audio:bool} $plan
* @return list<string>
*/
public static function convertArgs(string $source, string $target, array $plan): array
{
$args = [
'ffmpeg', '-nostdin', '-hide_banner', '-loglevel', 'error', '-y',
'-i', $source,
'-map', '0:v:0',
];
if ($plan['audio']) {
$args = [...$args, '-map', '0:a:0'];
}
$args = [
...$args,
'-vf', "scale={$plan['width']}:{$plan['height']}:flags=lanczos,setsar=1",
'-r', $plan['fps'], '-fps_mode', 'cfr',
'-c:v', 'libx264', '-preset', 'veryfast', '-crf', '21',
'-pix_fmt', 'yuv420p',
'-g', (string) self::KEYFRAME_EVERY, '-keyint_min', (string) self::KEYFRAME_EVERY, '-sc_threshold', '0',
];
$args = $plan['audio']
? [...$args, '-c:a', 'aac', '-b:a', '160k', '-ac', '2', '-ar', '48000']
: [...$args, '-an'];
return [...$args, '-sn', '-dn', '-map_metadata', '-1', '-movflags', '+faststart', '-progress', 'pipe:1', '-nostats', '-f', 'mp4', $target];
}
/**
* A still from a little way in (not the very first frame, which is often black), as wide as the
* picker draws it.
*
* @return list<string>
*/
public static function posterArgs(string $video, string $target, float $duration): array
{
$at = sprintf('%.3f', max(0.0, min(1.0, $duration / 2)));
return [
'ffmpeg', '-nostdin', '-hide_banner', '-loglevel', 'error', '-y',
'-ss', $at, '-i', $video, '-frames:v', '1',
'-vf', 'scale=320:-2', '-q:v', '4', '-f', 'image2', $target,
];
}
/** @return list<string> */
public static function probeArgs(string $path): array
{
return ['ffprobe', '-v', 'error', '-print_format', 'json', '-show_streams', '-show_format', $path];
}
/**
* What the editor is told about a converted file, read back from the file itself rather than
* the plan: a crop or a rounding inside ffmpeg is what the editor has to draw.
*
* @param array<string,mixed> $probe
* @return array{width:int,height:int,duration:float,fps:float,audio:bool}
*/
public static function details(array $probe): array
{
$plan = self::plan($probe);
return [
'width' => $plan['width'],
'height' => $plan['height'],
'duration' => round($plan['duration'], 3),
'fps' => round(self::fpsValue($plan['fps']), 3),
'audio' => $plan['audio'],
];
}
/** The share of the work done, from one `out_time_us=` line of ffmpeg's progress. */
public static function progress(string $line, float $duration): ?float
{
if ($duration <= 0 || !preg_match('/^out_time_us=(\d+)$/', trim($line), $m)) {
return null;
}
return max(0.0, min(1.0, ((int) $m[1]) / 1e6 / $duration));
}
/** @param array<string,mixed> $video */
private static function rotation(array $video): int
{
foreach (is_array($video['side_data_list'] ?? null) ? $video['side_data_list'] : [] as $side) {
if (isset($side['rotation']) && is_numeric($side['rotation'])) {
return (int) $side['rotation'];
}
}
$tag = ($video['tags'] ?? [])['rotate'] ?? null;
return is_numeric($tag) ? (int) $tag : 0;
}
/** @param array<string,mixed> $probe @param array<string,mixed> $video */
private static function duration(array $probe, array $video): float
{
foreach ([$video['duration'] ?? null, ($probe['format'] ?? [])['duration'] ?? null] as $value) {
if (is_numeric($value) && (float) $value > 0) {
return (float) $value;
}
}
return 0.0;
}
/** @return array{0:string,1:float}|null the fraction as written and its value, null for none */
private static function rate(string $text): ?array
{
if (!preg_match('#^(\d+)(?:/(\d+))?$#', $text, $m)) {
return null;
}
$num = (int) $m[1];
$den = isset($m[2]) ? (int) $m[2] : 1;
if ($num <= 0 || $den <= 0) {
return null;
}
return [$den === 1 ? (string) $num : "{$num}/{$den}", $num / $den];
}
}
+173
View File
@@ -0,0 +1,173 @@
<?php
declare(strict_types=1);
/**
* The videos waiting to be converted (#402): `data/videos/queue/{folder}-{hash}.src` holds the
* uploaded bytes, `{folder}-{hash}.json` what became of them. The API adds to it, the worker in the
* container `game-maker-video` takes from it, one at a time, oldest first.
*
* A job is `queued`, `converting` (with `progress` from 0 to 1) or `failed` (with `error`). A done
* one is no job any more: its MP4 lies in the account's folder and the job is gone. A failed one
* stays, so the picker can say why, until the file is deleted or uploaded again.
*
* Both files are hidden from the web server: the upload is not what anybody should be served.
*/
final class VideoQueue
{
public const QUEUED = 'queued';
public const CONVERTING = 'converting';
public const FAILED = 'failed';
public function __construct(private readonly string $dir)
{
if (!is_dir($this->dir) && !mkdir($this->dir, 0775, true) && !is_dir($this->dir)) {
throw new RuntimeException("Cannot create directory {$this->dir}");
}
}
/**
* Copies an upload into a temp file beside the jobs, never more than `$maxBytes` of it: a video
* is too big to be held in memory. The caller hands the file to add() or deletes it.
*
* @param resource $in
* @throws VideoTooLargeException when the stream has more than `$maxBytes`
*/
public function receive($in, int $maxBytes): string
{
$tmp = "{$this->dir}/upload-" . bin2hex(random_bytes(8)) . '.tmp';
$out = fopen($tmp, 'xb');
if ($out === false) {
throw new RuntimeException('Cannot write uploaded video');
}
$copied = stream_copy_to_stream($in, $out, $maxBytes + 1);
fclose($out);
if ($copied === false || $copied > $maxBytes) {
unlink($tmp);
throw $copied === false ? new RuntimeException('Cannot write uploaded video') : new VideoTooLargeException();
}
return $tmp;
}
/** Queues the upload received into `$upload`, which is moved and gone afterwards. */
public function add(string $folder, string $hash, string $upload, string $name): void
{
if (!chmod($upload, AtomicFile::HIDDEN) || !rename($upload, $this->source($folder, $hash))) {
throw new RuntimeException('Cannot write uploaded video');
}
$this->write($folder, $hash, [
'folder' => $folder,
'hash' => $hash,
'name' => $name,
'state' => self::QUEUED,
'progress' => 0,
'error' => '',
'createdAt' => gmdate('c'),
]);
}
/** @return array<string,mixed>|null */
public function job(string $folder, string $hash): ?array
{
$path = $this->jobPath($folder, $hash);
if (!is_file($path)) {
return null;
}
$raw = @file_get_contents($path);
$job = $raw === false ? null : json_decode($raw, true);
return is_array($job) ? $job : null;
}
/** @return list<array<string,mixed>> every job, oldest first */
public function jobs(): array
{
$jobs = [];
foreach (glob($this->dir . '/*.json') ?: [] as $path) {
if (!preg_match('/^(' . UserStore::FOLDER_PATTERN . ')-([a-f0-9]{64})\.json$/', basename($path), $m)) {
continue;
}
$job = $this->job($m[1], $m[2]);
if ($job !== null) {
$jobs[] = $job;
}
}
usort($jobs, static fn(array $a, array $b): int => strcmp((string) $a['createdAt'], (string) $b['createdAt']));
return $jobs;
}
/** The oldest job nobody has started on, or null when there is none. */
public function next(): ?array
{
foreach ($this->jobs() as $job) {
if ($job['state'] === self::QUEUED && is_file($this->source($job['folder'], $job['hash']))) {
return $job;
}
}
return null;
}
/**
* Changes a job, unless it was deleted in the meantime: then false, and the one converting it
* knows to stop.
*
* @param array<string,mixed> $changes
*/
public function update(string $folder, string $hash, array $changes): bool
{
$job = $this->job($folder, $hash);
if ($job === null) {
return false;
}
$this->write($folder, $hash, $changes + $job);
return true;
}
/** A job that was being converted when the worker stopped is started again. */
public function requeueInterrupted(): int
{
$count = 0;
foreach ($this->jobs() as $job) {
if ($job['state'] === self::CONVERTING) {
$this->update($job['folder'], $job['hash'], ['state' => self::QUEUED, 'progress' => 0]);
$count++;
}
}
return $count;
}
public function remove(string $folder, string $hash): bool
{
$removed = false;
foreach ([$this->source($folder, $hash), $this->jobPath($folder, $hash)] as $path) {
if (is_file($path)) {
$removed = unlink($path) || $removed;
}
}
return $removed;
}
/** Drops the upload once it is converted or cannot be; the job itself says what happened. */
public function dropSource(string $folder, string $hash): void
{
$path = $this->source($folder, $hash);
if (is_file($path)) {
unlink($path);
}
}
public function source(string $folder, string $hash): string
{
return "{$this->dir}/{$folder}-{$hash}.src";
}
private function jobPath(string $folder, string $hash): string
{
return "{$this->dir}/{$folder}-{$hash}.json";
}
/** @param array<string,mixed> $job */
private function write(string $folder, string $hash, array $job): void
{
$json = json_encode($job, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
AtomicFile::write($this->jobPath($folder, $hash), (string) $json, AtomicFile::HIDDEN, 'video job');
}
}
+234
View File
@@ -0,0 +1,234 @@
<?php
declare(strict_types=1);
/**
* Videos (#402): `data/videos/{folder}/{hash}.mp4`, beside it `{hash}.jpg` (the poster) and
* `{hash}.meta.json` (size, length, frame rate, sound), all served as they are.
*
* Unlike an image or a sound a video is not kept as it came: the upload goes into the VideoQueue,
* and the worker writes the MP4 when it is done (see VideoFormat). The hash is the one of the
* uploaded bytes, so the name a project stores is known the moment the upload is in, long before
* the file exists, and uploading the same file twice converts it once.
*/
final class VideoStore extends HashedFileStore
{
/**
* What an upload has to start with to be sent to ffmpeg at all: the ISO family (MP4, MOV, M4V,
* 3GP), Matroska (MKV, WebM), AVI and MPEG transport streams. ffmpeg has the last word.
*/
private const SIGNATURE_HINT = 'MP4, MOV, WebM, MKV or AVI';
private readonly VideoQueue $queue;
public function __construct(private readonly string $root)
{
parent::__construct($root);
$this->queue = new VideoQueue("{$root}/queue");
}
protected static function filePattern(): string
{
return '[a-f0-9]{64}\.mp4';
}
public function queue(): VideoQueue
{
return $this->queue;
}
/**
* Takes an upload: a file already converted answers as it is, anything else waits in the
* queue, a failed one is tried again.
*
* @return array<string,mixed>
* @throws InvalidVideoException when the bytes cannot be a video
*/
public function store(string $bytes, string $originalName, string $folder): array
{
$in = fopen('php://memory', 'r+b');
fwrite($in, $bytes);
rewind($in);
try {
return $this->storeStream($in, strlen($bytes), $originalName, $folder);
} finally {
fclose($in);
}
}
/**
* Like store(), straight from the request: the bytes go to disk as they come, and only their
* first ones and their hash are looked at, so a video of 2 GB needs no 2 GB of memory.
*
* @param resource $in
* @return array<string,mixed>
* @throws VideoTooLargeException when the stream has more than `$maxBytes`
* @throws InvalidVideoException when the bytes cannot be a video
*/
public function storeStream($in, int $maxBytes, string $originalName, string $folder): array
{
$upload = $this->queue->receive($in, $maxBytes);
try {
if (!self::looksLikeVideo((string) file_get_contents($upload, false, null, 0, 256))) {
throw new InvalidVideoException('Not a video file – use ' . self::SIGNATURE_HINT);
}
if (!preg_match('/^' . UserStore::FOLDER_PATTERN . '$/', $folder)) {
throw new InvalidArgumentException("Invalid folder: {$folder}");
}
$hash = hash_file('sha256', $upload);
$dir = "{$this->root}/{$folder}";
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
throw new RuntimeException("Cannot create directory {$dir}");
}
OriginalName::write($dir, "{$hash}.mp4", $originalName);
if (!is_file("{$dir}/{$hash}.mp4")) {
$job = $this->queue->job($folder, $hash);
if ($job === null || $job['state'] === VideoQueue::FAILED) {
$this->queue->add($folder, $hash, $upload, OriginalName::sanitize($originalName));
}
}
return $this->record("{$folder}/{$hash}.mp4");
} finally {
if (is_file($upload)) {
unlink($upload);
}
}
}
/**
* One file as the picker shows it, done or not; null when there is neither file nor job.
*
* @return array<string,mixed>|null
*/
public function record(string $file): ?array
{
if (!self::isValidName($file)) {
return null;
}
if (is_file("{$this->root}/{$file}")) {
$folder = self::folderOf($file);
$dir = $folder === '' ? $this->root : "{$this->root}/{$folder}";
return [
'file' => $file,
'name' => OriginalName::read($dir, basename($file)),
...$this->details("{$this->root}/{$file}"),
'bytes' => (int) filesize("{$this->root}/{$file}"),
];
}
$job = $this->jobOf($file);
return $job === null ? null : $this->pending($job);
}
/** A video is there for deleting while it is converted or failed, too. */
public function exists(string $file): bool
{
return parent::exists($file) || $this->jobOf($file) !== null;
}
/** Lists the converted files like every store does, and the ones on their way after them. */
public function list(string $folder, bool $seesAll, array $owners): array
{
$items = parent::list($folder, $seesAll, $owners);
foreach ($this->queue->jobs() as $job) {
if ($seesAll || $job['folder'] === $folder) {
$items[] = $this->pending($job) + [
'updatedAt' => (string) $job['createdAt'],
'owner' => $owners[$job['folder']] ?? '',
'deletable' => true,
];
}
}
return $items;
}
public function delete(string $file): bool
{
if (!self::isValidName($file)) {
return false;
}
$folder = self::folderOf($file);
$hash = basename($file, '.mp4');
$removed = $folder !== '' && $this->queue->remove($folder, $hash);
if (is_file("{$this->root}/{$file}")) {
$removed = parent::delete($file) || $removed;
$dir = $folder === '' ? $this->root : "{$this->root}/{$folder}";
foreach (["{$dir}/{$hash}.jpg", "{$dir}/{$hash}.meta.json"] as $path) {
if (is_file($path)) {
unlink($path);
}
}
} elseif ($removed) {
OriginalName::delete("{$this->root}/{$folder}", "{$hash}.mp4");
}
return $removed;
}
/** Called by the worker once the MP4 is in place: what the editor reads instead of probing. */
public static function writeDetails(string $dir, string $hash, array $details): void
{
$json = json_encode($details, JSON_UNESCAPED_SLASHES);
AtomicFile::write("{$dir}/{$hash}.meta.json", (string) $json, AtomicFile::SERVED, 'video details');
}
/** @return array<string,mixed> */
protected function details(string $path): array
{
$meta = substr($path, 0, -strlen('.mp4')) . '.meta.json';
$raw = is_file($meta) ? file_get_contents($meta) : false;
$details = $raw === false ? null : json_decode($raw, true);
$details = is_array($details) ? $details : [];
$file = substr($path, strlen($this->root) + 1);
return [
'status' => 'ready',
'width' => (int) ($details['width'] ?? 0),
'height' => (int) ($details['height'] ?? 0),
'duration' => (float) ($details['duration'] ?? 0),
'fps' => (float) ($details['fps'] ?? 0),
'audio' => (bool) ($details['audio'] ?? false),
'poster' => substr($file, 0, -strlen('.mp4')) . '.jpg',
];
}
/** @return array<string,mixed>|null */
private function jobOf(string $file): ?array
{
$folder = self::folderOf($file);
return $folder === '' ? null : $this->queue->job($folder, basename($file, '.mp4'));
}
/**
* @param array<string,mixed> $job
* @return array<string,mixed>
*/
private function pending(array $job): array
{
return [
'file' => "{$job['folder']}/{$job['hash']}.mp4",
'name' => (string) $job['name'],
'status' => (string) $job['state'],
'progress' => (float) $job['progress'],
'error' => (string) $job['error'],
'bytes' => 0,
];
}
private static function looksLikeVideo(string $bytes): bool
{
if (strlen($bytes) < 16) {
return false;
}
return substr($bytes, 4, 4) === 'ftyp'
|| str_starts_with($bytes, "\x1A\x45\xDF\xA3")
|| (str_starts_with($bytes, 'RIFF') && substr($bytes, 8, 4) === 'AVI ')
|| ($bytes[0] === "\x47" && strlen($bytes) > 188 && $bytes[188] === "\x47");
}
}
/** Thrown by VideoStore::store when the uploaded bytes cannot be a video. */
final class InvalidVideoException extends RuntimeException
{
}
/** Thrown by VideoQueue::receive when an upload is bigger than the API takes. */
final class VideoTooLargeException extends RuntimeException
{
}
+150
View File
@@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
/**
* Converts what waits in the VideoQueue, one video at a time (#402). Runs in the container
* `game-maker-video` through `tools/video-worker.php`, and in CI beside the PHP server.
*
* Each job goes: probe the upload, convert it into a temp file beside its target, read the result
* back for its details, cut a poster, then move all three into place and drop the job. Whatever
* fails on the way fails the job with ffmpeg's own words, and the temp files go.
*/
final class VideoWorker
{
/** How often a running conversion writes its progress, in seconds. */
private const PROGRESS_EVERY = 0.5;
public function __construct(private readonly VideoStore $store, private readonly string $videos)
{
}
/** Converts the oldest waiting video; false when there was none. */
public function runOnce(): bool
{
$job = $this->store->queue()->next();
if ($job === null) {
return false;
}
$this->convert((string) $job['folder'], (string) $job['hash']);
return true;
}
private function convert(string $folder, string $hash): void
{
$queue = $this->store->queue();
if (!$queue->update($folder, $hash, ['state' => VideoQueue::CONVERTING, 'progress' => 0, 'error' => ''])) {
return;
}
$dir = "{$this->videos}/{$folder}";
$tag = bin2hex(random_bytes(4));
$tmpVideo = "{$dir}/{$hash}.{$tag}.tmp.mp4";
$tmpPoster = "{$dir}/{$hash}.{$tag}.tmp.jpg";
try {
$plan = VideoFormat::plan(self::probe($queue->source($folder, $hash)));
$finished = $this->run(
VideoFormat::convertArgs($queue->source($folder, $hash), $tmpVideo, $plan),
$plan['duration'],
static fn(float $p): bool => $queue->update($folder, $hash, ['progress' => round($p, 3)]),
);
if (!$finished) {
return;
}
$details = VideoFormat::details(self::probe($tmpVideo));
$this->run(VideoFormat::posterArgs($tmpVideo, $tmpPoster, $details['duration']), 0, null);
if ($queue->job($folder, $hash) === null) {
return;
}
chmod($tmpVideo, AtomicFile::SERVED);
chmod($tmpPoster, AtomicFile::SERVED);
VideoStore::writeDetails($dir, $hash, $details);
rename($tmpPoster, "{$dir}/{$hash}.jpg");
rename($tmpVideo, "{$dir}/{$hash}.mp4");
$queue->remove($folder, $hash);
} catch (Throwable $e) {
$queue->dropSource($folder, $hash);
$queue->update($folder, $hash, ['state' => VideoQueue::FAILED, 'error' => self::shorten($e->getMessage())]);
} finally {
foreach ([$tmpVideo, $tmpPoster] as $path) {
if (is_file($path)) {
unlink($path);
}
}
}
}
/** @return array<string,mixed> */
private static function probe(string $path): array
{
[$code, $out, $err] = self::capture(VideoFormat::probeArgs($path));
$probe = json_decode($out, true);
if ($code !== 0 || !is_array($probe)) {
throw new RuntimeException('ffmpeg cannot read this file' . ($err === '' ? '' : ": {$err}"));
}
return $probe;
}
/**
* Runs ffmpeg, reporting progress from its `-progress pipe:1` lines to `$report`. Returns false
* when `$report` says the job is gone, after stopping ffmpeg.
*
* @param list<string> $args
* @param (callable(float):bool)|null $report
*/
private function run(array $args, float $duration, ?callable $report): bool
{
$err = tempnam(sys_get_temp_dir(), 'video-err-');
$process = proc_open($args, [0 => ['file', '/dev/null', 'r'], 1 => ['pipe', 'w'], 2 => ['file', $err, 'w']], $pipes);
if (!is_resource($process)) {
throw new RuntimeException('Cannot start ffmpeg');
}
$last = 0.0;
$alive = true;
while (($line = fgets($pipes[1])) !== false) {
$share = $report === null ? null : VideoFormat::progress($line, $duration);
if ($share !== null && microtime(true) - $last >= self::PROGRESS_EVERY) {
$last = microtime(true);
if (!$report($share)) {
$alive = false;
proc_terminate($process);
break;
}
}
}
fclose($pipes[1]);
$code = proc_close($process);
$message = trim((string) file_get_contents($err));
unlink($err);
if (!$alive) {
return false;
}
if ($code !== 0) {
throw new RuntimeException($message === '' ? "ffmpeg stopped with code {$code}" : $message);
}
return true;
}
/**
* @param list<string> $args
* @return array{0:int,1:string,2:string}
*/
private static function capture(array $args): array
{
$process = proc_open($args, [0 => ['file', '/dev/null', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
if (!is_resource($process)) {
throw new RuntimeException('Cannot start ffprobe');
}
$out = (string) stream_get_contents($pipes[1]);
$err = trim((string) stream_get_contents($pipes[2]));
fclose($pipes[1]);
fclose($pipes[2]);
return [proc_close($process), $out, $err];
}
/** The last lines of what ffmpeg said: enough to tell why, not a log. */
private static function shorten(string $message): string
{
$lines = array_values(array_filter(array_map('trim', explode("\n", $message)), static fn(string $l): bool => $l !== ''));
$text = implode(' · ', array_slice($lines, -2));
return mb_strlen($text) > 300 ? mb_substr($text, 0, 300) . '…' : $text;
}
}