Repository navigation
Expand file tree
/
Copy pathSamples.php
More file actions
506 lines (421 loc) · 15.5 KB
/
Copy pathSamples.php
File metadata and controls
506 lines (421 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
declare(strict_types=1);
namespace Utopia\Tests;
use Utopia\Video\Process;
/**
* The sample library the end-to-end tests run against.
*
* Sources live in tests/samples/in and results are written to tests/samples/out,
* so both are easy to inspect by hand after a run. The media itself is generated
* rather than committed — the repository stays small, and each file's exact
* shape is described here rather than in a binary nobody can read.
*
* Run `composer samples` to populate the directory without running the tests.
*/
final class Samples
{
/** Seconds of content in each generated sample. */
private const DURATION = 6;
/** Keyframe cadence, so any of these can be cut into segments. */
private const KEYFRAME = 'expr:gte(t,n_forced*2)';
/**
* Containers a consumer might realistically be handed, each with the codecs
* that container is normally used with.
*
* @var array<string, list<string>>
*/
private const CONTAINERS = [
// The everyday case.
'mp4' => ['-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-c:a', 'aac'],
// Editors and camera rigs.
'mov' => ['-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-c:a', 'aac'],
// The container that actually supports many tracks well.
'mkv' => ['-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-c:a', 'aac'],
// Older exports, and the reason "just read the extension" fails.
'avi' => ['-c:v', 'mpeg4', '-vtag', 'DX50', '-c:a', 'libmp3lame'],
'wmv' => ['-c:v', 'wmv2', '-c:a', 'wmav2'],
'flv' => ['-c:v', 'flv1', '-c:a', 'libmp3lame', '-ar', '44100'],
// Broadcast and HLS-adjacent.
'ts' => ['-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-c:a', 'aac'],
// Royalty-free web.
'webm' => ['-c:v', 'libvpx-vp9', '-b:v', '300k', '-cpu-used', '8', '-c:a', 'libopus'],
'ogv' => ['-c:v', 'libtheora', '-q:v', '4', '-c:a', 'libvorbis'],
// Phones.
'3gp' => ['-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-ar', '8000'],
];
/**
* Audio tracks for the multi-language samples: language tag, title, and the
* tone that makes each one audibly distinct.
*
* @var list<array{code: string, title: string, hz: int}>
*/
private const LANGUAGES = [
['code' => 'eng', 'title' => 'English', 'hz' => 440],
['code' => 'spa', 'title' => 'Espanol', 'hz' => 554],
['code' => 'fra', 'title' => 'Francais', 'hz' => 659],
['code' => 'jpn', 'title' => 'Japanese', 'hz' => 880],
];
public static function root(): string
{
return \dirname(__DIR__, 2).'/tests/samples';
}
public static function in(): string
{
return self::root().'/in';
}
public static function out(): string
{
return self::root().'/out';
}
/**
* Whether the tools needed to build and use the samples are installed.
*/
public static function available(): bool
{
return Process::exists('ffmpeg') && Process::exists('ffprobe');
}
/**
* Every sample source, keyed by filename.
*
* @return array<string, string>
*/
public static function all(): array
{
$found = [];
foreach (\glob(self::in().'/*') ?: [] as $path) {
if (\is_file($path)) {
$found[\basename($path)] = $path;
}
}
\ksort($found);
return $found;
}
/**
* Samples whose name starts with the given prefix.
*
* @return array<string, string>
*/
public static function matching(string $prefix): array
{
return \array_filter(
self::all(),
static fn (string $name): bool => \str_starts_with($name, $prefix),
ARRAY_FILTER_USE_KEY,
);
}
/**
* Builds anything missing. Safe to call repeatedly — existing files are left
* alone, so a full run costs nothing after the first.
*/
public static function build(bool $force = false): void
{
$in = self::in();
if (! \is_dir($in)) {
\mkdir($in, 0o755, true);
}
if (! \is_dir(self::out())) {
\mkdir(self::out(), 0o755, true);
}
if ($force) {
foreach (\glob($in.'/*') ?: [] as $path) {
\unlink($path);
}
}
$master = self::master();
self::containers($master);
self::multitrack($master);
self::shapes($master);
self::sound($master);
}
/**
* The source everything else is derived from: moving detail that compresses
* like real footage, plus a tone.
*/
private static function master(): string
{
$path = self::in().'/video.mp4';
if (\is_file($path)) {
return $path;
}
self::run([
'-f', 'lavfi', '-i', 'testsrc2=duration='.self::DURATION.':size=640x360:rate=30',
'-f', 'lavfi', '-i', 'sine=frequency=440:duration='.self::DURATION,
'-c:v', 'libx264', '-preset', 'ultrafast', '-pix_fmt', 'yuv420p',
'-force_key_frames', self::KEYFRAME,
'-c:a', 'aac', '-shortest',
'-metadata', 'title=Utopia Video Sample',
'-metadata', 'artist=Utopia',
], $path);
return $path;
}
/**
* Encoders this ffmpeg was actually built with.
*
* Builds differ — a distribution package and a hand-rolled one rarely carry
* the same set — so the sample library adapts instead of failing on whichever
* codec happens to be missing.
*
* @return list<string>
*/
private static function encoders(): array
{
/** @var list<string>|null $found */
static $found = null;
if ($found !== null) {
return $found;
}
$found = [];
foreach (\explode("\n", Process::read(['ffmpeg', '-hide_banner', '-encoders'], 30)) as $line) {
if (\preg_match('/^\s*[A-Z.]{6}\s+(\S+)/', $line, $match) === 1) {
$found[] = $match[1];
}
}
return $found;
}
/**
* @param list<string> $codecs
*/
private static function buildable(array $codecs): bool
{
$available = self::encoders();
foreach ($codecs as $index => $value) {
// Only inspect the values that follow a -c:v or -c:a flag.
$flag = $codecs[$index - 1] ?? '';
if (($flag === '-c:v' || $flag === '-c:a') && ! \in_array($value, $available, true)) {
return false;
}
}
return true;
}
/**
* The same content in every container worth supporting, so nothing depends
* on a file extension.
*/
private static function containers(string $master): void
{
foreach (self::CONTAINERS as $extension => $codecs) {
$path = self::in().'/video.'.$extension;
if ($extension === 'mp4' || \is_file($path)) {
continue;
}
if (! self::buildable($codecs)) {
// This ffmpeg cannot write that combination; the rest still build.
continue;
}
$args = ['-i', $master, ...$codecs];
// Not every codec honours forced keyframes, so only ask where it helps.
if (\in_array('libx264', $codecs, true) || \in_array('libvpx-vp9', $codecs, true)) {
$args[] = '-force_key_frames';
$args[] = self::KEYFRAME;
}
if ($extension === '3gp') {
// 3GP expects a small frame and a low sample rate.
$args[] = '-vf';
$args[] = 'scale=352:288';
}
self::run($args, $path);
}
}
/**
* Several audio tracks, each tagged with a language, in the two containers
* where that is common: MP4 and Matroska. Matroska also carries subtitles in
* more than one language.
*/
private static function multitrack(string $master): void
{
foreach (['mp4' => 3, 'mkv' => 4] as $extension => $count) {
$path = self::in().'/multi-audio.'.$extension;
if (\is_file($path)) {
continue;
}
$args = ['-i', $master];
$languages = \array_slice(self::LANGUAGES, 0, $count);
foreach ($languages as $language) {
$args[] = '-f';
$args[] = 'lavfi';
$args[] = '-i';
$args[] = 'sine=frequency='.$language['hz'].':duration='.self::DURATION;
}
$args[] = '-map';
$args[] = '0:v:0';
foreach (\array_keys($languages) as $index) {
$args[] = '-map';
$args[] = ($index + 1).':a:0';
}
foreach ($languages as $index => $language) {
$args[] = '-metadata:s:a:'.$index;
$args[] = 'language='.$language['code'];
$args[] = '-metadata:s:a:'.$index;
$args[] = 'title='.$language['title'];
}
$args[] = '-c:v';
$args[] = 'copy';
$args[] = '-c:a';
$args[] = 'aac';
$args[] = '-shortest';
self::run($args, $path);
}
// The realistic case: a release with several dubs and several subtitle
// languages in the one file.
$both = self::in().'/multi-track.mkv';
if (! \is_file($both) && \is_file(self::in().'/multi-audio.mkv')) {
$tracks = ['eng' => "1\n00:00:01,000 --> 00:00:03,000\nHello\n", 'fra' => "1\n00:00:01,000 --> 00:00:03,000\nBonjour\n"];
$args = ['-i', self::in().'/multi-audio.mkv'];
$written = [];
foreach ($tracks as $code => $body) {
$srt = self::in().'/.both-'.$code.'.srt';
\file_put_contents($srt, $body);
$written[] = $srt;
$args[] = '-i';
$args[] = $srt;
}
$args[] = '-map';
$args[] = '0';
foreach (\array_keys($tracks) as $index => $code) {
$args[] = '-map';
$args[] = ($index + 1).':0';
}
foreach (\array_keys($tracks) as $index => $code) {
$args[] = '-metadata:s:s:'.$index;
$args[] = 'language='.$code;
}
$args[] = '-c';
$args[] = 'copy';
$args[] = '-c:s';
$args[] = 'srt';
self::run($args, $both);
foreach ($written as $srt) {
@\unlink($srt);
}
}
// Subtitles in three languages, which is what Matroska is usually used for.
$subtitled = self::in().'/multi-subtitle.mkv';
if (! \is_file($subtitled)) {
$tracks = [
'eng' => "1\n00:00:01,000 --> 00:00:03,000\nHello\n",
'fra' => "1\n00:00:01,000 --> 00:00:03,000\nBonjour\n",
'jpn' => "1\n00:00:01,000 --> 00:00:03,000\nこんにちは\n",
];
$args = ['-i', $master];
$written = [];
foreach ($tracks as $code => $body) {
$srt = self::in().'/.'.$code.'.srt';
\file_put_contents($srt, $body);
$written[] = $srt;
$args[] = '-i';
$args[] = $srt;
}
$args[] = '-map';
$args[] = '0';
foreach (\array_keys($tracks) as $index => $code) {
$args[] = '-map';
$args[] = ($index + 1).':0';
}
foreach (\array_keys($tracks) as $index => $code) {
$args[] = '-metadata:s:s:'.$index;
$args[] = 'language='.$code;
}
$args[] = '-c:v';
$args[] = 'copy';
$args[] = '-c:a';
$args[] = 'copy';
$args[] = '-c:s';
$args[] = 'srt';
self::run($args, $subtitled);
foreach ($written as $srt) {
@\unlink($srt);
}
}
}
/**
* The properties that make real files awkward.
*/
private static function shapes(string $master): void
{
$in = self::in();
// Non-square pixels: stored 4:3, displayed wider.
self::once($in.'/anamorphic.mp4', [
'-i', $master, '-vf', 'scale=640:480,setsar=4:3',
'-c:v', 'libx264', '-preset', 'ultrafast', '-force_key_frames', self::KEYFRAME, '-c:a', 'copy',
]);
// Frames arriving at an uneven rate.
self::once($in.'/variable-fps.mp4', [
'-i', $master, '-vf', "select='not(mod(n,3))+gte(t,3)'", '-fps_mode', 'vfr',
'-c:v', 'libx264', '-preset', 'ultrafast', '-an',
]);
// Orientation in the display matrix rather than the frame size.
self::once($in.'/rotated.mp4', [
'-display_rotation', '90', '-i', $master, '-c', 'copy',
]);
// Shot on a phone, held upright.
self::once($in.'/portrait.mp4', [
'-i', $master, '-vf', 'scale=360:640,setsar=1:1',
'-c:v', 'libx264', '-preset', 'ultrafast', '-force_key_frames', self::KEYFRAME, '-c:a', 'copy',
]);
// Newer codec, same content.
self::once($in.'/hevc.mp4', [
'-i', $master, '-c:v', 'libx265', '-preset', 'ultrafast', '-tag:v', 'hvc1',
'-force_key_frames', self::KEYFRAME, '-c:a', 'copy',
]);
// No sound at all.
self::once($in.'/silent.mp4', ['-i', $master, '-an', '-c:v', 'copy']);
// Shorter than a single segment.
self::once($in.'/tiny.mp4', ['-i', $master, '-t', '0.5', '-c', 'copy']);
// Chapters, which players show as a timeline.
$meta = $in.'/.chapters.txt';
\file_put_contents($meta, <<<'META'
;FFMETADATA1
title=Chaptered Sample
[CHAPTER]
TIMEBASE=1/1000
START=0
END=3000
title=Opening
[CHAPTER]
TIMEBASE=1/1000
START=3000
END=6000
title=Closing
META);
self::once($in.'/chapters.mp4', ['-i', $master, '-i', $meta, '-map_metadata', '1', '-map', '0', '-c', 'copy']);
@\unlink($meta);
}
/**
* Audio-only sources, with and without artwork.
*/
private static function sound(string $master): void
{
$in = self::in();
self::once($in.'/audio.m4a', ['-i', $master, '-vn', '-c:a', 'aac']);
self::once($in.'/audio.mp3', ['-i', $master, '-vn', '-c:a', 'libmp3lame']);
$cover = $in.'/.cover.jpg';
if (! \is_file($in.'/artwork.m4a')) {
self::run(['-i', $master, '-frames:v', '1', '-vf', 'scale=300:300'], $cover);
self::run([
'-i', $in.'/audio.m4a', '-i', $cover,
'-map', '0:a', '-map', '1:v', '-c', 'copy',
'-disposition:v:0', 'attached_pic',
], $in.'/artwork.m4a');
@\unlink($cover);
}
}
/**
* @param list<string> $args
*/
private static function once(string $path, array $args): void
{
if (! \is_file($path)) {
self::run($args, $path);
}
}
/**
* @param list<string> $args
*/
private static function run(array $args, string $target): void
{
Process::run(
[...['ffmpeg', '-y', '-hide_banner', '-loglevel', 'error'], ...$args, $target],
timeout: 300,
);
}
}