launch-film 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/check.js CHANGED
@@ -16,10 +16,12 @@
16
16
  const fs = require("fs");
17
17
  const path = require("path");
18
18
  const { spawnSync } = require("child_process");
19
- const { FFMPEG, run, probe, writePng } = require("./ffmpeg");
19
+ const { FFMPEG, input, why, stoppedSync, run, probe, writePng } = require("./ffmpeg");
20
20
  const frames = require("./frames");
21
21
  const { readCues } = require("./cues");
22
22
  const draw = require("./draw");
23
+ const { within, save } = require("./out");
24
+ const { filmListed } = require("./confine");
23
25
 
24
26
  const RULES = {
25
27
  // What a film may hold at its start and its end (a fade in, an end card).
@@ -47,6 +49,8 @@ function parseExpect(text) {
47
49
  if (!text) return {};
48
50
  const m = /^(?:(\d+)\s*[x\u00d7]\s*(\d+))?\s*(?:@\s*(\d+(?:\.\d+)?))?$/i.exec(String(text).trim());
49
51
  if (!m || (!m[1] && !m[3])) throw new Error(`--expect "${text}" should look like 1920x1080@30`);
52
+ // A size or a rate of 0 was taken for none given, and passed.
53
+ if ([m[1], m[2], m[3]].some((v) => v !== undefined && !(Number(v) > 0))) throw new Error(`--expect "${text}": the size and the frame rate must be above 0, as in 1920x1080@30`);
50
54
  return { width: m[1] ? Number(m[1]) : null, height: m[2] ? Number(m[2]) : null, fps: m[3] ? Number(m[3]) : null };
51
55
  }
52
56
 
@@ -69,27 +73,37 @@ function parseAllow(text) {
69
73
  });
70
74
  }
71
75
 
