Choose size, rate and what follows when recording the editor

The editor recording had fixed settings: the canvas size at 60 fps,
always converted and imported. Recording in 4K needs a choice, and the
server shrank every video to 1080p anyway. Start recording... now opens
a dialog, and a recording always goes to the server as it is.

- The dialog holds name, resolution (Screen, 1080p, 1440p, 4K), fps
  (60 or 30), mouse pointer, sound, Convert and Import after
  conversion. Import is greyed out while Convert is off. The browser
  keeps the last choice, not the name.
- A larger resolution makes the editor draw finer while it records,
  so 4K is sharp, not stretched. It is never below the screen's own
  resolution and goes back after Stop.
- Stop always uploads. Without Convert the file only stays on the
  server. Without Import it is converted but no asset is added. Both
  are found in the video picker.
- The server keeps the original next to each MP4 for every upload, so
  Download in the video tab and the picker gives the original. Videos
  from before have none, they still download their MP4.
- A file left unconverted shows in the picker as not converted, with
  a Convert button. POST videos/{file}/convert queues it, and upload
  takes convert=0.
- The worker now caps a video at 3840x2160 instead of 1920x1080. Videos
  converted before stay as they are.

Tests:
- PHP videos.test.php: 81 checks green, with the original, convert=0,
  a later Convert and the 4K cap.
- Unit recording: 5 new, green. Red without the rule that a recording
  is never below the screen's resolution.
- e2e 67e-ii: the video tab downloads the .source.mkv, a Matroska
  file. 67f: 1440p from a 1280x800 window gives 2304x1440 at 30 fps
  without sound, kept and not imported, then converted in the picker.
  Both red with the MP4 download and without the finer drawing.
- The README names 2045 e2e checks.
- Gate on melektaus-atx: 304 files, 4699 tests green.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-12 17:01:55 +02:00
1 parent 93190e185c
commit ba01d146e9
26 files changed
+1011 -129

No files matched your search

