@senseihq/emaki-cli 0.1.1 → 0.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senseihq/emaki-cli",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "The motion stories command line: set a project up, install the Claude plugin (skills, agents, playbook), run the production steps (validate, sheet, voice, fit, render, build) per film and locale, then stage, publish and run a campaign.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,8 +28,8 @@
28
28
  "postpublish": "rm -f LICENSE"
29
29
  },
30
30
  "dependencies": {
31
- "@senseihq/emaki": "0.1.1",
32
- "@senseihq/emaki-posting": "0.1.1",
31
+ "@senseihq/emaki": "0.1.2",
32
+ "@senseihq/emaki-posting": "0.1.2",
33
33
  "sade": "^1.8.1"
34
34
  },
35
35
  "peerDependencies": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emaki",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Build a multilingual site of motion stories (articles, story player and narrated videos from one set of JSON scenes) through a guided, gated program: interview, outline, item cards, pilot, automated batches, review loop.",
5
5
  "author": {
6
6
  "name": "SenecaGlobal"
@@ -0,0 +1,166 @@
1
+ // What build decides, as pure functions (build.py's): the freeze table, the ffmpeg arguments for the final videos,
2
+ // chapters, the description, the posters. build-video.js runs what these return; media.js re-exports them.
3
+ import { join } from 'node:path'
4
+ import { runningTime } from './util.js'
5
+
6
+ /** build.py's plan: each scene's place on the rendered picture (start, dur: film.json plus timing.<locale>.json) and on
7
+ * the output (at, tot). A take is laid `lead` s into its scene; when lead + take + tail is longer than the scene, the
8
+ * scene's last frame is frozen for the difference (ext), so nothing is cut off or sped up. `takes`: scene id →
9
+ * { file, seconds }. After `emaki fit` and a fresh render no scene needs a freeze. */
10
+ export function buildPlan(film, timing, takes, { lead, tail }) {
11
+ const rows = []
12
+ let start = 0, at = 0
13
+ for (const [i, s] of film.scenes.entries()) {
14
+ const dur = runningTime(s, timing?.[s.id])
15
+ const take = takes.get(s.id) ?? null
16
+ const L = take ? take.seconds : 0
17
+ let ext = take ? Math.max(0, lead + L + tail - dur) : 0
18
+ if (ext < 1e-3) ext = 0
19
+ rows.push({ n: i + 1, id: s.id, name: s.name ?? s.id, start, dur, take: take?.file ?? null, L, ext, tot: dur + ext, at })
20
+ start += dur
21
+ at += dur + ext
22
+ }
23
+ return { rows, picture: start, total: at, extended: rows.some((r) => r.ext > 0), missing: rows.filter((r) => !r.take).map((r) => r.id) }
24
+ }
25
+
26
+ const mmss = (t) => `${String(Math.floor(t / 60)).padStart(2, '0')}:${String(Math.floor(t % 60)).padStart(2, '0')}`
27
+
28
+ /** The table build --check prints (build.py's): each scene's length, take, freeze and new length, and where it starts. */
29
+ export function planTable(plan) {
30
+ const f = (x, w) => x.toFixed(1).padStart(w)
31
+ const lines = [`${'#'.padStart(2)} ${'scene'.padEnd(12)} ${'dur'.padStart(5)} ${'take'.padStart(5)} ${'ext'.padStart(5)} ${'new'.padStart(6)} start`]
32
+ for (const r of plan.rows)
33
+ lines.push(`${String(r.n).padStart(2)} ${r.id.padEnd(12)} ${f(r.dur, 5)} ${f(r.L, 5)} ${f(r.ext, 5)} ${f(r.tot, 6)} ${mmss(r.at)}${r.take ? '' : ' (no take)'}`)
34
+ const k = plan.rows.filter((r) => r.ext > 0).length
35
+ lines.push(`total ${plan.total.toFixed(1)} s` + (k ? ` (${k} scene${k > 1 ? 's' : ''} frozen to fit ${k > 1 ? 'their takes' : 'its take'}; \`emaki fit\` and a new render make that unnecessary)` : ''))
36
+ return lines
37
+ }
38
+
39
+ const escMeta = (s) => String(s).replace(/[\\=;#]/g, (c) => `\\${c}`).replace(/\n/g, ' ')
40
+
41
+ /** FFMETADATA with one chapter per scene, on the exact scene boundaries (VLC, QuickTime; YouTube ignores it). */
42
+ export function ffmeta(rows) {
43
+ const lines = [';FFMETADATA1']
44
+ for (const r of rows)
45
+ lines.push('[CHAPTER]', 'TIMEBASE=1/1000', `START=${Math.round(r.at * 1000)}`, `END=${Math.round((r.at + r.tot) * 1000)}`, `title=${escMeta(r.name)}`)
46
+ return lines.join('\n') + '\n'
47
+ }
48
+
49
+ /** YouTube's chapter list: [at, title] from 0:00, every chapter at least 10 s (YouTube shows none unless all its rules
50
+ * hold). Short scenes fold into the chapter they start; a short tail joins the last one. Boundaries stay on cuts. */
51
+ export function ytChapters(rows) {
52
+ const groups = []
53
+ let cur = [], start = 0
54
+ for (const r of rows) {
55
+ cur.push(r)
56
+ if (r.at + r.tot - start >= 10 - 1e-6) {
57
+ groups.push([start, cur])
58
+ cur = []
59
+ start = r.at + r.tot
60
+ }
61
+ }
62
+ if (cur.length) {
63
+ if (groups.length) groups.at(-1)[1].push(...cur)
64
+ else groups.push([start, cur])
65
+ }
66
+ return groups.map(([at, rs]) => [at, rs.map((r) => r.name).join(' + ')])
67
+ }
68
+
69
+ /** chapters.txt: "m:ss title" per line, ready for a video description. */
70
+ export const chaptersText = (chapters) =>
71
+ chapters.map(([at, name]) => `${Math.floor(at / 60)}:${String(Math.floor(at % 60)).padStart(2, '0')} ${name}\n`).join('')
72
+
73
+ /** description.txt: the title, the chapter list and the story link, ready to paste into a video description. */
74
+ export const description = ({ title, chapters, link = null }) => `${title}\n\n${chaptersText(chapters)}${link ? `\n${link}\n` : ''}`
75
+
76
+ /** The story page a video links to: emaki.config.json storyUrl ({site} {locale} {slug}), by default
77
+ * {site}/{locale}/stories/{slug}; null when the pattern needs the site url and site.json has none. */
78
+ export function storyLink(site, cfg, slug, locale) {
79
+ const pattern = cfg?.storyUrl ?? '{site}/{locale}/stories/{slug}'
80
+ const base = String(site?.url ?? '').replace(/\/+$/, '')
81
+ if (pattern.includes('{site}') && !base) return null
82
+ return pattern.replaceAll('{site}', base).replaceAll('{locale}', locale).replaceAll('{slug}', slug)
83
+ }
84
+
85
+ /** poster.jpg, poster-vertical.jpg and <slug>-<locale>-thumb.jpg (1280x720, YouTube's custom thumbnail): from the
86
+ * covers render saved, else a still from the end card of the built video (2.5 s into the last scene, or 0.2 s
87
+ * before its end). Each step is { copy: [src, dst] } or { ffmpeg: args }; a step with no source is left out. */
88
+ export function posterSteps({ render, out, slug, locale, built, last, exists }) {
89
+ const cover = (sfx) => join(render, `${slug}-${locale}-cover-${sfx}.jpg`)
90
+ const still = (last.at + Math.min(last.tot - 0.2, 2.5)).toFixed(2)
91
+ const steps = []
92
+ for (const [sfx, layout, name] of [['16x9', 'landscape', 'poster.jpg'], ['9x16', 'portrait', 'poster-vertical.jpg']]) {
93
+ if (exists(cover(sfx))) steps.push({ copy: [cover(sfx), join(out, name)] })
94
+ else if (built[layout]) steps.push({ ffmpeg: ['-y', '-v', 'error', '-ss', still, '-i', built[layout], '-frames:v', '1', '-q:v', '2', join(out, name)] })
95
+ }
96
+ const thumb = join(out, `${slug}-${locale}-thumb.jpg`)
97
+ if (exists(cover('16x9'))) steps.push({ ffmpeg: ['-y', '-v', 'error', '-i', cover('16x9'), '-vf', 'scale=1280:720', '-q:v', '3', thumb] })
98
+ else if (built.landscape)
99
+ steps.push({ ffmpeg: ['-y', '-v', 'error', '-ss', still, '-i', built.landscape, '-vf', 'scale=1280:720', '-frames:v', '1', '-q:v', '3', thumb] })
100
+ return steps
101
+ }
102
+
103
+ /** The -web.mp4: the 16:9 film at 30 fps, CRF 26, AAC 128k, chapters kept; small enough for static hosting. */
104
+ export const webArgs = (src, dst) => ['-y', '-v', 'error', '-i', src, '-r', '30', '-c:v', 'libx264', '-crf', '26', '-preset', 'slow',
105
+ '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '128k', '-map_chapters', '0', '-movflags', '+faststart', dst]
106
+
107
+ /** The web copy's size, flagged above the 25 MiB a static host such as Cloudflare Pages serves. */
108
+ export function webNote(bytes) {
109
+ const mb = bytes / 1048576
110
+ return `${mb.toFixed(1)} MiB${mb > 25 ? ' - OVER 25 MiB, raise -crf' : ''}`
111
+ }
112
+
113
+ /** One take for the browser (voice-web): mono AAC 64k at -16 LUFS (speech heard on its own, under a ducked score),
114
+ * the index up front so it starts streaming at once. */
115
+ export const webVoiceArgs = (src, dst) => ['-y', '-v', 'error', '-i', src, '-vn', '-ac', '1', '-ar', '48000',
116
+ '-af', 'loudnorm=I=-16:TP=-1.5:LRA=11', '-c:a', 'aac', '-b:a', '64k', '-movflags', '+faststart', dst]
117
+
118
+ const LOUDNORM = 'loudnorm=I=-14:TP=-1.5:LRA=11'
119
+
120
+ /** The rendered picture as one input; its own audio track, when `music`, resampled as the score. */
121
+ function wholePicture(pic, music) {
122
+ return { inputs: ['-i', pic], fc: music ? ['[0:a]aresample=48000,aformat=channel_layouts=stereo[mu]'] : [], vid: '0:v', mu: music ? '[mu]' : null }
123
+ }
124
+
125
+ const sceneVideo = (r, i) => `[${i}:v]${r.ext ? `tpad=stop_mode=clone:stop_duration=${r.ext.toFixed(3)},` : ''}setpts=PTS-STARTPTS[v${i}]`
126
+ function sceneMusic(r, i) {
127
+ const loop = r.ext ? `afade=t=in:st=0:d=0.005,afade=t=out:st=${(r.dur - 0.005).toFixed(3)}:d=0.005,aloop=loop=-1:size=2e9,` : ''
128
+ return `[${i}:a]aresample=48000,aformat=channel_layouts=stereo,${loop}apad,atrim=0:${r.tot.toFixed(3)},asetpts=PTS-STARTPTS[m${i}]`
129
+ }
130
+
131
+ /** Each scene cut from the picture on its own input, a frozen one padded with its last frame and (when `music`) its
132
+ * music looped under the hold, then the scenes joined again. */
133
+ function scenePicture(pic, rows, music) {
134
+ const n = rows.length, labels = (x) => rows.map((_, i) => `[${x}${i}]`).join('')
135
+ const fc = rows.flatMap((r, i) => (music ? [sceneVideo(r, i), sceneMusic(r, i)] : [sceneVideo(r, i)]))
136
+ fc.push(`${labels('v')}concat=n=${n}:v=1:a=0[pv]`)
137
+ if (music) fc.push(`${labels('m')}concat=n=${n}:v=0:a=1[mu]`)
138
+ return { inputs: rows.flatMap((r) => ['-ss', r.start.toFixed(3), '-t', r.dur.toFixed(3), '-i', pic]), fc, vid: '[pv]', mu: music ? '[mu]' : null }
139
+ }
140
+
141
+ /** The voice (input `vo`) alone at -14 LUFS, or with the score (`mu`) ducked under it. */
142
+ const mixFilters = (mu, vo, scoreLevel) => (mu
143
+ ? [`${mu}volume=${scoreLevel}[sc]`, `[${vo}:a]aresample=48000,asplit=2[vo][key]`,
144
+ '[sc][key]sidechaincompress=threshold=0.05:ratio=6:release=400[duck]', `[duck][vo]amix=inputs=2:normalize=0,${LOUDNORM}[a]`]
145
+ : [`[${vo}:a]${LOUDNORM}[a]`])
146
+
147
+ /** ffmpeg for one final video: the rendered picture (`pic`, its own audio track the music when `music`), the voice
148
+ * (`voice`, already on the output timeline), the score ducked under the voice, -14 LUFS, `subtitles` (a filter) burned
149
+ * in. When the plan freezes a scene, each scene is cut from the picture on its own input, the frozen one padded with
150
+ * its last frame and its music looped under the hold (5 ms fades hide the seam), and the scenes joined again.
151
+ * `chapters`: an FFMETADATA file (ffmeta) whose chapters are written in the same pass. */
152
+ export function buildArgs({ pic, voice, plan, music, subtitles = null, chapters = null, scoreLevel = 0.55, dst }) {
153
+ const picture = plan.extended ? scenePicture(pic, plan.rows, music) : wholePicture(pic, music)
154
+ const vo = picture.inputs.filter((x) => x === '-i').length
155
+ const inputs = [...picture.inputs, '-i', voice, ...(chapters ? ['-i', chapters] : [])]
156
+ const meta = chapters ? ['-map_metadata', String(vo + 1), '-map_chapters', String(vo + 1)] : []
157
+ const fc = [...picture.fc, ...mixFilters(picture.mu, vo, scoreLevel)]
158
+ let vid = picture.vid
159
+ if (subtitles) {
160
+ fc.push(`${vid.startsWith('[') ? vid : `[${vid}]`}${subtitles}[v]`)
161
+ vid = '[v]'
162
+ }
163
+ return ['-y', '-v', 'error', ...inputs, '-filter_complex', fc.join(';'), '-map', vid, '-map', '[a]', ...meta,
164
+ '-c:v', 'libx264', '-crf', '18', '-preset', 'medium', '-pix_fmt', 'yuv420p', '-c:a', 'aac', '-b:a', '192k',
165
+ '-t', plan.total.toFixed(3), '-movflags', '+faststart', dst]
166
+ }
@@ -0,0 +1,113 @@
1
+ // The final videos (build.py): the rendered picture, the voice laid into each scene, the score ducked under it,
2
+ // -14 LUFS, captions burned in and shipped as .srt/.vtt, chapters, the description, the web copy and the posters.
3
+ import { copyFileSync, existsSync, mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs'
4
+ import { join, resolve } from 'node:path'
5
+ import { SUFFIX } from './config.js'
6
+ import { buildArgs, buildPlan, chaptersText, description, ffmeta, planTable, posterSteps, storyLink, webArgs, webNote, ytChapters } from './media.js'
7
+ import { takesFor } from './takes.js'
8
+ import { die, hasAudio, load, mediaLength, pad2, run } from './util.js'
9
+
10
+ const stamp = (t, sep = '.') => {
11
+ const h = Math.floor(t / 3600), m = Math.floor((t % 3600) / 60), s = t % 60
12
+ return `${pad2(h)}:${pad2(m)}:${s.toFixed(3).padStart(6, '0')}`.replace('.', sep)
13
+ }
14
+
15
+ /** Captions from the voice lines, spread over each take by sentence length. */
16
+ export function captions(takes, starts, lead, length = mediaLength) {
17
+ const vtt = ['WEBVTT', ''], srt = []
18
+ let k = 0
19
+ for (const [sid, [f, line]] of takes) {
20
+ let t0 = starts.get(sid)[0] + lead
21
+ const span = length(f) - 0.1
22
+ const parts = line.text.split(/(?<=[.!?¿¡;:])\s+/).map((x) => x.trim()).filter(Boolean)
23
+ const total = parts.reduce((n, x) => n + x.length, 0)
24
+ for (const x of parts) {
25
+ const dd = (span * x.length) / total
26
+ k++
27
+ vtt.push(`${stamp(t0)} --> ${stamp(t0 + dd)}`, x, '')
28
+ srt.push(String(k), `${stamp(t0, ',')} --> ${stamp(t0 + dd, ',')}`, x, '')
29
+ t0 += dd
30
+ }
31
+ }
32
+ return { vtt: vtt.join('\n'), srt: srt.join('\n') }
33
+ }
34
+
35
+ /** libass lays SRT out on a 384x288 canvas scaled to the video, so these are in 288ths of the height: landscape 16 ≈
36
+ * 60 px text 127 px up; portrait 9 ≈ 60 px text 333 px up (clear of the platform's buttons). */
37
+ const burnIn = (srtPath, layout) => {
38
+ const size = layout === 'landscape' ? 16 : 9, margin = layout === 'landscape' ? 34 : 50
39
+ return `subtitles=${srtPath.replace(/([:\\'])/g, '\\$1')}:force_style='FontName=Inter,FontSize=${size},PrimaryColour=&H00FFFFFF,OutlineColour=&H66000000,BackColour=&H66000000,BorderStyle=3,Outline=5,Shadow=0,MarginV=${margin}'`
40
+ }
41
+
42
+ /** Final videos per orientation (build.py): the rendered picture with the voice laid `lead` s into each scene, the
43
+ * render's music ducked under it, -14 LUFS, captions burned in (and shipped as .srt/.vtt). A take that outruns its
44
+ * scene freezes the scene's last frame until it has finished (buildPlan). `check` prints the plan and builds nothing;
45
+ * `out` is the output folder (default production/<slug>/<locale>/out/). */
46
+ export function build(p, slug, { locale, noCaptions = false, check = false, out: target = undefined, log = console.info, length = mediaLength } = {}) {
47
+ const d = p.filmDir(slug), film = load(join(d, 'film.json'))
48
+ const tp = join(d, `timing.${locale}.json`)
49
+ const tm = existsSync(tp) ? (load(tp).scenes ?? {}) : {}
50
+ const takes = takesFor(p, slug, locale)
51
+ const plan = buildPlan(film, tm, new Map([...takes].map(([sid, [file]]) => [sid, { file, seconds: length(file) }])), p)
52
+ const render = p.prod(slug, locale, 'render')
53
+ const pics = ['landscape', 'portrait'].map((layout) => [layout, join(render, `${slug}-${locale}-${SUFFIX[layout]}.mp4`)])
54
+ .filter(([, f]) => existsSync(f))
55
+ log(`${film.title ?? slug} (${locale}): ${plan.picture.toFixed(1)} s as rendered`)
56
+ planTable(plan).forEach((l) => log(l))
57
+ if (plan.missing.length) log(`missing takes: ${plan.missing.join(', ')}`)
58
+ if (check) {
59
+ if (!pics.length) log(`MISSING: no render in ${p.rel(render)}; run \`emaki render ${slug} --locale ${locale}\``)
60
+ return { built: [], duration: plan.total, plan }
61
+ }
62
+ if (!takes.size) die(`no takes in ${p.rel(p.prod(slug, locale, 'takes'))}; run \`emaki voice\` (or record them) first`)
63
+ if (!pics.length) die(`no render in ${p.rel(render)}; run \`emaki render ${slug} --locale ${locale}\` first`)
64
+ const out = target ? resolve(target) : p.prod(slug, locale, 'out')
65
+ mkdirSync(out, { recursive: true })
66
+ const voiced = new Map([...takes].filter(([sid]) => plan.rows.some((r) => r.id === sid)))
67
+ const starts = new Map(plan.rows.map((r) => [r.id, [r.at, r.tot]]))
68
+ const cap = captions(voiced, starts, p.lead, length)
69
+ const srtPath = join(out, `${slug}-${locale}.srt`)
70
+ writeFileSync(join(out, `${slug}-${locale}.vtt`), cap.vtt)
71
+ writeFileSync(srtPath, cap.srt)
72
+ const inputs = [], filters = []
73
+ ;[...voiced].forEach(([sid, [f]], i) => {
74
+ inputs.push('-i', f)
75
+ const ms = Math.round((starts.get(sid)[0] + p.lead) * 1000)
76
+ filters.push(`[${i}:a]aresample=48000,adelay=${ms}|${ms},apad[v${i}]`)
77
+ })
78
+ const voicePath = join(out, 'voice.wav')
79
+ run('ffmpeg', ['-y', '-v', 'error', ...inputs, '-filter_complex',
80
+ filters.join(';') + ';' + [...voiced].map((_, i) => `[v${i}]`).join('') + `amix=inputs=${voiced.size}:normalize=0,atrim=0:${plan.total.toFixed(3)}[vo]`,
81
+ '-map', '[vo]', '-ac', '2', voicePath])
82
+ const chapters = join(out, 'chapters.ffmeta')
83
+ writeFileSync(chapters, ffmeta(plan.rows))
84
+ const yt = ytChapters(plan.rows)
85
+ writeFileSync(join(out, 'chapters.txt'), chaptersText(yt))
86
+ const story = join(p.content, 'stories', locale, `${slug}.json`)
87
+ const title = (existsSync(story) ? load(story).title : null) ?? film.title ?? slug
88
+ writeFileSync(join(out, 'description.txt'), description({ title, chapters: yt, link: storyLink(p.site, p.cfg, slug, locale) }))
89
+ const built = [], videos = {}
90
+ for (const [layout, pic] of pics) {
91
+ const dst = join(out, `${slug}-${locale}-${SUFFIX[layout]}.mp4`)
92
+ run('ffmpeg', buildArgs({
93
+ pic, voice: voicePath, plan, music: hasAudio(pic), subtitles: noCaptions ? null : burnIn(srtPath, layout),
94
+ chapters, scoreLevel: p.cfg.scoreLevel ?? 0.55, dst
95
+ }))
96
+ log(`built ${p.rel(dst)} (${plan.rows.length} chapters)`)
97
+ built.push((videos[layout] = dst))
98
+ }
99
+ rmSync(chapters, { force: true })
100
+ if (videos.landscape) {
101
+ const web = join(out, `${slug}-${locale}-web.mp4`)
102
+ run('ffmpeg', webArgs(videos.landscape, web))
103
+ log(`built ${p.rel(web)} (${webNote(statSync(web).size)})`)
104
+ built.push(web)
105
+ }
106
+ for (const step of posterSteps({ render, out, slug, locale, built: videos, last: plan.rows.at(-1), exists: existsSync })) {
107
+ if (step.copy) copyFileSync(...step.copy)
108
+ else run('ffmpeg', step.ffmpeg)
109
+ log(` ${p.rel((step.copy ?? step.ffmpeg).at(-1))}`)
110
+ }
111
+ log(`captions: ${p.rel(out)}/${slug}-${locale}.vtt/.srt; chapters.txt, description.txt`)
112
+ return { built, duration: plan.total, plan }
113
+ }
package/src/campaign.js CHANGED
@@ -5,32 +5,13 @@
5
5
  import { readFileSync, readdirSync, writeFileSync } from 'node:fs'
6
6
  import { join, relative } from 'node:path'
7
7
  import { SUFFIX } from './config.js'
8
+ import { parseCsv } from './csv.js'
8
9
  import { beatsOf, campaignDir, dayScriptPath, slugOfScript } from './day-script.js'
9
10
  import { newFilm } from './films.js'
10
11
  import { filmFiles, recordLinks, storiesIndex, storyUrl } from './stories.js'
11
12
  import { die, exists, load, save } from './util.js'
12
13
 
13
- /** Rows of a CSV with a header line, as objects. Quoted fields may hold commas, newlines and "" quotes. */
14
- export function parseCsv(text) {
15
- const rows = []
16
- let row = [], cell = '', quoted = false
17
- for (let i = 0; i < text.length; i++) {
18
- const c = text[i]
19
- if (quoted) {
20
- if (c === '"' && text[i + 1] === '"') { cell += '"'; i++ }
21
- else if (c === '"') quoted = false
22
- else cell += c
23
- } else if (c === '"') quoted = true
24
- else if (c === ',') { row.push(cell); cell = '' }
25
- else if (c === '\n' || c === '\r') {
26
- if (c === '\r' && text[i + 1] === '\n') i++
27
- row.push(cell); rows.push(row); row = []; cell = ''
28
- } else cell += c
29
- }
30
- if (cell || row.length) { row.push(cell); rows.push(row) }
31
- const [head = [], ...body] = rows.filter((r) => r.some((x) => x !== ''))
32
- return body.map((r) => Object.fromEntries(head.map((h, i) => [h.trim(), r[i] ?? ''])))
33
- }
14
+ export { parseCsv }
34
15
 
35
16
  /** Today (YYYY-MM-DD) in a time zone. */
36
17
  export const todayIn = (timeZone = 'UTC', now = new Date()) =>
package/src/capture.js CHANGED
@@ -1,79 +1,14 @@
1
1
  // sheet and render drive the site's capture page in headless Chromium: /show/<slug>?capture=1 installs
2
2
  // window.__show = { meta, ready, seek(t), audio() } (installCapture in @senseihq/emaki; ShowPage in each wrapper).
3
3
  // Playwright is an optional dependency: `npm i -D playwright && npx playwright install chromium`.
4
- import { spawn } from 'node:child_process'
5
- import { existsSync, mkdirSync, renameSync, writeFileSync, readFileSync } from 'node:fs'
6
- import { connect } from 'node:net'
7
- import { dirname, join } from 'node:path'
8
- import { SIZES, SUFFIX } from './config.js'
9
- import { encodeArgs, frameRange, musicFor, muxArgs, renderOut, renderSteps, retrying, staleRender } from './media.js'
10
- import { die, mediaLength, run } from './util.js'
4
+ import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { chromium, openShow, openSite } from './show-page.js'
7
+ import { die } from './util.js'
11
8
 
12
- /** page.evaluate that survives the dev server reloading the page under it (a file saved mid-render): it waits for
13
- * the show to come back and repeats the same call (render.py's call()). */
14
- const evaluate = (page, fn, arg) => retrying(() => page.evaluate(fn, arg), async () => {
15
- await page.waitForFunction(() => window.__show, null, { timeout: 30000 })
16
- await page.evaluate(() => window.__show.ready)
17
- })
18
-
19
- export async function chromium() {
20
- try {
21
- return (await import('playwright')).chromium
22
- } catch {
23
- return die('sheet, render and carousel need Playwright: npm i -D playwright && npx playwright install chromium')
24
- }
25
- }
26
-
27
- const portOpen = (port) => new Promise((ok) => {
28
- const s = connect({ port, host: '127.0.0.1' })
29
- s.setTimeout(500)
30
- s.once('connect', () => { s.destroy(); ok(true) })
31
- s.once('error', () => ok(false))
32
- s.once('timeout', () => { s.destroy(); ok(false) })
33
- })
34
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
35
-
36
- /** The site to capture: `url` if given (must answer), else the dev server (emaki.config.json devCommand), started if
37
- * nothing listens on the port yet and stopped afterwards. */
38
- export async function openSite(p, url) {
39
- const port = p.cfg.port ?? 5199
40
- let proc = null
41
- const base = (url ?? `http://localhost:${port}`).replace(/\/$/, '')
42
- if (url) {
43
- try {
44
- await fetch(url, { signal: AbortSignal.timeout(10000) })
45
- } catch (e) {
46
- die(`the site at ${url} is not responding (${e.message}); start it, or leave out --url to let emaki start it`)
47
- }
48
- } else if (!(await portOpen(port))) {
49
- const cmd = (p.cfg.devCommand ?? 'npm run dev -- --port {port} --strictPort').replaceAll('{port}', String(port))
50
- proc = spawn(cmd, { shell: true, cwd: p.root, stdio: 'ignore', detached: process.platform !== 'win32' })
51
- let up = false
52
- for (let i = 0; i < 120 && !(up = await portOpen(port)); i++) await sleep(500)
53
- if (!up) { stop(); die(`the dev server did not start (${cmd})`) }
54
- }
55
- function stop() {
56
- if (!proc) return
57
- try { process.kill(-proc.pid) } catch { proc.kill() }
58
- }
59
- const showPath = p.cfg.showPath ?? '/show/{slug}?capture=1&o={layout}&locale={locale}'
60
- return {
61
- base,
62
- show: (slug, layout, locale) => base + showPath.replaceAll('{slug}', slug).replaceAll('{layout}', layout).replaceAll('{locale}', locale),
63
- close: stop
64
- }
65
- }
66
-
67
- async function openShow(browser, site, slug, layout, locale) {
68
- const [width, height] = SIZES[layout]
69
- const page = await browser.newPage({ viewport: { width, height }, deviceScaleFactor: 1 })
70
- const errors = []
71
- page.on('pageerror', (e) => errors.push(String(e)))
72
- await page.goto(site.show(slug, layout, locale))
73
- await page.waitForFunction(() => window.__show, null, { timeout: 30000 })
74
- await page.evaluate(() => window.__show.ready)
75
- return { page, meta: await page.evaluate(() => window.__show.meta), errors }
76
- }
9
+ // The capture page (show-page.js) and render (render.js), re-exported: carousel, the CLI and the API import them here.
10
+ export { chromium, openSite }
11
+ export { render } from './render.js'
77
12
 
78
13
  /** A contact sheet: each scene's final picture (or --at scene:t,...) in both orientations, side by side. */
79
14
  export async function sheet(p, slug, { locale, url, at, log = console.info } = {}) {
@@ -120,86 +55,3 @@ export async function sheet(p, slug, { locale, url, at, log = console.info } = {
120
55
  site.close()
121
56
  }
122
57
  }
123
-
124
- /** Frame-by-frame capture to MP4 with its music, plus a cover JPEG per orientation (render.py).
125
- * `from`/`to` capture a range of film time; `out` is a file (one layout) or a folder. `music`: synth (the page's
126
- * score, saved as score.wav), export (the audio of production/<slug>/<locale>/<slug>.mp4) or none. `audioOnly` lays
127
- * the music on the existing render without capturing frames; `coverOnly` saves just the covers. */
128
- export async function render(p, slug, {
129
- locale, url, o = 'both', fps = 30, from = 0, to = undefined, out: target = undefined, music: mode = 'synth',
130
- audioOnly = false, coverOnly = false, log = console.info
131
- } = {}) {
132
- p.filmDir(slug)
133
- const steps = renderSteps({ audioOnly, coverOnly })
134
- const layouts = o === 'both' ? ['landscape', 'portrait'] : [o]
135
- const out = p.prod(slug, locale, 'render')
136
- mkdirSync(out, { recursive: true })
137
- const music = musicFor(mode, { exportFile: p.prod(slug, locale, `${slug}.mp4`), exists: existsSync })
138
- if (steps.music && music.note) log(` ${music.note}`)
139
- const site = await openSite(p, url), made = []
140
- const browser = await (await chromium()).launch()
141
- try {
142
- let audio
143
- for (const layout of layouts) {
144
- const { page, meta, errors } = await openShow(browser, site, slug, layout, locale)
145
- const call = (fn, arg) => evaluate(page, fn, arg)
146
- const dst = renderOut(out, slug, locale, layout, { out: target, layouts: layouts.length })
147
- if (steps.music && audio === undefined) {
148
- audio = music.kind === 'file' ? music.file : null
149
- if (music.kind === 'synth') {
150
- const b64 = await call(() => window.__show.audio())
151
- if (b64) writeFileSync((audio = join(out, 'score.wav')), Buffer.from(b64, 'base64'))
152
- log(b64 ? ` score: ${p.rel(audio)}` : ' the film has no score: silent picture')
153
- }
154
- }
155
- if (steps.frames) {
156
- const { t0, end, frames, seconds } = frameRange(meta.duration, fps, from, to)
157
- log(`${slug}: ${layout} at ${fps} fps, ${t0 ? `${t0.toFixed(1)}–` : ''}${end.toFixed(1)} s, ${audio ? `music: ${mode}` : 'no music'}`)
158
- const tmp = dst.replace(/\.mp4$/i, '.part.mp4')
159
- mkdirSync(dirname(dst), { recursive: true })
160
- const ff = spawn('ffmpeg', encodeArgs({ fps, seconds, dst: tmp, audio, t0 }), { stdio: ['pipe', 'inherit', 'inherit'] })
161
- const done = new Promise((ok, bad) => { ff.on('close', (c) => (c ? bad(new Error('ffmpeg failed')) : ok())); ff.on('error', bad) })
162
- const began = Date.now()
163
- for (let i = 0; i < frames; i++) {
164
- await call((t) => window.__show.seek(t), t0 + i / fps)
165
- const jpg = await page.screenshot({ type: 'jpeg', quality: 94 })
166
- if (!ff.stdin.write(jpg)) await new Promise((r) => ff.stdin.once('drain', r))
167
- if (i % (fps * 10) === 0) log(` ${layout} ${(t0 + i / fps).toFixed(1).padStart(6)}s / ${end.toFixed(1)}s`)
168
- }
169
- ff.stdin.end()
170
- await done.catch((e) => die(e.message))
171
- if (errors.length) die('the page reported errors: ' + errors.slice(0, 3).join('; '))
172
- renameSync(tmp, dst)
173
- log(` wrote ${p.rel(dst)} (${frames} frames, ${Math.round((Date.now() - began) / 1000)}s)`)
174
- made.push(dst)
175
- } else if (steps.music) {
176
- if (!existsSync(dst)) die(`--audio-only needs ${p.rel(dst)}; render it first`)
177
- const dur = mediaLength(dst)
178
- const stale = staleRender(dur, meta.duration, Boolean(from) || to != null)
179
- if (stale) log(` warning: ${p.rel(dst)}: ${stale}`)
180
- if (audio) {
181
- const tmp = dst.replace(/\.mp4$/i, '.mux.mp4')
182
- run('ffmpeg', muxArgs(dst, audio, Number(from) || 0, dur, tmp))
183
- renameSync(tmp, dst)
184
- log(` music laid on ${p.rel(dst)}`)
185
- } else log(` no music to lay on ${p.rel(dst)}`)
186
- made.push(dst)
187
- }
188
- await call((t) => window.__show.seek(t), meta.cover)
189
- const cover = join(out, `${slug}-${locale}-cover-${SUFFIX[layout]}.jpg`)
190
- writeFileSync(cover, await page.screenshot({ type: 'jpeg', quality: 92 }))
191
- log(` cover: ${p.rel(cover)} (film time ${meta.cover.toFixed(1)} s)`)
192
- if (layout === 'landscape' && p.cfg.coversDir) {
193
- const siteCover = join(p.root, p.cfg.coversDir, locale === p.site.defaultLocale ? `${slug}.jpg` : `${slug}.${locale}.jpg`)
194
- mkdirSync(dirname(siteCover), { recursive: true })
195
- run('ffmpeg', ['-y', '-loglevel', 'error', '-i', cover, '-vf', 'scale=1600:-2', '-q:v', '3', siteCover])
196
- log(` cover for the site: ${p.rel(siteCover)}`)
197
- }
198
- await page.close()
199
- }
200
- return made
201
- } finally {
202
- await browser.close()
203
- site.close()
204
- }
205
- }
@@ -0,0 +1,63 @@
1
+ // emaki campaign status | shortfilm | posted | due | calendar: the daily series from the command line.
2
+ import { act, P } from './run.js'
3
+
4
+ const yes = (b) => (b ? 'yes' : ' - ')
5
+
6
+ /** One day of campaign status: what exists for it, or that it has no day script yet. */
7
+ function statusRow(r) {
8
+ const head = `${r.date} ${r.topic.slice(0, 11).padEnd(11)} ${r.title.slice(0, 46).padEnd(46)}`
9
+ if (!r.script) return `${head} (no day script yet)`
10
+ const posts = ['youtube', 'instagram', 'x'].map((k) => (r.posts[k] ? k[0].toUpperCase() : '·')).join('')
11
+ return `${head} ${yes(r.story)} ${yes(r.film)} ${String(r.takes).padStart(4)} ${yes(r.render)} ${yes(r.built)} ${String(r.carousel).padStart(3)} ${posts}`
12
+ }
13
+
14
+ /** The campaign commands. */
15
+ export function campaignCommands(prog) {
16
+ prog.command('campaign status')
17
+ .describe('What is due, and for each day what exists: story, film, takes, render, Short, carousel, posts.')
18
+ .option('--days', 'How many days', 14)
19
+ .option('--from', 'First date YYYY-MM-DD (default: today in the site\'s time zone)')
20
+ .action(act(async (o) => {
21
+ const { campaignStatus } = await import('../campaign.js')
22
+ console.info(`${'date'.padEnd(10)} ${'topic'.padEnd(11)} ${'title'.padEnd(46)} story film takes rndr short car posts`)
23
+ for (const r of campaignStatus(P(), { from: o.from && String(o.from), days: Number(o.days) })) console.info(statusRow(r))
24
+ console.info('\nposts: Y = YouTube, I = Instagram, X = X; · = not yet')
25
+ }))
26
+
27
+ prog.command('campaign shortfilm <slug>')
28
+ .describe('Set up a Short\'s film (scenes and voice lines) from its day script\'s voiceover table.')
29
+ .option('--locale', 'Locale (default: the site default)')
30
+ .action(act(async (slug, o) => {
31
+ const { shortFilm } = await import('../campaign.js')
32
+ const r = shortFilm(P(), slug, { locale: o.locale })
33
+ console.info(`wrote ${r.path}: ${r.scenes} beats. Next: build the scenes, then \`emaki voice ${slug}\`, \`emaki fit ${slug}\`, \`emaki render ${slug} --o portrait\`.`)
34
+ }))
35
+
36
+ prog.command('campaign posted <slug>')
37
+ .describe('Record where a piece was posted: the campaign ledger and the story\'s social links.')
38
+ .option('--youtube', 'Video id or URL')
39
+ .option('--instagram', 'Post URL')
40
+ .option('--x', 'Post URL')
41
+ .action(act(async (slug, o) => {
42
+ const { campaignPosted } = await import('../campaign.js')
43
+ const s = (v) => (v === undefined || v === true ? undefined : String(v))
44
+ const r = campaignPosted(P(), slug, { youtube: s(o.youtube), instagram: s(o.instagram), x: s(o.x) })
45
+ console.info(`recorded ${slug}: ${JSON.stringify(r)}. Commit content/stories/index.json; the next build links the posts.`)
46
+ }))
47
+
48
+ prog.command('campaign due')
49
+ .describe('The piece for one day (default today in the site\'s time zone), as JSON, for automation.')
50
+ .option('--date', 'YYYY-MM-DD')
51
+ .action(act(async (o) => {
52
+ const { campaignDue } = await import('../campaign.js')
53
+ console.info(JSON.stringify(campaignDue(P(), { date: o.date && String(o.date) }), null, 2))
54
+ }))
55
+
56
+ prog.command('campaign calendar')
57
+ .describe('Rewrite CALENDAR.md in the campaign folder from calendar.csv.')
58
+ .action(act(async () => {
59
+ const { campaignCalendar } = await import('../campaign.js')
60
+ const r = campaignCalendar(P())
61
+ console.info(`wrote ${r.path} (${r.days} days)`)
62
+ }))
63
+ }