Skip to content

The swiss army knife of lossless video/audio editing

Feature media: fixes from review

Claudede0d89678ba8
diff --git a/AGENTS.md b/AGENTS.md
index dc2302b7..a714ce41 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -33,6 +33,6 @@ yarn build && yarn download-ffmpeg-linux-x64
xvfb-run -a -s "-screen 0 1280x800x24" node script/featureMedia.ts cut-segments
```
-The clips come from `dist/feature-media-clips`, or are downloaded from `FEATURE_MEDIA_CLIPS_URL`. The results go to `dist/feature-media`. Look at the screenshots to check that the demo did what it should. Never commit clips or results.
+The clips come from `dist/feature-media-clips`, or are downloaded from `FEATURE_MEDIA_CLIPS_URL`. 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 509921fe..6aa68f27 100644
--- a/script/featureMedia.ts
+++ b/script/featureMedia.ts
@@ -3,9 +3,9 @@
// xvfb-run -a -s "-screen 0 1280x800x24" node script/featureMedia.ts <scenario> [--clips dir]
import assert from 'node:assert';
import { createWriteStream } from 'node:fs';
-import { access, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
+import { access, mkdir, mkdtemp, rename, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { join, resolve } from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import timers from 'node:timers/promises';
@@ -36,18 +36,20 @@ for (const clip of scenario.clips) {
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(path));
+ // 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);
}
}
-// A fresh config, so the demo doesn't depend on (or change) your own settings, and no project file is saved next to the clip (it would be loaded next time)
+// A fresh config, so the demo doesn't depend on (or change) your own settings, doesn't hand over to your running LosslessCut, and saves no project file next to the clip (it would be loaded next time)
const configDir = await mkdtemp(join(tmpdir(), 'losslesscut-feature-media-'));
-await writeFile(join(configDir, 'config.json'), JSON.stringify({ windowBounds: { x: 0, y: 0, width, height }, enableUpdateCheck: false, autoSaveProjectFile: false }));
+await writeFile(join(configDir, 'config.json'), JSON.stringify({ windowBounds: { x: 0, y: 0, width, height }, allowMultipleInstances: true, autoSaveProjectFile: false }));
-// The app's own log, to see what went wrong if a step doesn't work
+// The app's log, including the renderer's console. Failed actions don't fail the HTTP request, so check the screenshots and this log
const appLog = join(outDir, `${scenarioName}-app.log`);
// The app path must come first: the app skips the first two arguments when run as `electron <app>` (process.defaultApp)
-const app = execa('node_modules/electron/dist/electron', ['.', '--no-sandbox', '--config-dir', configDir, '--disable-networking', '--http-api', String(port)], { reject: false, stdout: { file: appLog }, stderr: { file: appLog } });
+const app = execa('node_modules/electron/dist/electron', ['.', '--no-sandbox', '--config-dir', configDir, '--disable-networking', '--http-api', String(port)], { reject: false, env: { ELECTRON_ENABLE_LOGGING: '1' }, stdout: { file: appLog }, stderr: { file: appLog } });
async function post(path: string, body?: unknown) {
const response = await fetch(`http://127.0.0.1:${port}/api/${path}`, {
@@ -59,45 +61,45 @@ async function post(path: string, body?: unknown) {
assert(response.ok, `${path} failed: ${response.status} ${await response.text()}`);
}
-const grab = ['-f', 'x11grab', '-draw_mouse', '0', '-video_size', `${width}x${height}`, '-i', `${display}+0,0`];
+const grab = ['-f', 'x11grab', '-draw_mouse', '0', '-framerate', '30', '-video_size', `${width}x${height}`, '-i', `${display}+0,0`];
const rawVideo = join(configDir, 'raw.mkv');
-let recorder: ReturnType<typeof execa> | undefined;
try {
// Wait for the HTTP API
for (let i = 0; ; i += 1) {
if (await fetch(`http://127.0.0.1:${port}/`).then(() => true, () => false)) break;
- assert(i < 60, 'LosslessCut did not start');
+ assert(i < 60 && app.exitCode == null, `LosslessCut did not start, see ${appLog}`);
await timers.setTimeout(500);
}
await timers.setTimeout(3000);
// Lossless while recording, re-encoded below
- recorder = execa(ffmpeg, ['-v', 'error', ...grab, '-framerate', '30', '-c:v', 'libx264', '-preset', 'ultrafast', '-qp', '0', rawVideo], { reject: false });
+ const recorder = execa(ffmpeg, ['-v', 'error', ...grab, '-c:v', 'libx264', '-preset', 'ultrafast', '-qp', '0', rawVideo], { reject: false, stderr: 'inherit' });
await timers.setTimeout(500);
- await scenario.run({
- open: async (clip) => post('action/openFiles', [join(process.cwd(), clips, clip)]),
- action: async (name, args) => {
- console.log('Action', name, args ?? '');
- await post(`action/${name}`, args);
- },
- shot: async (name) => {
- await execa(ffmpeg, ['-v', 'error', '-y', ...grab, '-frames:v', '1', join(outDir, `${scenarioName}-${name}.png`)]);
- },
- wait: async (ms) => timers.setTimeout(ms),
- });
-
- // ffmpeg finishes the file and exits (with code 255) on SIGINT
- recorder.kill('SIGINT');
- await recorder;
+ try {
+ await scenario.run({
+ open: async (clip) => post('action/openFiles', [resolve(clips, clip)]),
+ action: async (name, args) => {
+ console.log('Action', name, args ?? '');
+ await post(`action/${name}`, args);
+ },
+ shot: async (name) => {
+ await execa(ffmpeg, ['-v', 'error', '-y', ...grab, '-frames:v', '1', join(outDir, `${scenarioName}-${name}.png`)]);
+ },
+ wait: async (ms) => timers.setTimeout(ms),
+ });
+ } finally {
+ // ffmpeg finishes the file and exits (with code 255) on SIGINT
+ recorder.kill('SIGINT');
+ }
+ const { exitCode } = await recorder;
+ assert(exitCode === 0 || exitCode === 255, `Recording failed with exit code ${exitCode}`);
// Small file for posting: x264 at a quality that keeps UI text sharp, and the index at the start for streaming
const videoPath = join(outDir, `${scenarioName}.mp4`);
await execa(ffmpeg, ['-v', 'error', '-y', '-i', rawVideo, '-c:v', 'libx264', '-preset', 'slow', '-crf', '24', '-tune', 'animation', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', videoPath]);
console.log('Wrote', videoPath, 'and screenshots in', outDir);
} finally {
- recorder?.kill();
- await post('action/quit').catch(() => undefined);
app.kill();
await app;
await rm(configDir, { recursive: true, force: true });
diff --git a/script/featureMedia/scenarios.ts b/script/featureMedia/scenarios.ts
index 99ea20a9..fa630078 100644
--- a/script/featureMedia/scenarios.ts
+++ b/script/featureMedia/scenarios.ts
@@ -11,9 +11,8 @@ const scenarios: Record<string, Scenario> = {
await open('demo.mp4');
await wait(2000);
await shot('opened');
- // The file starts with one segment; addSegment starts the next one
- for (const [i, [start, end]] of ([['00:00:02', '00:00:05'], ['00:00:09', '00:00:13']] as const).entries()) {
- if (i > 0) await action('addSegment');
+ // Setting a start after the last segment starts a new segment
+ for (const [start, end] of [['00:00:02', '00:00:05'], ['00:00:09', '00:00:13']]) {
await action('goToTimecodeDirect', { time: start });
await action('setCutStart');
await wait(700);