+27 -6
View File
@@ -63,12 +63,16 @@ 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,…}
* GET ?r=videos → [{file,name,status,…,original,owner,deletable}]
* POST ?r=videos&name=x.mov[&convert=0] raw video bytes → {file,name,status,…}
* POST ?r=videos/{file}/convert → {file,name,status,…} | 403 | 404
* 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`)
* or ready with width, height, duration, fps, audio and `poster`;
* with `convert=0` it is only kept, `unconverted`, until it is asked
* to be. `original` is the upload as it came, null for a video
* uploaded before uploads were kept)
*
* 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
@@ -538,7 +542,8 @@ function routeFiles(
// 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);
$convert = ($_GET['convert'] ?? '1') !== '0';
Response::json($store->storeStream($in, $maxBytes, $name, $users->folderOf($user, true), $convert), 201);
}
$bytes = Response::rawBody();
if (strlen($bytes) > $maxBytes) {
@@ -557,6 +562,20 @@ function routeFiles(
Response::error('Method not allowed', 405);
}
// A video kept without being converted is converted later, at its owner's word.
if ($store instanceof VideoStore && $method === 'POST' && end($parts) === 'convert') {
$file = implode('/', array_slice($parts, 1, -1));
$folder = HashedFileStore::folderOf($file);
if (!$isAdmin && ($folder === '' || $folder !== $users->folderOf($user))) {
Response::error('Only its owner may convert this video', 403);
}
$record = $store->convert($file);
if ($record === null) {
Response::error("{$noun} not found", 404);
}
Response::json($record);
}
if ($method === 'DELETE') {
$file = implode('/', array_slice($parts, 1));
// Asked before whether there is one: somebody else's folder tells a stranger nothing.
@@ -637,10 +656,12 @@ try {
}
// A project reaches deepest, `projects/{id}/snapshots/{snapshotId}/restore`; a stored file is
// `textures/{folder}/{file}`; everything else stops at two parts.
// `textures/{folder}/{file}`, a video to convert `videos/{folder}/{file}/convert`; everything
// else stops at two parts.
$depth = match ($parts[0] ?? '') {
'projects' => 5,
'textures', 'fonts', 'sounds', 'videos' => 3,
'videos' => 4,
'textures', 'fonts', 'sounds' => 3,
default => 2,
};
if (count($parts) < 1 || count($parts) > $depth) {
+4 -4
View File
@@ -7,13 +7,13 @@ declare(strict_types=1);
*
* 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.
* on a keyframe quickly. The picture fits into 3840 × 2160 (2160 × 3840 standing up), so a 4K
* recording of the editor stays 4K, 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_LONG_SIDE = 3840;
public const MAX_SHORT_SIDE = 2160;
public const MAX_FPS = 60;
public const KEYFRAME_EVERY = 15;
/** Used when a file does not say its frame rate at all. */
+27 -12
View File
@@ -2,15 +2,17 @@
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
* The videos waiting to be converted (#402): `data/videos/queue/{folder}-{hash}.json` says what
* became of an upload, and its `source` where the upload lies – kept as it came beside the MP4,
* `{folder}/{hash}.source.{ext}`, for downloading it. 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.
* A job from before the uploads were kept has no `source`: its bytes wait hidden in the queue as
* `{folder}-{hash}.src` and go once they are converted.
*/
final class VideoQueue
{
@@ -27,7 +29,7 @@ final class VideoQueue
/**
* 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.
* is too big to be held in memory. The caller keeps the file or deletes it.
*
* @param resource $in
* @throws VideoTooLargeException when the stream has more than `$maxBytes`
@@ -48,15 +50,16 @@ final class VideoQueue
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
/**
* Queues the upload kept at `$source`, a path inside the videos' directory such as
* `{folder}/{hash}.source.mkv`. The upload stays where it is.
*/
public function add(string $folder, string $hash, string $source, 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,
'source' => $source,
'name' => $name,
'state' => self::QUEUED,
'progress' => 0,
@@ -134,10 +137,11 @@ final class VideoQueue
return $count;
}
/** Drops the job, and an old one's hidden upload; a kept upload is the store's to delete. */
public function remove(string $folder, string $hash): bool
{
$removed = false;
foreach ([$this->source($folder, $hash), $this->jobPath($folder, $hash)] as $path) {
foreach ([$this->hiddenUpload($folder, $hash), $this->jobPath($folder, $hash)] as $path) {
if (is_file($path)) {
$removed = unlink($path) || $removed;
}
@@ -145,16 +149,27 @@ final class VideoQueue
return $removed;
}
/** Drops the upload once it is converted or cannot be; the job itself says what happened. */
/**
* Drops an old job's hidden upload once it cannot be converted; the job itself says why. A kept
* upload stays: it can still be downloaded as it came.
*/
public function dropSource(string $folder, string $hash): void
{
$path = $this->source($folder, $hash);
$path = $this->hiddenUpload($folder, $hash);
if (is_file($path)) {
unlink($path);
}
}
/** Where the bytes of a job lie, for ffmpeg to read. */
public function source(string $folder, string $hash): string
{
$source = (string) ($this->job($folder, $hash)['source'] ?? '');
$kept = '#^' . UserStore::FOLDER_PATTERN . '/[a-f0-9]{64}\.source\.[a-z0-9]+$#';
return preg_match($kept, $source) ? dirname($this->dir) . "/{$source}" : $this->hiddenUpload($folder, $hash);
}
private function hiddenUpload(string $folder, string $hash): string
{
return "{$this->dir}/{$folder}-{$hash}.src";
}
+144 -19
View File
@@ -2,22 +2,31 @@
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.
* Videos (#402): `data/videos/{folder}/{hash}.mp4`, beside it `{hash}.jpg` (the poster),
* `{hash}.meta.json` (size, length, frame rate, sound) and `{hash}.source.{ext}`, the upload as it
* came, 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.
* What a project plays is the MP4 the worker makes of the upload (see VideoFormat), which waits in
* the VideoQueue until then. The upload itself is kept for downloading it, and one sent up without
* being converted stays only that until it is asked to be. 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. Videos uploaded before the uploads
* were kept have none.
*/
final class VideoStore extends HashedFileStore
{
/** An upload kept, waiting for nobody to convert it. */
public const UNCONVERTED = 'unconverted';
/**
* 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';
/** The endings a kept upload may have, taken from its name; anything else is told by its bytes. */
private const SOURCE_EXTENSIONS = ['mp4', 'mov', 'm4v', '3gp', 'mkv', 'webm', 'avi', 'ts', 'mts', 'm2ts'];
private readonly VideoQueue $queue;
public function __construct(private readonly string $root)
@@ -38,18 +47,18 @@ final class VideoStore extends HashedFileStore
/**
* Takes an upload: a file already converted answers as it is, anything else waits in the
* queue, a failed one is tried again.
* queue, a failed one is tried again. Without `$convert` it is only kept.
*
* @return array<string,mixed>
* @throws InvalidVideoException when the bytes cannot be a video
*/
public function store(string $bytes, string $originalName, string $folder): array
public function store(string $bytes, string $originalName, string $folder, bool $convert = true): array
{
$in = fopen('php://memory', 'r+b');
fwrite($in, $bytes);
rewind($in);
try {
return $this->storeStream($in, strlen($bytes), $originalName, $folder);
return $this->storeStream($in, strlen($bytes), $originalName, $folder, $convert);
} finally {
fclose($in);
}
@@ -64,11 +73,12 @@ final class VideoStore extends HashedFileStore
* @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
public function storeStream($in, int $maxBytes, string $originalName, string $folder, bool $convert = true): array
{
$upload = $this->queue->receive($in, $maxBytes);
try {
if (!self::looksLikeVideo((string) file_get_contents($upload, false, null, 0, 256))) {
$head = (string) file_get_contents($upload, false, null, 0, 256);
if (!self::looksLikeVideo($head)) {
throw new InvalidVideoException('Not a video file – use ' . self::SIGNATURE_HINT);
}
if (!preg_match('/^' . UserStore::FOLDER_PATTERN . '$/', $folder)) {
@@ -80,12 +90,15 @@ final class VideoStore extends HashedFileStore
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));
if ($this->sourceOf($folder, $hash) === null) {
$source = "{$dir}/{$hash}.source." . self::sourceExtension($originalName, $head);
if (!chmod($upload, AtomicFile::SERVED) || !rename($upload, $source)) {
throw new RuntimeException('Cannot write uploaded video');
}
}
if ($convert) {
$this->enqueue($folder, $hash);
}
return $this->record("{$folder}/{$hash}.mp4");
} finally {
if (is_file($upload)) {
@@ -94,6 +107,35 @@ final class VideoStore extends HashedFileStore
}
}
/**
* Converts an upload that was kept without: it waits in the queue like any other, a failed one
* is tried again. Null when there is no such video.
*
* @return array<string,mixed>|null
*/
public function convert(string $file): ?array
{
$folder = self::isValidName($file) ? self::folderOf($file) : '';
if ($folder === '') {
return null;
}
$this->enqueue($folder, basename($file, '.mp4'));
return $this->record($file);
}
/** Queues a kept upload, unless it is converted or on its way already. */
private function enqueue(string $folder, string $hash): void
{
$source = $this->sourceOf($folder, $hash);
if ($source === null || is_file("{$this->root}/{$folder}/{$hash}.mp4")) {
return;
}
$job = $this->queue->job($folder, $hash);
if ($job === null || $job['state'] === VideoQueue::FAILED) {
$this->queue->add($folder, $hash, $source, OriginalName::read("{$this->root}/{$folder}", "{$hash}.mp4"));
}
}
/**
* One file as the picker shows it, done or not; null when there is neither file nor job.
*
@@ -115,16 +157,23 @@ final class VideoStore extends HashedFileStore
];
}
$job = $this->jobOf($file);
return $job === null ? null : $this->pending($job);
if ($job !== null) {
return $this->pending($job);
}
$folder = self::folderOf($file);
return $folder === '' ? null : $this->unconverted($folder, basename($file, '.mp4'));
}
/** A video is there for deleting while it is converted or failed, too. */
/** A video is there for deleting while it is converted, failed or only kept, too. */
public function exists(string $file): bool
{
return parent::exists($file) || $this->jobOf($file) !== null;
return $this->record($file) !== null;
}
/** Lists the converted files like every store does, and the ones on their way after them. */
/**
* Lists the converted files like every store does, then the ones on their way, then the ones
* kept without being converted.
*/
public function list(string $folder, bool $seesAll, array $owners): array
{
$items = parent::list($folder, $seesAll, $owners);
@@ -137,6 +186,20 @@ final class VideoStore extends HashedFileStore
];
}
}
foreach (glob("{$this->root}/*/*.source.*") ?: [] as $path) {
$owner = basename(dirname($path));
if (!preg_match('/^([a-f0-9]{64})\.source\.[a-z0-9]+$/', basename($path), $m) || (!$seesAll && $owner !== $folder)) {
continue;
}
$record = $this->unconverted($owner, $m[1]);
if ($record !== null) {
$items[] = $record + [
'updatedAt' => gmdate('c', (int) filemtime($path)),
'owner' => $owners[$owner] ?? '',
'deletable' => true,
];
}
}
return $items;
}
@@ -148,6 +211,10 @@ final class VideoStore extends HashedFileStore
$folder = self::folderOf($file);
$hash = basename($file, '.mp4');
$removed = $folder !== '' && $this->queue->remove($folder, $hash);
$source = $folder === '' ? null : $this->sourceOf($folder, $hash);
if ($source !== null) {
$removed = unlink("{$this->root}/{$source}") || $removed;
}
if (is_file("{$this->root}/{$file}")) {
$removed = parent::delete($file) || $removed;
$dir = $folder === '' ? $this->root : "{$this->root}/{$folder}";
@@ -177,6 +244,7 @@ final class VideoStore extends HashedFileStore
$details = $raw === false ? null : json_decode($raw, true);
$details = is_array($details) ? $details : [];
$file = substr($path, strlen($this->root) + 1);
$folder = self::folderOf($file);
return [
'status' => 'ready',
'width' => (int) ($details['width'] ?? 0),
@@ -185,9 +253,65 @@ final class VideoStore extends HashedFileStore
'fps' => (float) ($details['fps'] ?? 0),
'audio' => (bool) ($details['audio'] ?? false),
'poster' => substr($file, 0, -strlen('.mp4')) . '.jpg',
'original' => $folder === '' ? null : $this->sourceOf($folder, basename($file, '.mp4')),
];
}
/**
* Where the upload of a video is kept, `{folder}/{hash}.source.{ext}` as it is served; null for
* one uploaded before uploads were kept.
*/
private function sourceOf(string $folder, string $hash): ?string
{
foreach (glob("{$this->root}/{$folder}/{$hash}.source.*") ?: [] as $path) {
if (preg_match('/^[a-f0-9]{64}\.source\.[a-z0-9]+$/', basename($path))) {
return "{$folder}/" . basename($path);
}
}
return null;
}
/**
* A kept upload that is neither converted nor on its way, as the picker shows it; null when
* the video is either, or there is no upload.
*
* @return array<string,mixed>|null
*/
private function unconverted(string $folder, string $hash): ?array
{
$source = $this->sourceOf($folder, $hash);
if ($source === null || is_file("{$this->root}/{$folder}/{$hash}.mp4") || $this->queue->job($folder, $hash) !== null) {
return null;
}
return [
'file' => "{$folder}/{$hash}.mp4",
'name' => OriginalName::read("{$this->root}/{$folder}", "{$hash}.mp4"),
'status' => self::UNCONVERTED,
'progress' => 0,
'error' => '',
'bytes' => (int) filesize("{$this->root}/{$source}"),
'original' => $source,
];
}
/**
* The ending a kept upload is served with: the one of its name when that is a video's, else the
* one its first bytes tell.
*/
private static function sourceExtension(string $name, string $head): string
{
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if (in_array($extension, self::SOURCE_EXTENSIONS, true)) {
return $extension;
}
return match (true) {
substr($head, 4, 4) === 'ftyp' => substr($head, 8, 4) === 'qt ' ? 'mov' : 'mp4',
str_starts_with($head, "\x1A\x45\xDF\xA3") => str_contains($head, 'webm') ? 'webm' : 'mkv',
str_starts_with($head, 'RIFF') => 'avi',
default => 'ts',
};
}
/** @return array<string,mixed>|null */
private function jobOf(string $file): ?array
{
@@ -208,6 +332,7 @@ final class VideoStore extends HashedFileStore
'progress' => (float) $job['progress'],
'error' => (string) $job['error'],
'bytes' => 0,
'original' => $this->sourceOf((string) $job['folder'], (string) $job['hash']),
];
}