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>
217 lines
8.3 KiB
PHP
217 lines
8.3 KiB
PHP
<?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 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 = 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. */
|
||
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];
|
||
}
|
||
}
|