@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 +3 -3
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/src/build-media.js +166 -0
- package/src/build-video.js +113 -0
- package/src/campaign.js +2 -21
- package/src/capture.js +7 -155
- package/src/commands/campaign.js +63 -0
- package/src/commands/posting.js +74 -0
- package/src/commands/production.js +128 -0
- package/src/commands/run.js +19 -0
- package/src/commands/setup.js +97 -0
- package/src/csv.js +42 -0
- package/src/film-checks.js +102 -0
- package/src/films.js +3 -78
- package/src/fit.js +79 -0
- package/src/index.js +10 -342
- package/src/init.js +5 -9
- package/src/media.js +7 -163
- package/src/migrate.js +44 -27
- package/src/publish.js +43 -25
- package/src/render.js +125 -0
- package/src/show-page.js +75 -0
- package/src/stage.js +44 -31
- package/src/takes.js +18 -0
- package/src/tts-cloud.js +122 -0
- package/src/tts-io.js +51 -0
- package/src/tts-offline.js +85 -0
- package/src/tts.js +5 -237
- package/src/voice.js +10 -199
package/src/migrate.js
CHANGED
|
@@ -15,9 +15,8 @@ import { die, exists, load } from './util.js'
|
|
|
15
15
|
/** Index fields that are words for the reader: emaki keeps them per locale, in the story body. */
|
|
16
16
|
const TEXT = ['title', 'dek', 'kanjiGloss', 'series']
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!exists(films) && !exists(articles)) die(`${from} has neither src/lib/motion/films nor src/lib/content/articles`)
|
|
18
|
+
/** Writes that never overwrite: each target lands in `wrote`, or in `kept` when it already exists. */
|
|
19
|
+
function writer(p) {
|
|
21
20
|
const wrote = [], kept = []
|
|
22
21
|
const put = (dst, write) => {
|
|
23
22
|
const rel = p.rel(dst)
|
|
@@ -33,35 +32,53 @@ export function migrate(p, from, { locale = 'en' } = {}) {
|
|
|
33
32
|
if (e.isDirectory()) tree(join(src, e.name), join(dst, e.name))
|
|
34
33
|
else copy(join(src, e.name), join(dst, rename(e.name)))
|
|
35
34
|
}
|
|
35
|
+
return { wrote, kept, copy, json, tree }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The sub-folders of `dir` that hold a film.json (none when `dir` is missing). */
|
|
39
|
+
const filmFolders = (dir) => (exists(dir) ? readdirSync(dir).filter((d) => exists(join(dir, d, 'film.json'))) : [])
|
|
36
40
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
/** src/lib/motion/films/<slug>/ → content/films/<slug>/ (base.css becomes look.css), then the logos. */
|
|
42
|
+
function moveFilms(p, from, w) {
|
|
43
|
+
const films = join(from, 'src/lib/motion/films')
|
|
44
|
+
for (const slug of filmFolders(films)) w.tree(join(films, slug), join(p.films, slug), (n) => (n === 'base.css' ? 'look.css' : n))
|
|
40
45
|
const logos = join(from, 'src/lib/motion/logos')
|
|
41
|
-
if (exists(logos)) tree(logos, join(p.content, 'logos'))
|
|
46
|
+
if (exists(logos)) w.tree(logos, join(p.content, 'logos'))
|
|
47
|
+
}
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
/** The articles index without its reader-facing text, and each article's body with that text, in `locale`. */
|
|
50
|
+
function moveArticles(p, articles, locale, w) {
|
|
51
|
+
if (!exists(join(articles, 'index.json'))) return
|
|
52
|
+
const index = load(join(articles, 'index.json'))
|
|
53
|
+
w.json(join(p.content, 'stories', 'index.json'), index.map((e) => Object.fromEntries(Object.entries(e).filter(([k]) => !TEXT.includes(k)))))
|
|
54
|
+
for (const e of index) {
|
|
55
|
+
const src = join(articles, `${e.slug}.json`)
|
|
56
|
+
if (!exists(src)) continue
|
|
57
|
+
const text = Object.fromEntries(TEXT.filter((k) => k in e).map((k) => [k, e[k]]))
|
|
58
|
+
w.json(join(p.content, 'stories', locale, `${e.slug}.json`), { ...text, ...load(src) })
|
|
52
59
|
}
|
|
60
|
+
}
|
|
53
61
|
|
|
62
|
+
/** films/<slug>/: the narration (into a film that was moved) and the recorded takes. */
|
|
63
|
+
function moveMasters(p, from, locale, w) {
|
|
54
64
|
const masters = join(from, 'films')
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
for (const slug of filmFolders(masters)) {
|
|
66
|
+
const scenes = load(join(masters, slug, 'film.json')).scenes ?? []
|
|
67
|
+
if (scenes.length && exists(join(p.films, slug)))
|
|
68
|
+
w.json(join(p.films, slug, 'voice', `${locale}.json`), scenes.map((s) => ({ scene: s.id, text: s.text ?? '', say: s.say ?? null })))
|
|
69
|
+
const takes = join(masters, slug, 'takes')
|
|
70
|
+
const wavs = exists(takes) ? readdirSync(takes).filter((n) => n.endsWith('.wav')) : []
|
|
71
|
+
for (const f of wavs) w.copy(join(takes, f), p.prod(slug, locale, 'takes', f))
|
|
72
|
+
}
|
|
73
|
+
}
|
|
64
74
|
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
export function migrate(p, from, { locale = 'en' } = {}) {
|
|
76
|
+
const films = join(from, 'src/lib/motion/films'), articles = join(from, 'src/lib/content/articles')
|
|
77
|
+
if (!exists(films) && !exists(articles)) die(`${from} has neither src/lib/motion/films nor src/lib/content/articles`)
|
|
78
|
+
const w = writer(p)
|
|
79
|
+
moveFilms(p, from, w)
|
|
80
|
+
moveArticles(p, articles, locale, w)
|
|
81
|
+
moveMasters(p, from, locale, w)
|
|
82
|
+
if (!exists(join(p.content, 'site.json'))) w.json(join(p.content, 'site.json'), { name: '', url: '', locales: [locale], defaultLocale: locale })
|
|
83
|
+
return { wrote: w.wrote, kept: w.kept }
|
|
67
84
|
}
|
package/src/publish.js
CHANGED
|
@@ -34,38 +34,45 @@ function ytClient(env, fetch, log) {
|
|
|
34
34
|
return youtube({ clientId: env.YT_CLIENT_ID, clientSecret: env.YT_CLIENT_SECRET, refreshToken: env.YT_REFRESH_TOKEN, fetch, log })
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const yt = dryRun ? null : ytClient(env, fetch, log)
|
|
37
|
+
const DESCRIPTION_MODES = ['prepend', 'replace']
|
|
38
|
+
const chapterCount = (desc) => desc.split('\n').filter((l) => /^\d+:\d\d /.test(l)).length
|
|
39
|
+
|
|
40
|
+
/** What a YouTube post carries: the built 16:9 film, the description, the story's tags, captions and thumbnail. */
|
|
41
|
+
function youtubePiece(p, slug, locale) {
|
|
43
42
|
const story = storyFor(p, slug, locale), f = filmFiles(p, slug, locale)
|
|
44
43
|
if (!f.wide) die(`${slug}: no built 16:9 video (${p.rel(f.expect.wide)}); run \`emaki render ${slug}\` and \`emaki build ${slug}\` first`)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const remote = dryRun ? null : (await yt.getVideo(update)).snippet
|
|
49
|
-
const merged = remote ? mergeDescription(remote.description ?? '', desc, descriptionMode) : desc
|
|
50
|
-
log(`YouTube update · ${story.title} · https://www.youtube.com/watch?v=${update}`)
|
|
51
|
-
log(` title: ${remote ? JSON.stringify(remote.title) : '(remote)'} → ${JSON.stringify(story.title)}`)
|
|
52
|
-
log(` desc: ${descriptionMode}, ${merged.length} chars`)
|
|
53
|
-
log(` tags: ${tags.length ? tags.join(', ') : '(keep remote)'}`)
|
|
54
|
-
log(` captions: ${f.srt ? `replace uploaded tracks with ${basename(f.srt)}` : 'none (skipped)'}`)
|
|
55
|
-
log(` thumb: ${f.thumb ? `set ${basename(f.thumb)}` : 'skipped (none rendered)'}`)
|
|
56
|
-
if (dryRun) return log('dry run: nothing changed'), { dryRun: true }
|
|
57
|
-
await yt.updateMetadata(update, { title: story.title, description: merged, tags: tags.length ? tags : undefined })
|
|
58
|
-
if (srt) await yt.replaceCaptions(update, srt, locale)
|
|
59
|
-
if (thumb) await yt.setThumbnail(update, thumb)
|
|
60
|
-
recordLinks(p, slug, { youtube: update })
|
|
61
|
-
log(`updated; recorded in ${p.rel(storiesIndex(p))}`)
|
|
62
|
-
return { id: update, updated: true }
|
|
44
|
+
return {
|
|
45
|
+
story, f, desc: youtubeDescription(p, slug, locale, story.title), tags: story.tags,
|
|
46
|
+
srt: f.srt ? readFileSync(f.srt) : null, thumb: f.thumb ? readFileSync(f.thumb) : null
|
|
63
47
|
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** --update: refreshes the uploaded video's title, description (merged with the remote one), tags, captions, thumbnail. */
|
|
51
|
+
async function updateYoutube(p, slug, yt, { story, f, desc, tags, srt, thumb }, { update, dryRun, descriptionMode, locale, log }) {
|
|
52
|
+
const remote = dryRun ? null : (await yt.getVideo(update)).snippet
|
|
53
|
+
const merged = remote ? mergeDescription(remote.description ?? '', desc, descriptionMode) : desc
|
|
54
|
+
log(`YouTube update · ${story.title} · https://www.youtube.com/watch?v=${update}`)
|
|
55
|
+
log(` title: ${remote ? JSON.stringify(remote.title) : '(remote)'} → ${JSON.stringify(story.title)}`)
|
|
56
|
+
log(` desc: ${descriptionMode}, ${merged.length} chars`)
|
|
57
|
+
log(` tags: ${tags.length ? tags.join(', ') : '(keep remote)'}`)
|
|
58
|
+
log(` captions: ${f.srt ? `replace uploaded tracks with ${basename(f.srt)}` : 'none (skipped)'}`)
|
|
59
|
+
log(` thumb: ${f.thumb ? `set ${basename(f.thumb)}` : 'skipped (none rendered)'}`)
|
|
60
|
+
if (dryRun) return log('dry run: nothing changed'), { dryRun: true }
|
|
61
|
+
await yt.updateMetadata(update, { title: story.title, description: merged, tags: tags.length ? tags : undefined })
|
|
62
|
+
if (srt) await yt.replaceCaptions(update, srt, locale)
|
|
63
|
+
if (thumb) await yt.setThumbnail(update, thumb)
|
|
64
|
+
recordLinks(p, slug, { youtube: update })
|
|
65
|
+
log(`updated; recorded in ${p.rel(storiesIndex(p))}`)
|
|
66
|
+
return { id: update, updated: true }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A new upload; refused when the story already has a video, unless --force-new. */
|
|
70
|
+
async function uploadYoutube(p, slug, yt, { story, f, desc, tags, srt, thumb }, { privacy, forceNew, dryRun, locale, log }) {
|
|
64
71
|
if (story.youtube && !forceNew)
|
|
65
72
|
die(`${slug} is already on YouTube as ${story.youtube}; pass --update ${story.youtube} to refresh it, or --force-new to upload again`)
|
|
66
73
|
log(`YouTube · ${story.title} · privacy ${privacy}`)
|
|
67
74
|
log(` video: ${basename(f.wide)} (${kb(f.wide)})`)
|
|
68
|
-
log(` desc: ${desc.length} chars, ${desc
|
|
75
|
+
log(` desc: ${desc.length} chars, ${chapterCount(desc)} chapters`)
|
|
69
76
|
log(` tags: ${tags.slice(0, 10).join(', ')}`)
|
|
70
77
|
log(` captions: ${f.srt ? basename(f.srt) : 'none (continues without)'}`)
|
|
71
78
|
log(` thumb: ${f.thumb ? basename(f.thumb) : 'none (continues without)'}`)
|
|
@@ -76,6 +83,17 @@ async function publishYoutube(p, slug, { locale, verify, dryRun, privacy = 'unli
|
|
|
76
83
|
return r
|
|
77
84
|
}
|
|
78
85
|
|
|
86
|
+
async function publishYoutube(p, slug, { locale, verify, dryRun, privacy = 'unlisted', update, forceNew, descriptionMode = 'prepend', env = process.env, fetch = globalThis.fetch, log = console.info } = {}) {
|
|
87
|
+
if (verify) return log(`YouTube posts as: ${await ytClient(env, fetch, log).verify()}`)
|
|
88
|
+
if (!PRIVACY.includes(privacy)) die('--privacy: public, unlisted or private')
|
|
89
|
+
if (!DESCRIPTION_MODES.includes(descriptionMode)) die('--description-mode: prepend or replace')
|
|
90
|
+
locale ??= p.site.defaultLocale
|
|
91
|
+
const yt = dryRun ? null : ytClient(env, fetch, log)
|
|
92
|
+
const piece = youtubePiece(p, slug, locale)
|
|
93
|
+
if (update) return updateYoutube(p, slug, yt, piece, { update, dryRun, descriptionMode, locale, log })
|
|
94
|
+
return uploadYoutube(p, slug, yt, piece, { privacy, forceNew, dryRun, locale, log })
|
|
95
|
+
}
|
|
96
|
+
|
|
79
97
|
/** Public URLs Meta can fetch: the flags, else the files staged on the desk and signed for an hour. */
|
|
80
98
|
async function reelUrls(p, slug, f, { videoUrl, coverUrl, env, fetch, now }) {
|
|
81
99
|
if (videoUrl && coverUrl) return [videoUrl, coverUrl]
|
package/src/render.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// render: the film frame by frame to MP4 with its music, plus a cover JPEG per orientation (render.py). One job is
|
|
2
|
+
// shared by every layout; each layout captures its frames (or lays the music), then saves its covers.
|
|
3
|
+
import { spawn } from 'node:child_process'
|
|
4
|
+
import { existsSync, mkdirSync, renameSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { dirname, join } from 'node:path'
|
|
6
|
+
import { SUFFIX } from './config.js'
|
|
7
|
+
import { encodeArgs, frameRange, musicFor, muxArgs, renderOut, renderSteps, staleRender } from './media.js'
|
|
8
|
+
import { chromium, evaluate, openShow, openSite } from './show-page.js'
|
|
9
|
+
import { die, mediaLength, run } from './util.js'
|
|
10
|
+
|
|
11
|
+
/** The score for the whole run, read once: the exported film's audio, the page's synthesised score (saved as
|
|
12
|
+
* score.wav), or null for a silent picture. */
|
|
13
|
+
async function scoreFor(job, call) {
|
|
14
|
+
if (job.music.kind !== 'synth') return job.music.kind === 'file' ? job.music.file : null
|
|
15
|
+
const b64 = await call(() => window.__show.audio())
|
|
16
|
+
if (!b64) return job.log(' the film has no score: silent picture'), null
|
|
17
|
+
const audio = join(job.out, 'score.wav')
|
|
18
|
+
writeFileSync(audio, Buffer.from(b64, 'base64'))
|
|
19
|
+
job.log(` score: ${job.p.rel(audio)}`)
|
|
20
|
+
return audio
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Seeks the page to each frame of the range and writes its JPEG to ffmpeg's stdin, waiting when the pipe is full. */
|
|
24
|
+
async function pipeFrames(job, page, call, ff, { layout, t0, end, frames }) {
|
|
25
|
+
const { fps, log } = job
|
|
26
|
+
for (let i = 0; i < frames; i++) {
|
|
27
|
+
await call((t) => window.__show.seek(t), t0 + i / fps)
|
|
28
|
+
const jpg = await page.screenshot({ type: 'jpeg', quality: 94 })
|
|
29
|
+
if (!ff.stdin.write(jpg)) await new Promise((r) => ff.stdin.once('drain', r))
|
|
30
|
+
if (i % (fps * 10) === 0) log(` ${layout} ${(t0 + i / fps).toFixed(1).padStart(6)}s / ${end.toFixed(1)}s`)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Captures the frames into <dst>.part.mp4 through ffmpeg (the score under them), then moves it into place. */
|
|
35
|
+
async function captureFrames(job, { page, meta, errors }, call, layout, dst) {
|
|
36
|
+
const { p, slug, fps, audio, log } = job
|
|
37
|
+
const range = frameRange(meta.duration, fps, job.from, job.to), { t0, end, frames, seconds } = range
|
|
38
|
+
log(`${slug}: ${layout} at ${fps} fps, ${t0 ? `${t0.toFixed(1)}–` : ''}${end.toFixed(1)} s, ${audio ? `music: ${job.mode}` : 'no music'}`)
|
|
39
|
+
const tmp = dst.replace(/\.mp4$/i, '.part.mp4')
|
|
40
|
+
mkdirSync(dirname(dst), { recursive: true })
|
|
41
|
+
const ff = spawn('ffmpeg', encodeArgs({ fps, seconds, dst: tmp, audio, t0 }), { stdio: ['pipe', 'inherit', 'inherit'] })
|
|
42
|
+
const done = new Promise((ok, bad) => { ff.on('close', (c) => (c ? bad(new Error('ffmpeg failed')) : ok())); ff.on('error', bad) })
|
|
43
|
+
const began = Date.now()
|
|
44
|
+
await pipeFrames(job, page, call, ff, { layout, ...range })
|
|
45
|
+
ff.stdin.end()
|
|
46
|
+
await done.catch((e) => die(e.message))
|
|
47
|
+
if (errors.length) die('the page reported errors: ' + errors.slice(0, 3).join('; '))
|
|
48
|
+
renameSync(tmp, dst)
|
|
49
|
+
log(` wrote ${p.rel(dst)} (${frames} frames, ${Math.round((Date.now() - began) / 1000)}s)`)
|
|
50
|
+
return dst
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** --audio-only: lays the score on the existing render, warning when the render no longer runs as long as the film. */
|
|
54
|
+
function layMusic(job, { meta }, dst) {
|
|
55
|
+
const { p, audio, from, to, log } = job
|
|
56
|
+
if (!existsSync(dst)) die(`--audio-only needs ${p.rel(dst)}; render it first`)
|
|
57
|
+
const dur = mediaLength(dst)
|
|
58
|
+
const stale = staleRender(dur, meta.duration, Boolean(from) || to != null)
|
|
59
|
+
if (stale) log(` warning: ${p.rel(dst)}: ${stale}`)
|
|
60
|
+
if (!audio) return log(` no music to lay on ${p.rel(dst)}`), dst
|
|
61
|
+
const tmp = dst.replace(/\.mp4$/i, '.mux.mp4')
|
|
62
|
+
run('ffmpeg', muxArgs(dst, audio, Number(from) || 0, dur, tmp))
|
|
63
|
+
renameSync(tmp, dst)
|
|
64
|
+
log(` music laid on ${p.rel(dst)}`)
|
|
65
|
+
return dst
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The cover frame as a JPEG; for landscape, also the site's cover (emaki.config.json coversDir) at 1600 px wide. */
|
|
69
|
+
async function saveCovers(job, { page, meta }, call, layout) {
|
|
70
|
+
const { p, slug, locale, log } = job
|
|
71
|
+
await call((t) => window.__show.seek(t), meta.cover)
|
|
72
|
+
const cover = join(job.out, `${slug}-${locale}-cover-${SUFFIX[layout]}.jpg`)
|
|
73
|
+
writeFileSync(cover, await page.screenshot({ type: 'jpeg', quality: 92 }))
|
|
74
|
+
log(` cover: ${p.rel(cover)} (film time ${meta.cover.toFixed(1)} s)`)
|
|
75
|
+
if (layout !== 'landscape' || !p.cfg.coversDir) return
|
|
76
|
+
const siteCover = join(p.root, p.cfg.coversDir, locale === p.site.defaultLocale ? `${slug}.jpg` : `${slug}.${locale}.jpg`)
|
|
77
|
+
mkdirSync(dirname(siteCover), { recursive: true })
|
|
78
|
+
run('ffmpeg', ['-y', '-loglevel', 'error', '-i', cover, '-vf', 'scale=1600:-2', '-q:v', '3', siteCover])
|
|
79
|
+
log(` cover for the site: ${p.rel(siteCover)}`)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** One layout: the frames (or, with --audio-only, the music) as the steps say, then the covers. Returns the video. */
|
|
83
|
+
async function renderLayout(job, browser, site, layout) {
|
|
84
|
+
const show = await openShow(browser, site, job.slug, layout, job.locale)
|
|
85
|
+
const call = (fn, arg) => evaluate(show.page, fn, arg)
|
|
86
|
+
const dst = renderOut(job.out, job.slug, job.locale, layout, { out: job.target, layouts: job.layouts })
|
|
87
|
+
if (job.steps.music && job.audio === undefined) job.audio = await scoreFor(job, call)
|
|
88
|
+
let made = null
|
|
89
|
+
if (job.steps.frames) made = await captureFrames(job, show, call, layout, dst)
|
|
90
|
+
else if (job.steps.music) made = layMusic(job, show, dst)
|
|
91
|
+
await saveCovers(job, show, call, layout)
|
|
92
|
+
await show.page.close()
|
|
93
|
+
return made
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Frame-by-frame capture to MP4 with its music, plus a cover JPEG per orientation (render.py).
|
|
97
|
+
* `from`/`to` capture a range of film time; `out` is a file (one layout) or a folder. `music`: synth (the page's
|
|
98
|
+
* score, saved as score.wav), export (the audio of production/<slug>/<locale>/<slug>.mp4) or none. `audioOnly` lays
|
|
99
|
+
* the music on the existing render without capturing frames; `coverOnly` saves just the covers. */
|
|
100
|
+
export async function render(p, slug, {
|
|
101
|
+
locale, url, o = 'both', fps = 30, from = 0, to = undefined, out: target = undefined, music: mode = 'synth',
|
|
102
|
+
audioOnly = false, coverOnly = false, log = console.info
|
|
103
|
+
} = {}) {
|
|
104
|
+
p.filmDir(slug)
|
|
105
|
+
const steps = renderSteps({ audioOnly, coverOnly })
|
|
106
|
+
const layouts = o === 'both' ? ['landscape', 'portrait'] : [o]
|
|
107
|
+
const out = p.prod(slug, locale, 'render')
|
|
108
|
+
mkdirSync(out, { recursive: true })
|
|
109
|
+
const music = musicFor(mode, { exportFile: p.prod(slug, locale, `${slug}.mp4`), exists: existsSync })
|
|
110
|
+
if (steps.music && music.note) log(` ${music.note}`)
|
|
111
|
+
// What every layout shares; `audio` (the score) is read with the first layout's page and kept.
|
|
112
|
+
const job = { p, slug, locale, steps, music, mode, out, target, layouts: layouts.length, fps, from, to, log, audio: undefined }
|
|
113
|
+
const site = await openSite(p, url), made = []
|
|
114
|
+
const browser = await (await chromium()).launch()
|
|
115
|
+
try {
|
|
116
|
+
for (const layout of layouts) {
|
|
117
|
+
const dst = await renderLayout(job, browser, site, layout)
|
|
118
|
+
if (dst) made.push(dst)
|
|
119
|
+
}
|
|
120
|
+
return made
|
|
121
|
+
} finally {
|
|
122
|
+
await browser.close()
|
|
123
|
+
site.close()
|
|
124
|
+
}
|
|
125
|
+
}
|
package/src/show-page.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The capture page in headless Chromium: the site to capture (a running one, or the dev server started for the run),
|
|
2
|
+
// /show/<slug>?capture=1 opened at a layout's size, and page.evaluate that survives a reload. sheet, render and
|
|
3
|
+
// carousel use it. Playwright is an optional dependency: `npm i -D playwright && npx playwright install chromium`.
|
|
4
|
+
import { spawn } from 'node:child_process'
|
|
5
|
+
import { connect } from 'node:net'
|
|
6
|
+
import { SIZES } from './config.js'
|
|
7
|
+
import { retrying } from './media.js'
|
|
8
|
+
import { die } from './util.js'
|
|
9
|
+
|
|
10
|
+
/** page.evaluate that survives the dev server reloading the page under it (a file saved mid-render): it waits for
|
|
11
|
+
* the show to come back and repeats the same call (render.py's call()). */
|
|
12
|
+
export const evaluate = (page, fn, arg) => retrying(() => page.evaluate(fn, arg), async () => {
|
|
13
|
+
await page.waitForFunction(() => window.__show, null, { timeout: 30000 })
|
|
14
|
+
await page.evaluate(() => window.__show.ready)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export async function chromium() {
|
|
18
|
+
try {
|
|
19
|
+
return (await import('playwright')).chromium
|
|
20
|
+
} catch {
|
|
21
|
+
return die('sheet, render and carousel need Playwright: npm i -D playwright && npx playwright install chromium')
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const portOpen = (port) => new Promise((ok) => {
|
|
26
|
+
const s = connect({ port, host: '127.0.0.1' })
|
|
27
|
+
s.setTimeout(500)
|
|
28
|
+
s.once('connect', () => { s.destroy(); ok(true) })
|
|
29
|
+
s.once('error', () => ok(false))
|
|
30
|
+
s.once('timeout', () => { s.destroy(); ok(false) })
|
|
31
|
+
})
|
|
32
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
33
|
+
|
|
34
|
+
/** The site to capture: `url` if given (must answer), else the dev server (emaki.config.json devCommand), started if
|
|
35
|
+
* nothing listens on the port yet and stopped afterwards. */
|
|
36
|
+
export async function openSite(p, url) {
|
|
37
|
+
const port = p.cfg.port ?? 5199
|
|
38
|
+
let proc = null
|
|
39
|
+
const base = (url ?? `http://localhost:${port}`).replace(/\/$/, '')
|
|
40
|
+
if (url) {
|
|
41
|
+
try {
|
|
42
|
+
await fetch(url, { signal: AbortSignal.timeout(10000) })
|
|
43
|
+
} catch (e) {
|
|
44
|
+
die(`the site at ${url} is not responding (${e.message}); start it, or leave out --url to let emaki start it`)
|
|
45
|
+
}
|
|
46
|
+
} else if (!(await portOpen(port))) {
|
|
47
|
+
const cmd = (p.cfg.devCommand ?? 'npm run dev -- --port {port} --strictPort').replaceAll('{port}', String(port))
|
|
48
|
+
proc = spawn(cmd, { shell: true, cwd: p.root, stdio: 'ignore', detached: process.platform !== 'win32' })
|
|
49
|
+
let up = false
|
|
50
|
+
for (let i = 0; i < 120 && !(up = await portOpen(port)); i++) await sleep(500)
|
|
51
|
+
if (!up) { stop(); die(`the dev server did not start (${cmd})`) }
|
|
52
|
+
}
|
|
53
|
+
function stop() {
|
|
54
|
+
if (!proc) return
|
|
55
|
+
try { process.kill(-proc.pid) } catch { proc.kill() }
|
|
56
|
+
}
|
|
57
|
+
const showPath = p.cfg.showPath ?? '/show/{slug}?capture=1&o={layout}&locale={locale}'
|
|
58
|
+
return {
|
|
59
|
+
base,
|
|
60
|
+
show: (slug, layout, locale) => base + showPath.replaceAll('{slug}', slug).replaceAll('{layout}', layout).replaceAll('{locale}', locale),
|
|
61
|
+
close: stop
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A page on the capture route at the layout's size, once the show is ready; its meta and the errors it reports. */
|
|
66
|
+
export async function openShow(browser, site, slug, layout, locale) {
|
|
67
|
+
const [width, height] = SIZES[layout]
|
|
68
|
+
const page = await browser.newPage({ viewport: { width, height }, deviceScaleFactor: 1 })
|
|
69
|
+
const errors = []
|
|
70
|
+
page.on('pageerror', (e) => errors.push(String(e)))
|
|
71
|
+
await page.goto(site.show(slug, layout, locale))
|
|
72
|
+
await page.waitForFunction(() => window.__show, null, { timeout: 30000 })
|
|
73
|
+
await page.evaluate(() => window.__show.ready)
|
|
74
|
+
return { page, meta: await page.evaluate(() => window.__show.meta), errors }
|
|
75
|
+
}
|
package/src/stage.js
CHANGED
|
@@ -68,40 +68,53 @@ export function postsFor(p, slug, locale) {
|
|
|
68
68
|
|
|
69
69
|
const sha = (bytes) => createHash('sha256').update(bytes).digest('hex')
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
/** --channels (a list, or comma-separated) or every channel; an unknown name is refused. */
|
|
72
|
+
function wantedChannels(channels) {
|
|
73
73
|
const want = channels ? [channels].flat().flatMap((c) => String(c).split(',')).map((c) => c.trim()).filter(Boolean) : [...CHANNELS]
|
|
74
74
|
for (const c of want) if (!CHANNELS.includes(c)) die(`unknown channel "${c}" (${CHANNELS.join(', ')})`)
|
|
75
|
+
return want
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Uploads a channel's files to the desk (once per key across channels; PNG as JPEG) and lists them with their hashes.
|
|
79
|
+
* Without a desk (dry run) nothing is sent and each hash is computed here. */
|
|
80
|
+
async function channelMedia(files, { slug, channel, desk, uploaded, toJpeg }) {
|
|
81
|
+
const media = []
|
|
82
|
+
for (const x of files) {
|
|
83
|
+
const path = x.path.endsWith('.png') ? toJpeg(x.path) : x.path
|
|
84
|
+
const name = (x.name ?? basename(path)).toLowerCase(), type = typeOf(path)
|
|
85
|
+
const key = `${slug}/${type === 'image/jpeg' ? `${channel.split('_')[0]}-${name}` : name}`
|
|
86
|
+
const bytes = readFileSync(path)
|
|
87
|
+
if (desk && !uploaded.has(key)) uploaded.set(key, await desk.putMedia(key, bytes, type))
|
|
88
|
+
media.push({ key, sha256: uploaded.get(key) ?? sha(bytes), type })
|
|
89
|
+
}
|
|
90
|
+
return media
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** One channel: skipped when a file is missing, printed on a dry run (no desk), else staged on the desk. */
|
|
94
|
+
async function stageChannel(p, slug, channel, [text, files], ctx) {
|
|
95
|
+
const { desk, log, result } = ctx
|
|
96
|
+
const missing = files.filter((x) => !x.path).map((x) => p.rel(x.expect))
|
|
97
|
+
if (missing.length) {
|
|
98
|
+
log(` ${channel}: skipped, missing ${missing.join(', ')}`)
|
|
99
|
+
result.skipped.push({ channel, missing })
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
const media = await channelMedia(files, { slug, channel, ...ctx })
|
|
103
|
+
const post = { ...text, media }
|
|
104
|
+
result.posts[channel] = post
|
|
105
|
+
if (!desk) return log(`--- ${channel}\n${JSON.stringify(post, null, 2)}`)
|
|
106
|
+
const r = await desk.stage(slug, channel, post)
|
|
107
|
+
log(` ${channel}: staged (${String(r.content_sha).slice(0, 10)}), ${media.length} file(s)`)
|
|
108
|
+
result.staged.push({ channel, content_sha: r.content_sha, files: media.length })
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function stage(p, slug, { locale, channels, dryRun = false, env = process.env, fetch = globalThis.fetch, log = console.info, toJpeg = pngToJpeg } = {}) {
|
|
112
|
+
locale ??= p.site.defaultLocale
|
|
113
|
+
const want = wantedChannels(channels)
|
|
75
114
|
const posts = postsFor(p, slug, locale)
|
|
76
115
|
const desk = dryRun ? null : deskClient(p, { env, fetch })
|
|
77
|
-
const
|
|
78
|
-
for (const channel of CHANNELS.filter((c) => want.includes(c) && posts[c]))
|
|
79
|
-
const [text, files] = posts[channel]
|
|
80
|
-
const missing = files.filter((x) => !x.path).map((x) => p.rel(x.expect))
|
|
81
|
-
if (missing.length) {
|
|
82
|
-
log(` ${channel}: skipped, missing ${missing.join(', ')}`)
|
|
83
|
-
result.skipped.push({ channel, missing })
|
|
84
|
-
continue
|
|
85
|
-
}
|
|
86
|
-
const media = []
|
|
87
|
-
for (const x of files) {
|
|
88
|
-
const path = x.path.endsWith('.png') ? toJpeg(x.path) : x.path
|
|
89
|
-
const name = (x.name ?? basename(path)).toLowerCase(), type = typeOf(path)
|
|
90
|
-
const key = `${slug}/${type === 'image/jpeg' ? `${channel.split('_')[0]}-${name}` : name}`
|
|
91
|
-
const bytes = readFileSync(path)
|
|
92
|
-
if (desk && !uploaded.has(key)) uploaded.set(key, await desk.putMedia(key, bytes, type))
|
|
93
|
-
media.push({ key, sha256: uploaded.get(key) ?? sha(bytes), type })
|
|
94
|
-
}
|
|
95
|
-
const post = { ...text, media }
|
|
96
|
-
result.posts[channel] = post
|
|
97
|
-
if (!desk) {
|
|
98
|
-
log(`--- ${channel}\n${JSON.stringify(post, null, 2)}`)
|
|
99
|
-
continue
|
|
100
|
-
}
|
|
101
|
-
const r = await desk.stage(slug, channel, post)
|
|
102
|
-
log(` ${channel}: staged (${String(r.content_sha).slice(0, 10)}), ${media.length} file(s)`)
|
|
103
|
-
result.staged.push({ channel, content_sha: r.content_sha, files: media.length })
|
|
104
|
-
}
|
|
116
|
+
const ctx = { desk, log, toJpeg, uploaded: new Map(), result: { staged: [], skipped: [], posts: {} } }
|
|
117
|
+
for (const channel of CHANNELS.filter((c) => want.includes(c) && posts[c])) await stageChannel(p, slug, channel, posts[channel], ctx)
|
|
105
118
|
if (desk) log(`Review and approve on ${desk.origin}/admin`)
|
|
106
|
-
return result
|
|
119
|
+
return ctx.result
|
|
107
120
|
}
|
package/src/takes.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The recorded or synthesised takes of a film in a locale: production/<slug>/<locale>/takes/take-NN.<ext>, one per
|
|
2
|
+
// voice line, in voice/<locale>.json order.
|
|
3
|
+
import { existsSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { load, pad2 } from './util.js'
|
|
6
|
+
|
|
7
|
+
/** scene id → [take file, voice line] for the takes that exist. */
|
|
8
|
+
export function takesFor(p, slug, locale) {
|
|
9
|
+
const d = p.filmDir(slug), lines = load(join(d, 'voice', `${locale}.json`)), folder = p.prod(slug, locale, 'takes')
|
|
10
|
+
const out = new Map()
|
|
11
|
+
lines.forEach((l, i) => {
|
|
12
|
+
for (const ext of ['wav', 'mp3', 'm4a', 'aiff']) {
|
|
13
|
+
const f = join(folder, `take-${pad2(i + 1)}.${ext}`)
|
|
14
|
+
if (existsSync(f)) { out.set(l.scene, [f, l]); break }
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
return out
|
|
18
|
+
}
|
package/src/tts-cloud.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The cloud text-to-speech providers: ElevenLabs, OpenAI (or Azure OpenAI), Azure AI Speech, Google Cloud and Amazon
|
|
2
|
+
// Polly. Keys come from environment variables only. check(cfg, locale) throws TTSError when not ready.
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { env, fail, get, inTemp, lang, post, sh, toTake } from './tts-io.js'
|
|
6
|
+
import { which } from './util.js'
|
|
7
|
+
|
|
8
|
+
const elevenlabs = {
|
|
9
|
+
label: 'ElevenLabs',
|
|
10
|
+
check(cfg) {
|
|
11
|
+
env('ELEVENLABS_API_KEY')
|
|
12
|
+
if (!cfg.voice_id && !cfg.voice) fail('set "voice_id"')
|
|
13
|
+
},
|
|
14
|
+
async synth(text, dst, cfg, locale, ctx) {
|
|
15
|
+
const body = {
|
|
16
|
+
text, model_id: cfg.model ?? 'eleven_multilingual_v2', language_code: lang(locale),
|
|
17
|
+
voice_settings: { stability: cfg.stability ?? 0.55, similarity_boost: cfg.similarity ?? 0.8 }
|
|
18
|
+
}
|
|
19
|
+
if (ctx.prev) body.previous_text = ctx.prev
|
|
20
|
+
if (ctx.next) body.next_text = ctx.next
|
|
21
|
+
const audio = await post(`https://api.elevenlabs.io/v1/text-to-speech/${cfg.voice_id ?? cfg.voice}?output_format=mp3_44100_128`, body,
|
|
22
|
+
{ 'xi-api-key': env('ELEVENLABS_API_KEY'), 'content-type': 'application/json' })
|
|
23
|
+
toTake(audio, dst)
|
|
24
|
+
},
|
|
25
|
+
async voices() {
|
|
26
|
+
const data = await get('https://api.elevenlabs.io/v1/voices', { 'xi-api-key': env('ELEVENLABS_API_KEY') })
|
|
27
|
+
return (data.voices ?? []).map((v) => `${v.voice_id} ${v.name} ${JSON.stringify(v.labels ?? {})}`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const openai = {
|
|
32
|
+
label: 'OpenAI (or Azure OpenAI)',
|
|
33
|
+
check() {
|
|
34
|
+
if (process.env.AZURE_OPENAI_ENDPOINT) env('AZURE_OPENAI_API_KEY')
|
|
35
|
+
else env('OPENAI_API_KEY')
|
|
36
|
+
},
|
|
37
|
+
async synth(text, dst, cfg) {
|
|
38
|
+
const body = { model: cfg.model ?? 'gpt-4o-mini-tts', voice: cfg.voice ?? 'alloy', input: text, response_format: 'wav' }
|
|
39
|
+
if (cfg.instructions) body.instructions = cfg.instructions // e.g. "Calm, warm, unhurried explainer."
|
|
40
|
+
let url, headers
|
|
41
|
+
if (process.env.AZURE_OPENAI_ENDPOINT) {
|
|
42
|
+
url = `${process.env.AZURE_OPENAI_ENDPOINT.replace(/\/$/, '')}/openai/deployments/${cfg.deployment ?? body.model}/audio/speech?api-version=${cfg.api_version ?? '2025-03-01-preview'}`
|
|
43
|
+
headers = { 'api-key': env('AZURE_OPENAI_API_KEY'), 'content-type': 'application/json' }
|
|
44
|
+
} else {
|
|
45
|
+
url = 'https://api.openai.com/v1/audio/speech'
|
|
46
|
+
headers = { authorization: `Bearer ${env('OPENAI_API_KEY')}`, 'content-type': 'application/json' }
|
|
47
|
+
}
|
|
48
|
+
toTake(await post(url, body, headers), dst)
|
|
49
|
+
},
|
|
50
|
+
voices: async () => ['alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse (all speak every supported language; set "instructions" for tone)']
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const xml = (s) => s.replace(/[<>&'"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' })[c])
|
|
54
|
+
const azure = {
|
|
55
|
+
label: 'Azure AI Speech',
|
|
56
|
+
check(cfg) {
|
|
57
|
+
env('AZURE_SPEECH_KEY'); env('AZURE_SPEECH_REGION')
|
|
58
|
+
if (!cfg.voice) fail('set "voice", e.g. "en-US-AvaMultilingualNeural" or "es-ES-ElviraNeural"')
|
|
59
|
+
},
|
|
60
|
+
async synth(text, dst, cfg, locale) {
|
|
61
|
+
const ssml = `<speak version='1.0' xml:lang='${locale}'><voice name='${cfg.voice}'><prosody rate='${cfg.rate ?? '0%'}'>${xml(text)}</prosody></voice></speak>`
|
|
62
|
+
const audio = await post(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/v1`, ssml, {
|
|
63
|
+
'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY'), 'content-type': 'application/ssml+xml',
|
|
64
|
+
'X-Microsoft-OutputFormat': 'riff-48khz-16bit-mono-pcm', 'user-agent': 'emaki'
|
|
65
|
+
})
|
|
66
|
+
toTake(audio, dst)
|
|
67
|
+
},
|
|
68
|
+
async voices(cfg, locale) {
|
|
69
|
+
const data = await get(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/voices/list`, { 'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY') })
|
|
70
|
+
return data.filter((v) => v.Locale.startsWith(locale) || v.Locale.split('-')[0] === lang(locale)).map((v) => `${v.ShortName} ${v.Gender}`)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function googleAuth() {
|
|
75
|
+
if (process.env.GOOGLE_TTS_API_KEY) return { 'X-Goog-Api-Key': process.env.GOOGLE_TTS_API_KEY }
|
|
76
|
+
if (which('gcloud')) {
|
|
77
|
+
const tok = spawnSync('gcloud', ['auth', 'print-access-token'], { encoding: 'utf-8' }).stdout?.trim()
|
|
78
|
+
if (tok) return { authorization: `Bearer ${tok}` }
|
|
79
|
+
}
|
|
80
|
+
return fail('set GOOGLE_TTS_API_KEY, or sign in with `gcloud auth login`')
|
|
81
|
+
}
|
|
82
|
+
const google = {
|
|
83
|
+
label: 'Google Cloud Text-to-Speech',
|
|
84
|
+
check(cfg) {
|
|
85
|
+
googleAuth()
|
|
86
|
+
if (!cfg.voice) fail('set "voice", e.g. "en-US-Neural2-F" or "es-ES-Chirp3-HD-Aoede"')
|
|
87
|
+
},
|
|
88
|
+
async synth(text, dst, cfg) {
|
|
89
|
+
const body = {
|
|
90
|
+
input: { text }, voice: { languageCode: cfg.voice.split('-').slice(0, 2).join('-'), name: cfg.voice },
|
|
91
|
+
audioConfig: { audioEncoding: 'LINEAR16', sampleRateHertz: 48000, speakingRate: cfg.speed ?? 1 }
|
|
92
|
+
}
|
|
93
|
+
const data = await post('https://texttospeech.googleapis.com/v1/text:synthesize', body, { ...googleAuth(), 'content-type': 'application/json' }, 'json')
|
|
94
|
+
toTake(Buffer.from(data.audioContent, 'base64'), dst)
|
|
95
|
+
},
|
|
96
|
+
async voices(cfg, locale) {
|
|
97
|
+
const data = await get(`https://texttospeech.googleapis.com/v1/voices?languageCode=${locale}`, googleAuth())
|
|
98
|
+
return (data.voices ?? []).map((v) => `${v.name} ${v.ssmlGender}`)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const polly = {
|
|
103
|
+
label: 'Amazon Polly',
|
|
104
|
+
check(cfg) {
|
|
105
|
+
if (!which('aws')) fail('install the AWS CLI and sign in (aws configure / SSO); credentials stay in your AWS profile')
|
|
106
|
+
if (!cfg.voice) fail('set "voice", e.g. "Joanna" (en-US) or "Lucia" (es-ES)')
|
|
107
|
+
},
|
|
108
|
+
async synth(text, dst, cfg, locale) {
|
|
109
|
+
inTemp((d) => {
|
|
110
|
+
const raw = join(d, 'raw.mp3')
|
|
111
|
+
sh('aws', ['polly', 'synthesize-speech', '--output-format', 'mp3', '--voice-id', cfg.voice, '--engine', cfg.engine ?? 'neural',
|
|
112
|
+
'--language-code', cfg.language ?? locale, '--text', text, ...(cfg.region ? ['--region', cfg.region] : []), raw])
|
|
113
|
+
toTake(raw, dst)
|
|
114
|
+
})
|
|
115
|
+
},
|
|
116
|
+
async voices(cfg, locale) {
|
|
117
|
+
const out = JSON.parse(String(sh('aws', ['polly', 'describe-voices', '--language-code', cfg.language ?? locale, '--engine', cfg.engine ?? 'neural', ...(cfg.region ? ['--region', cfg.region] : [])])))
|
|
118
|
+
return out.Voices.map((v) => `${v.Id} ${v.Gender}`)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { elevenlabs, openai, azure, google, polly }
|