72
- // The sound, measured: ebur128's loudness every 100 ms and its summary
73
- // (integrated loudness, range, sample and true peak), and silencedetect's
74
- // stretches.
76
+ // The sound, measured: astats reads its peak and how flat it stays there,
77
+ // from the sound as it is in the film; ebur128 its loudness every 100 ms and
78
+ // its summary (integrated loudness, range and true peak), and silencedetect
79
+ // its stretches, at 48 kHz. ffmpeg 4.4's ebur128 takes nothing else, so at
80
+ // another rate ffmpeg would put a resampler in front of it, and what came
81
+ // after it would no longer see the flat tops of clipped sound; newer builds
82
+ // measure at the film's own rate. Resampled here on every build, a film has
83
+ // the same numbers on all of them. framelog=verbose keeps ebur128's line for
84
+ // every 100 ms below the info level its summary is read at, and every ffmpeg
85
+ // from 4.4 on takes it.
75
86
  async function scanAudio(film, { work, duration }) {
76
87
  const notes = path.join(work, `.scan-audio-${process.pid}.txt`);
77
88
  try {
78
- const r = await run(["-i", film, "-vn", "-af",
79
- `ebur128=metadata=1:peak=true+sample:framelog=quiet,silencedetect=n=-50dB:d=0.5,astats=measure_overall=Peak_count+Flat_factor:measure_perchannel=none,ametadata=mode=print:file=${frames.filterPath(notes)}`,
89
+ const r = await run([...input(film), "-vn", "-af",
90
+ `astats=measure_overall=Peak_level+Peak_count+Flat_factor:measure_perchannel=none,aresample=48000,ebur128=metadata=1:peak=true:framelog=verbose,silencedetect=n=-50dB:d=0.5,ametadata=mode=print:file=${frames.filterPath(notes)}`,
80
91
  "-f", "null", "-"], { loglevel: "info" });
81
- if (r.code !== 0) throw new Error(`reading the sound: ffmpeg failed: ${r.stderr.trim().split("\n").slice(-2).join(" | ")}`);
92
+ if (r.short) throw Object.assign(new Error(r.short), { short: true });
93
+ if (r.code !== 0) throw new Error(`reading the sound: ${why(r, { lines: 2 })}`);
82
94
  const num = (re) => {
83
95
  const m = re.exec(r.stderr);
84
96
  return m ? (m[1] === "-inf" ? -Infinity : Number(m[1])) : null;
85
97
  };
98
+ const peak = num(/Peak level dB:\s*(-?[\d.]+|-inf)/);
86
99
  const summary = {
87
100
  integrated: num(/Integrated loudness:\s*\n\s*I:\s*(-?[\d.]+|-inf) LUFS/),
88
101
  range: num(/Loudness range:\s*\n\s*LRA:\s*(-?[\d.]+) LU/),
89
- samplePeak: num(/Sample peak:\s*\n\s*Peak:\s*(-?[\d.]+|-inf) dBFS/),
102
+ // To a tenth of a dB, as the other levels are given.
103
+ samplePeak: peak === null ? null : +peak.toFixed(1),
90
104
  truePeak: num(/True peak:\s*\n\s*Peak:\s*(-?[\d.]+|-inf) dBFS/),
91
- // astats: how often the sound reached its peak, and how flat it stayed
92
- // there. Clipped sound sits on its peak, flat, many times over.
105
+ // How often the sound reached its peak, and how flat it stayed there.
106
+ // Clipped sound sits on its peak, flat, many times over.
93
107
  peakCount: num(/Peak count:\s*([\d.]+)/),
94
108
  flat: num(/Flat factor:\s*(-?[\d.]+)/),
95
109
  };
@@ -125,7 +139,9 @@ async function scanAudio(film, { work, duration }) {
125
139
  // The sound's outline: the loudest sample in each of `columns` slices, in
126
140
  // dBFS, from a mono copy at 4 kHz.
127
141
  function envelope(film, { duration, columns }) {
128
- const r = spawnSync(FFMPEG, ["-v", "error", "-i", film, "-vn", "-ac", "1", "-ar", "4000", "-f", "f32le", "-"], { maxBuffer: Math.ceil(duration * 4000 * 4) + 1e6, timeout: 120_000 });
142
+ const r = spawnSync(FFMPEG, ["-v", "error", ...input(film), "-vn", "-ac", "1", "-ar", "4000", "-f", "f32le", "-"], { maxBuffer: Math.ceil(duration * 4000 * 4) + 1e6, timeout: 120_000 });
143
+ const stopped = stoppedSync(r, "ffmpeg", "reading the sound's outline");
144
+ if (stopped) throw stopped;
129
145
  if (r.status !== 0 || !r.stdout) return null;
130
146
  const n = Math.floor(r.stdout.length / 4);
131
147
  const peaks = new Array(columns).fill(0);
@@ -173,25 +189,38 @@ function nearest(list, t, fps) {
173
189
  // Checks a film. opts: { out, expect, seconds, lufs, cues, cuts, allow }
174
190
  // (the command's options as given). Returns { pass, checks, dir, files }.
175
191
  async function checkFilm(file, opts = {}) {
176
- const film = path.resolve(file);
177
- if (!fs.existsSync(film)) throw new Error(`${file} is not a file`);
178
- const info = probe(film);
179
- if (!info.duration || !info.width) throw new Error(`${file} could not be read as a video`);
192
+ // What the check is asked for is read first: a value it cannot use is
193
+ // refused before the film is.
180
194
  const expect = parseExpect(opts.expect);
181
195
  const seconds = parseSeconds(opts.seconds);
182
196
  const target = opts.lufs === undefined || opts.lufs === null || opts.lufs === "" ? null : Number(opts.lufs);
183
197
  if (target !== null && !(target < 0 && target > -70)) throw new Error(`--lufs "${opts.lufs}" should be a loudness like -14`);
184
198
  const allow = parseAllow(opts.allow);
199
+ const film = path.resolve(file);
200
+ if (!fs.existsSync(film)) throw new Error(`${file} is not a file`);
201
+ // With LAUNCH_FILM_CONFINE set, a film that is a list of other files is
202
+ // refused before ffprobe, which would open them, reads it (confine.js).
203
+ const listed = filmListed(film, file);
204
+ if (listed) throw new Error(listed);
205
+ const info = probe(film);
206
+ // ffprobe's own words when it would not read the film, as a guard that
207
+ // opens no name with a % in it, or a film the user may not read.
208
+ if (!info.duration || !info.width) throw new Error(`${file} could not be read as a video${info.failed ? ` (${info.failed})` : ""}`);
185
209
  const cues = opts.cues ? readCues(path.resolve(opts.cues)) : null;
186
210
  const dir = path.resolve(opts.out || path.join(path.dirname(film), "check"));
187
- fs.mkdirSync(dir, { recursive: true });
211
+ // The report and its pictures take the place of an earlier check's once
212
+ // all four are made (out.js).
213
+ return within(dir, (job) => measure(film, { info, expect, seconds, target, allow, cues, cutsGiven: opts.cuts || null, dir, job }));
214
+ }
215
+
216
+ async function measure(film, { info, expect, seconds, target, allow, cues, cutsGiven, dir, job }) {
188
217
  const duration = info.duration;
189
218
  const fps = info.fps || 25;
190
219
  const frame = 1.5 / fps;
191
220
 
192
- const scan = await frames.scanVideo(film, { work: dir, duration });
193
- const sound = info.audio ? await scanAudio(film, { work: dir, duration }) : null;
194
- const { cuts, source } = await frames.findCuts(film, { info, work: dir, scan, given: opts.cuts || null });
221
+ const scan = await frames.scanVideo(film, { work: job.part, duration });
222
+ const sound = info.audio ? await scanAudio(film, { work: job.part, duration }) : null;
223
+ const { cuts, source } = await frames.findCuts(film, { info, work: job.part, scan, given: cutsGiven });
195
224
 
196
225
  const checks = [];
197
226
  const add = (name, status, expected, found, extra = {}) => checks.push({ name, status, expected, found, ...extra });
@@ -267,10 +296,12 @@ async function checkFilm(file, opts = {}) {
267
296
  const warned = checks.filter((c) => c.status === "warn");
268
297
  const pass = !failed.length;
269
298
 
270
- // The pictures: a frame for each cut, and the sound over time.
271
- const shots = frames.shotsOf(cuts, duration);
299
+ // The pictures: a frame for each cut, from the film's picture, and the
300
+ // sound over time.
301
+ const span = frames.stillSpan(info);
302
+ const shots = frames.shotsOf(cuts, span.end);
272
303
  const temp = shots.map((s) => ({ at: frames.settled(s), name: `.cut-${process.pid}-${s.index}`, label: s.label }));
273
- const stills = await frames.grabFrames(film, temp, dir, { duration });
304
+ const stills = await frames.grabFrames(film, temp, job.part, { last: span.last });
274
305
  stills.forEach((s, i) => {
275
306
  const c = cuts[i];
276
307
  s.number = String(c.index);
@@ -278,14 +309,13 @@ async function checkFilm(file, opts = {}) {
278
309
  s.label = [c.label, c.verdict ? { on: "ON BEAT", near: `NEAR BEAT ${c.off > 0 ? "+" : ""}${c.off}F`, off: `OFF BEAT ${c.off > 0 ? "+" : ""}${c.off}F` }[c.verdict] : ""].filter(Boolean).join(" ");
279
310
  s.labelColour = c.verdict === "off" ? "#F87171" : c.verdict === "near" ? "#FACC15" : c.verdict === "on" ? "#4ADE80" : "#7D8494";
280
311
  });
281
- const sheet = path.join(dir, "sheet.png");
312
+ const sheet = job.out("sheet.png");
282
313
  try {
283
314
  await frames.contactSheet(stills, sheet, { title: `${path.basename(film)} ${length} S ${size} ${shownFps} FPS ${cuts.length} CUT${cuts.length > 1 ? "S" : ""} (${source.toUpperCase()})` });
284
315
  } finally {
285
316
  for (const s of stills) fs.rmSync(s.file, { force: true });
286
317
  }
287
- const wave = path.join(dir, "wave.png");
288
- await drawWave(wave, { film, duration, sound, target, cuts, cues, fps });
318
+ await drawWave(job.out("wave.png"), { film, duration, sound, target, cuts, cues, fps });
289
319
 
290
320
  const result = {
291
321
  file: path.relative(dir, film),
@@ -301,8 +331,8 @@ async function checkFilm(file, opts = {}) {
301
331
  rules: RULES,
302
332
  files: { report: "CHECK.md", sheet: "sheet.png", wave: "wave.png" },
303
333
  };
304
- fs.writeFileSync(path.join(dir, "check.json"), JSON.stringify(result, null, 2) + "\n");
305
- fs.writeFileSync(path.join(dir, "CHECK.md"), report(result, { film, source }));
334
+ save(job.out("check.json"), JSON.stringify(result, null, 2) + "\n");
335
+ save(job.out("CHECK.md"), report(result, { film, source }));
306
336
  return { ...result, dir };
307
337
  }
308
338