Skip to content

The swiss army knife of lossless video/audio editing

Feature media: make tears-of-steel.mkv from its source with a stream copy

Claudebbc6b58c88f2
diff --git a/AGENTS.md b/AGENTS.md
index b1864fae..a0fea989 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -34,6 +34,6 @@ yarn build && yarn download-ffmpeg-linux-x64
xvfb-run -a -s "-screen 0 1024x640x24" node script/featureMedia.ts cut-segments
```
-The clips come from `dist/feature-media-clips`, or are downloaded from https://static.mifi.no/losslesscut (or `FEATURE_MEDIA_CLIPS_URL`; behind a proxy, e.g. in a cloud session, set `NODE_USE_ENV_PROXY=1`). Scenarios can use only the clips there; ask the user to upload new ones. `tears-of-steel.mkv` is a 45 s excerpt (00:20–01:05) of [Tears of Steel](https://mango.blender.org/) with its stereo and 5.1 mixes and 6 subtitle tracks. It's CC BY 3.0, so posts that show it must credit "Tears of Steel (CC) Blender Foundation | mango.blender.org". The results go to `dist/feature-media`. Failed actions don't fail the script, so look at the screenshots (and `<scenario>-app.log`) to check that the demo did what it should. Never commit clips or results.
+The clips come from `dist/feature-media-clips`. Missing ones are made from free sources with an ffmpeg stream copy (`script/featureMedia/clips.ts`, which only downloads the part that's needed) or downloaded from https://static.mifi.no/losslesscut (or `FEATURE_MEDIA_CLIPS_URL`; behind a proxy, e.g. in a cloud session, set `NODE_USE_ENV_PROXY=1`). For a new clip, prefer a free source in `clips.ts` (its host must be allowed by the network policy), or ask the user to upload it. `tears-of-steel.mkv` is an excerpt of [Tears of Steel](https://mango.blender.org/), which is CC BY 3.0, so posts that show it must credit "Tears of Steel (CC) Blender Foundation | mango.blender.org". The results go to `dist/feature-media`. Failed actions don't fail the script, so look at the screenshots (and `<scenario>-app.log`) to check that the demo did what it should. Never commit clips or results.
When a user-visible feature lands on master, suggest adding a scenario for it and recording it.
diff --git a/script/featureMedia.ts b/script/featureMedia.ts
index 4512754d..2bc3a691 100644
--- a/script/featureMedia.ts
+++ b/script/featureMedia.ts
@@ -11,6 +11,7 @@ import { pipeline } from 'node:stream/promises';
import timers from 'node:timers/promises';
import { parseArgs } from 'node:util';
import { execa } from 'execa';
+import clipSources from './featureMedia/clips.ts';
import scenarios from './featureMedia/scenarios.ts';
const { values: { clips = 'dist/feature-media-clips' }, positionals: [scenarioName] } = parseArgs({ allowPositionals: true, options: { clips: { type: 'string' } } });
@@ -30,17 +31,26 @@ const outDir = 'dist/feature-media';
await mkdir(outDir, { recursive: true });
await mkdir(clips, { recursive: true });
-// Get the clips, from the folder or else download them
+// Get the clips, from the folder or else make them from their sources (see clips.ts) or download them
const clipsUrl = process.env['FEATURE_MEDIA_CLIPS_URL'] ?? 'https://static.mifi.no/losslesscut';
+// ffmpeg only uses the lowercase variable
+const httpProxy = process.env['http_proxy'] ?? process.env['HTTPS_PROXY'];
for (const clip of scenario.clips) {
const path = join(clips, clip);
if (!await access(path).then(() => true, () => false)) {
- console.log('Downloading', clip);
- const response = await fetch(`${clipsUrl}/${clip}`);
- assert(response.ok && response.body != null, `Failed to download ${clip}: ${response.status}`);
// Via a temp file, so an interrupted download isn't used next time
- await pipeline(Readable.fromWeb(response.body), createWriteStream(`${path}.part`));
- await rename(`${path}.part`, path);
+ const partPath = `${path}.part`;
+ const sourceArgs = clipSources[clip];
+ if (sourceArgs != null) {
+ console.log('Making', clip, 'from its sources');
+ await execa(ffmpeg, ['-v', 'error', '-y', ...sourceArgs, partPath], { ...(httpProxy != null && { env: { http_proxy: httpProxy } }) });
+ } else {
+ console.log('Downloading', clip);
+ const response = await fetch(`${clipsUrl}/${clip}`);
+ assert(response.ok && response.body != null, `Failed to download ${clip}: ${response.status}`);
+ await pipeline(Readable.fromWeb(response.body), createWriteStream(partPath));
+ }
+ await rename(partPath, path);
}
}
diff --git a/script/featureMedia/clips.ts b/script/featureMedia/clips.ts
new file mode 100644
index 00000000..4996c3ea
--- /dev/null
+++ b/script/featureMedia/clips.ts
@@ -0,0 +1,24 @@
+// Clips that script/featureMedia.ts makes from their free sources instead of downloading them from static.mifi.no.
+// Each is the ffmpeg arguments (before the output path) for a lossless stream copy, which only downloads the part that's needed.
+
+// An excerpt (00:20–01:05) of Tears of Steel, CC BY 3.0, (CC) Blender Foundation | mango.blender.org
+// with its stereo and 5.1 mixes and 6 subtitle tracks. The start is a keyframe, so the stream copy starts cleanly
+const tearsOfSteel = 'https://download.blender.org/demo/movies/ToS';
+const tearsOfSteelStart = '19.833333';
+const tearsOfSteelSubtitles = [['en', 'eng', 'English'], ['de', 'ger', 'Deutsch'], ['es', 'spa', 'Español'], ['fr-orig', 'fre', 'Français'], ['no', 'nor', 'Norsk'], ['JP', 'jpn', '日本語']] as const;
+
+const clipSources: Record<string, string[]> = {
+ 'tears-of-steel.mkv': [
+ ...[`${tearsOfSteel}/tears_of_steel_720p.mov`, `${tearsOfSteel}/Surround-TOS_DVDSURROUND-Dolby%205.1.ac3`, ...tearsOfSteelSubtitles.map(([file]) => `${tearsOfSteel}/subtitles/TOS-${file}.srt`)]
+ .flatMap((url) => ['-ss', tearsOfSteelStart, '-i', url]),
+ '-t', '45',
+ '-map', '0:v', '-map', '0:a', '-map', '1:a', ...tearsOfSteelSubtitles.flatMap((_, i) => ['-map', `${i + 2}:s`]),
+ '-c', 'copy', '-c:s', 'srt',
+ '-metadata:s:a:0', 'title=Stereo', '-metadata:s:a:1', 'language=eng', '-metadata:s:a:1', 'title=5.1 surround',
+ ...tearsOfSteelSubtitles.flatMap(([, language, title], i) => [`-metadata:s:s:${i}`, `language=${language}`, `-metadata:s:s:${i}`, `title=${title}`]),
+ '-disposition:s:0', 'default',
+ '-f', 'matroska',
+ ],
+};
+
+export default clipSources;
diff --git a/script/featureMedia/scenarios.ts b/script/featureMedia/scenarios.ts
index 74048f35..cfe41603 100644
--- a/script/featureMedia/scenarios.ts
+++ b/script/featureMedia/scenarios.ts
@@ -111,18 +111,18 @@ const scenarios: Record<string, Scenario> = {
await wait(1500);
await shot('dialog');
// Edit the title
- await click(743, 258);
+ await click(743, 277);
await wait(800);
await key('ctrl+a');
await type('English stereo mix');
await wait(500);
await key('Return');
await wait(1000);
- // Delete the ENCODER tag
- await click(791, 296);
+ // Delete the HANDLER_NAME tag
+ await click(791, 315);
await wait(1500);
await shot('edited');
- await click(761, 547);
+ await click(761, 528);
await wait(1500);
await shot('tracks');
},