@senseihq/emaki-cli 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +69 -2
- package/package.json +44 -4
- package/plugin/.claude-plugin/plugin.json +19 -0
- package/plugin/PLAYBOOK.md +137 -0
- package/plugin/README.md +51 -0
- package/plugin/agents/fact-checker.md +28 -0
- package/plugin/agents/localizer.md +34 -0
- package/plugin/agents/scene-builder.md +43 -0
- package/plugin/agents/story-planner.md +42 -0
- package/plugin/agents/story-writer.md +34 -0
- package/plugin/agents/visual-reviewer.md +33 -0
- package/plugin/modules/campaign/README.md +30 -0
- package/plugin/modules/campaign/calendar.csv +2 -0
- package/plugin/modules/campaign/day-script.template.md +42 -0
- package/plugin/modules/posting-desk/README.md +37 -0
- package/plugin/skills/emaki/SKILL.md +107 -0
- package/plugin/skills/emaki/references/01-interview.md +68 -0
- package/plugin/skills/emaki/references/02-architecture.md +69 -0
- package/plugin/skills/emaki/references/03-content-model.md +68 -0
- package/plugin/skills/emaki/references/04-scene-authoring.md +99 -0
- package/plugin/skills/emaki/references/05-voice-and-video.md +97 -0
- package/plugin/skills/emaki/references/06-i18n.md +37 -0
- package/plugin/skills/emaki/references/07-quality-gates.md +44 -0
- package/plugin/skills/emaki/references/08-feedback-loop.md +32 -0
- package/plugin/skills/emaki/references/09-publishing.md +14 -0
- package/plugin/skills/story-build/SKILL.md +37 -0
- package/plugin/skills/story-localize/SKILL.md +24 -0
- package/plugin/skills/story-media/SKILL.md +38 -0
- package/plugin/skills/story-publish/SKILL.md +36 -0
- package/plugin/skills/story-review/SKILL.md +29 -0
- package/plugin/templates/brief.md +32 -0
- package/plugin/templates/change-request.md +7 -0
- package/plugin/templates/changelog.md +4 -0
- package/plugin/templates/decisions.md +5 -0
- package/plugin/templates/glossary.md +4 -0
- package/plugin/templates/item-card.md +34 -0
- package/plugin/templates/outline.md +19 -0
- package/plugin/templates/review-sheet.md +12 -0
- package/plugin/templates/style-guide.md +21 -0
- package/plugin/templates/tracker.md +11 -0
- package/src/api.js +20 -0
- package/src/campaign.js +157 -0
- package/src/capture.js +205 -0
- package/src/carousel.js +91 -0
- package/src/config.js +32 -0
- package/src/day-script.js +66 -0
- package/src/desk-client.js +37 -0
- package/src/desk-plan.js +57 -0
- package/src/films.js +152 -0
- package/src/http.js +30 -0
- package/src/index.js +353 -0
- package/src/init.js +110 -0
- package/src/media.js +236 -0
- package/src/migrate.js +67 -0
- package/src/plugin.js +106 -0
- package/src/publish.js +118 -0
- package/src/stage.js +107 -0
- package/src/stories.js +66 -0
- package/src/tts.js +290 -0
- package/src/upload/instagram.js +34 -0
- package/src/upload/youtube.js +90 -0
- package/src/util.js +66 -0
- package/src/voice.js +295 -0
- package/src/youtube-text.js +58 -0
- package/templates/content/films/index.json +1 -0
- package/templates/content/films/measure-the-right-work/film.json +68 -0
- package/templates/content/films/measure-the-right-work/look.css +17 -0
- package/templates/content/films/measure-the-right-work/scene-close.json +153 -0
- package/templates/content/films/measure-the-right-work/scene-groups.json +99 -0
- package/templates/content/films/measure-the-right-work/scene-hook.json +96 -0
- package/templates/content/films/measure-the-right-work/scene-pair.json +190 -0
- package/templates/content/films/measure-the-right-work/scene-problem.json +162 -0
- package/templates/content/films/measure-the-right-work/score.js +19 -0
- package/templates/content/films/measure-the-right-work/strings/en.json +50 -0
- package/templates/content/films/measure-the-right-work/strings/es.json +50 -0
- package/templates/content/films/measure-the-right-work/timing.en.json +24 -0
- package/templates/content/films/measure-the-right-work/timing.es.json +24 -0
- package/templates/content/films/measure-the-right-work/voice/en/close.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/en/groups.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/en/hook.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/en/pair.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/en/problem.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/en.json +7 -0
- package/templates/content/films/measure-the-right-work/voice/es/close.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/es/groups.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/es/hook.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/es/pair.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/es/problem.m4a +0 -0
- package/templates/content/films/measure-the-right-work/voice/es.json +7 -0
- package/templates/content/logos/brand.svg +1 -0
- package/templates/content/site.json +10 -0
- package/templates/content/stories/en/measure-the-right-work.json +13 -0
- package/templates/content/stories/es/measure-the-right-work.json +13 -0
- package/templates/content/stories/index.json +12 -0
- package/templates/content/ui/en.json +1 -0
- package/templates/content/ui/es.json +1 -0
- package/templates/emaki.config.json +67 -0
- package/templates/film/look.css +17 -0
- package/templates/film/scene-close.json +153 -0
- package/templates/film/scene-template.json +71 -0
- package/templates/film/score.js +12 -0
package/src/stories.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// A story as the media pipeline needs it: its index entry with the reader-facing text of one locale, its public
|
|
2
|
+
// link, the built files of its film, and the links of its posts written back into the stories index.
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { SUFFIX } from './config.js'
|
|
5
|
+
import { die, exists, load, save } from './util.js'
|
|
6
|
+
|
|
7
|
+
export const storiesIndex = (p) => join(p.content, 'stories', 'index.json')
|
|
8
|
+
|
|
9
|
+
/** The index entry merged with title/dek/… from stories/<locale>/<slug>.json (else the default locale's). */
|
|
10
|
+
export function storyFor(p, slug, locale) {
|
|
11
|
+
const ip = storiesIndex(p)
|
|
12
|
+
const entry = exists(ip) ? load(ip).find((e) => e.slug === slug) : null
|
|
13
|
+
if (!entry) die(`${slug} is not in ${p.rel(ip)}`)
|
|
14
|
+
const body = [locale, p.site.defaultLocale].map((l) => join(p.content, 'stories', l, `${slug}.json`)).find(exists)
|
|
15
|
+
const text = body ? load(body) : {}
|
|
16
|
+
const film = exists(join(p.films, slug, 'film.json')) ? load(join(p.films, slug, 'film.json')) : {}
|
|
17
|
+
return { ...entry, title: text.title ?? entry.title ?? film.title ?? slug, dek: text.dek ?? entry.dek ?? '', tags: entry.tags ?? [] }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The public link of a story: emaki.config.json "storyUrl" ({url}, {locale}, {slug}), default {url}/{locale}/stories/{slug}. */
|
|
21
|
+
export function storyUrl(p, slug, locale) {
|
|
22
|
+
const tpl = p.cfg.storyUrl ?? '{url}/{locale}/stories/{slug}'
|
|
23
|
+
return tpl.replaceAll('{url}', (p.site.url ?? '').replace(/\/$/, '')).replaceAll('{locale}', locale).replaceAll('{slug}', slug)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** #tag #tag from a story's first five tags (dashes dropped). */
|
|
27
|
+
export const hashtags = (tags) => tags.slice(0, 5).map((t) => '#' + t.replace(/-/g, '')).join(' ')
|
|
28
|
+
|
|
29
|
+
/** What `emaki build` and `emaki render` made for a film in one locale; a missing file is null. */
|
|
30
|
+
export function filmFiles(p, slug, locale) {
|
|
31
|
+
const out = p.prod(slug, locale, 'out'), render = p.prod(slug, locale, 'render')
|
|
32
|
+
const has = (f) => (exists(f) ? f : null)
|
|
33
|
+
return {
|
|
34
|
+
wide: has(join(out, `${slug}-${locale}-${SUFFIX.landscape}.mp4`)),
|
|
35
|
+
tall: has(join(out, `${slug}-${locale}-${SUFFIX.portrait}.mp4`)),
|
|
36
|
+
srt: has(join(out, `${slug}-${locale}.srt`)),
|
|
37
|
+
thumb: has(join(render, `${slug}-${locale}-cover-${SUFFIX.landscape}.jpg`)),
|
|
38
|
+
coverTall: has(join(render, `${slug}-${locale}-cover-${SUFFIX.portrait}.jpg`)),
|
|
39
|
+
description: has(join(out, 'description.txt')),
|
|
40
|
+
expect: {
|
|
41
|
+
wide: join(out, `${slug}-${locale}-${SUFFIX.landscape}.mp4`),
|
|
42
|
+
tall: join(out, `${slug}-${locale}-${SUFFIX.portrait}.mp4`)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Writes post links into the story's index entry: `youtube` (the id) and social.youtube / social.instagram / social.x.
|
|
49
|
+
* Without `overwrite`, an existing id or link is never replaced. A Short (entry.video === 'short') links /shorts/.
|
|
50
|
+
*/
|
|
51
|
+
export function recordLinks(p, slug, { youtube, instagram, x } = {}, { overwrite = false } = {}) {
|
|
52
|
+
const ip = storiesIndex(p), index = load(ip)
|
|
53
|
+
const entry = index.find((e) => e.slug === slug)
|
|
54
|
+
if (!entry) die(`${slug} is not in ${p.rel(ip)}`)
|
|
55
|
+
const social = (entry.social ??= {})
|
|
56
|
+
const set = (obj, k, v) => { if (v && (overwrite || !obj[k])) obj[k] = v }
|
|
57
|
+
if (youtube && !/^https?:/.test(youtube)) {
|
|
58
|
+
set(entry, 'youtube', youtube)
|
|
59
|
+
youtube = entry.video === 'short' ? `https://www.youtube.com/shorts/${youtube}` : `https://www.youtube.com/watch?v=${youtube}`
|
|
60
|
+
}
|
|
61
|
+
set(social, 'youtube', youtube)
|
|
62
|
+
set(social, 'instagram', instagram)
|
|
63
|
+
set(social, 'x', x)
|
|
64
|
+
save(ip, index)
|
|
65
|
+
return entry
|
|
66
|
+
}
|
package/src/tts.js
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// Text-to-speech providers for the voice step. Pick one per locale in emaki.config.json (or `emaki set-voice`):
|
|
2
|
+
//
|
|
3
|
+
// "voice": {
|
|
4
|
+
// "default": { "provider": "piper", "model": "voices/en_US-lessac-medium.onnx" },
|
|
5
|
+
// "es": { "provider": "azure", "voice": "es-ES-ElviraNeural" }
|
|
6
|
+
// }
|
|
7
|
+
//
|
|
8
|
+
// Every provider writes a 48 kHz mono WAV with 0.1 s of air at the head (what fit and build expect). Keys come from
|
|
9
|
+
// environment variables only, never from the config file.
|
|
10
|
+
//
|
|
11
|
+
// | provider | runs | needs |
|
|
12
|
+
// |------------|------------------|-----------------------------------------------------------------|
|
|
13
|
+
// | piper | offline, any OS | `piper` (pip install piper-tts) + a voice .onnx/.onnx.json |
|
|
14
|
+
// | kokoro | offline, any OS | python3 with kokoro + soundfile (pip install kokoro soundfile) |
|
|
15
|
+
// | macos | offline, macOS | the built-in `say` |
|
|
16
|
+
// | espeak | offline, any OS | espeak-ng (robotic: timing drafts) |
|
|
17
|
+
// | elevenlabs | cloud | ELEVENLABS_API_KEY |
|
|
18
|
+
// | openai | cloud | OPENAI_API_KEY (Azure OpenAI: AZURE_OPENAI_ENDPOINT + AZURE_OPENAI_API_KEY) |
|
|
19
|
+
// | azure | cloud (Microsoft)| AZURE_SPEECH_KEY + AZURE_SPEECH_REGION |
|
|
20
|
+
// | google | cloud (Google) | GOOGLE_TTS_API_KEY, or `gcloud auth login` |
|
|
21
|
+
// | polly | cloud (AWS) | the `aws` CLI with credentials |
|
|
22
|
+
// | manual | a person | recorded take-NN.wav files |
|
|
23
|
+
import { spawnSync } from 'node:child_process'
|
|
24
|
+
import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
25
|
+
import { tmpdir } from 'node:os'
|
|
26
|
+
import { join } from 'node:path'
|
|
27
|
+
import { which } from './util.js'
|
|
28
|
+
|
|
29
|
+
export class TTSError extends Error {}
|
|
30
|
+
const fail = (m) => { throw new TTSError(m) }
|
|
31
|
+
const lang = (locale) => locale.split('-')[0]
|
|
32
|
+
|
|
33
|
+
function sh(cmd, args, input) {
|
|
34
|
+
const r = spawnSync(cmd, args, { input, encoding: input == null ? 'utf-8' : undefined, maxBuffer: 256 * 1024 * 1024 })
|
|
35
|
+
if (r.error) fail(`${cmd}: ${r.error.code === 'ENOENT' ? 'not installed' : r.error.message}`)
|
|
36
|
+
if (r.status) fail(`${cmd}: ${String(r.stderr ?? '').slice(-400)}`)
|
|
37
|
+
return r.stdout
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Any audio file or bytes → 48 kHz mono WAV with 0.1 s head air at dst. */
|
|
41
|
+
export function toTake(src, dst) {
|
|
42
|
+
const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
|
|
43
|
+
try {
|
|
44
|
+
let file = src
|
|
45
|
+
if (typeof src !== 'string') { file = join(dir, 'raw'); writeFileSync(file, src) }
|
|
46
|
+
sh('ffmpeg', ['-v', 'error', '-y', '-i', file, '-af', 'adelay=100|100', '-ar', '48000', '-ac', '1', dst])
|
|
47
|
+
} finally {
|
|
48
|
+
rmSync(dir, { recursive: true, force: true })
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function inTemp(fn) {
|
|
52
|
+
const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
|
|
53
|
+
try { return fn(dir) } finally { rmSync(dir, { recursive: true, force: true }) }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function env(...names) {
|
|
57
|
+
for (const n of names) if (process.env[n]) return process.env[n]
|
|
58
|
+
return fail(`set ${names.join(' or ')} in the environment (never in files)`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function post(url, body, headers, as = 'bytes') {
|
|
62
|
+
const r = await fetch(url, { method: 'POST', body: typeof body === 'string' || body instanceof Uint8Array ? body : JSON.stringify(body), headers })
|
|
63
|
+
if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status} ${(await r.text()).slice(0, 300)}`)
|
|
64
|
+
return as === 'json' ? r.json() : Buffer.from(await r.arrayBuffer())
|
|
65
|
+
}
|
|
66
|
+
async function get(url, headers) {
|
|
67
|
+
const r = await fetch(url, { headers })
|
|
68
|
+
if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status}`)
|
|
69
|
+
return r.json()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ── Providers: check(cfg, locale) throws TTSError when not ready; synth(text, dst, cfg, locale, ctx) ─────────────
|
|
73
|
+
|
|
74
|
+
const piper = {
|
|
75
|
+
label: 'Piper (offline)',
|
|
76
|
+
check(cfg) {
|
|
77
|
+
if (!which('piper')) fail('install it: pip install piper-tts')
|
|
78
|
+
if (!cfg.model || !existsSync(cfg.model)) fail(`voice model not found: "${cfg.model ?? ''}". Download a voice (.onnx and .onnx.json) from the Piper voices list and set "model"`)
|
|
79
|
+
},
|
|
80
|
+
async synth(text, dst, cfg) {
|
|
81
|
+
inTemp((d) => {
|
|
82
|
+
const raw = join(d, 'raw.wav')
|
|
83
|
+
const args = ['--model', cfg.model, '--output_file', raw]
|
|
84
|
+
if (cfg.speaker != null) args.push('--speaker', String(cfg.speaker))
|
|
85
|
+
if (cfg.length_scale) args.push('--length_scale', String(cfg.length_scale)) // >1 is slower
|
|
86
|
+
sh('piper', args, text)
|
|
87
|
+
if (!existsSync(raw)) fail('piper wrote nothing')
|
|
88
|
+
toTake(raw, dst)
|
|
89
|
+
})
|
|
90
|
+
},
|
|
91
|
+
voices: async () => ['Piper voices are files: pick one for this language from the Piper voice samples page, download its .onnx and .onnx.json, set "model".']
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const KOKORO_LANG = { en: 'a', 'en-GB': 'b', es: 'e', fr: 'f', hi: 'h', it: 'i', ja: 'j', pt: 'p', zh: 'z' }
|
|
95
|
+
const KOKORO_PY = `import sys, json, numpy as np, soundfile as sf
|
|
96
|
+
from kokoro import KPipeline
|
|
97
|
+
a = json.loads(sys.argv[1]); text = sys.stdin.read()
|
|
98
|
+
pipe = KPipeline(lang_code=a['code'])
|
|
99
|
+
audio = [x for _, _, x in pipe(text, voice=a['voice'], speed=a['speed'])]
|
|
100
|
+
sf.write(a['out'], np.concatenate(audio), 24000)`
|
|
101
|
+
const kokoro = {
|
|
102
|
+
label: 'Kokoro (offline)',
|
|
103
|
+
check(cfg, locale) {
|
|
104
|
+
const py = cfg.python ?? 'python3'
|
|
105
|
+
const r = spawnSync(py, ['-c', 'import kokoro, soundfile'], { encoding: 'utf-8' })
|
|
106
|
+
if (r.error || r.status) fail(`install it: ${py} -m pip install kokoro soundfile (and espeak-ng for some languages)`)
|
|
107
|
+
if (!KOKORO_LANG[locale] && !KOKORO_LANG[lang(locale)]) fail(`kokoro has no voices for ${locale}`)
|
|
108
|
+
},
|
|
109
|
+
async synth(text, dst, cfg, locale) {
|
|
110
|
+
inTemp((d) => {
|
|
111
|
+
const out = join(d, 'raw.wav')
|
|
112
|
+
const code = cfg.lang_code ?? KOKORO_LANG[locale] ?? KOKORO_LANG[lang(locale)]
|
|
113
|
+
sh(cfg.python ?? 'python3', ['-c', KOKORO_PY, JSON.stringify({ code, voice: cfg.voice ?? 'af_heart', speed: cfg.speed ?? 1, out })], text)
|
|
114
|
+
toTake(out, dst)
|
|
115
|
+
})
|
|
116
|
+
},
|
|
117
|
+
voices: async () => ['af_heart, af_bella, am_michael, bf_emma, bm_george (en); ef_dora, em_alex (es); ff_siwis (fr); if_sara (it); pf_dora (pt); jf_alpha (ja); zf_xiaobei (zh); see the Kokoro VOICES list']
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const macos = {
|
|
121
|
+
label: 'macOS say (offline, drafts)',
|
|
122
|
+
check() { if (!which('say')) fail('`say` exists only on macOS') },
|
|
123
|
+
async synth(text, dst, cfg) {
|
|
124
|
+
inTemp((d) => {
|
|
125
|
+
const raw = join(d, 'raw.aiff')
|
|
126
|
+
sh('say', ['-o', raw, ...(cfg.voice ? ['-v', cfg.voice] : []), ...(cfg.rate ? ['-r', String(cfg.rate)] : []), text])
|
|
127
|
+
toTake(raw, dst)
|
|
128
|
+
})
|
|
129
|
+
},
|
|
130
|
+
async voices(cfg, locale) {
|
|
131
|
+
const all = String(sh('say', ['-v', '?'])).split('\n').filter(Boolean)
|
|
132
|
+
const l = lang(locale), mine = all.filter((x) => x.includes(` ${l}_`) || x.includes(` ${l}-`))
|
|
133
|
+
return mine.length ? mine : all.slice(0, 40)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const espeak = {
|
|
138
|
+
label: 'eSpeak NG (offline, robotic)',
|
|
139
|
+
check() { if (!which('espeak-ng')) fail('install espeak-ng (brew/apt/choco)') },
|
|
140
|
+
async synth(text, dst, cfg, locale) {
|
|
141
|
+
inTemp((d) => {
|
|
142
|
+
const raw = join(d, 'raw.wav')
|
|
143
|
+
sh('espeak-ng', ['-v', cfg.voice ?? locale, '-s', String(cfg.speed ?? 160), '-w', raw, text])
|
|
144
|
+
toTake(raw, dst)
|
|
145
|
+
})
|
|
146
|
+
},
|
|
147
|
+
voices: async (cfg, locale) => String(sh('espeak-ng', [`--voices=${lang(locale)}`])).split('\n').filter(Boolean)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const elevenlabs = {
|
|
151
|
+
label: 'ElevenLabs',
|
|
152
|
+
check(cfg) {
|
|
153
|
+
env('ELEVENLABS_API_KEY')
|
|
154
|
+
if (!cfg.voice_id && !cfg.voice) fail('set "voice_id"')
|
|
155
|
+
},
|
|
156
|
+
async synth(text, dst, cfg, locale, ctx) {
|
|
157
|
+
const body = {
|
|
158
|
+
text, model_id: cfg.model ?? 'eleven_multilingual_v2', language_code: lang(locale),
|
|
159
|
+
voice_settings: { stability: cfg.stability ?? 0.55, similarity_boost: cfg.similarity ?? 0.8 }
|
|
160
|
+
}
|
|
161
|
+
if (ctx.prev) body.previous_text = ctx.prev
|
|
162
|
+
if (ctx.next) body.next_text = ctx.next
|
|
163
|
+
const audio = await post(`https://api.elevenlabs.io/v1/text-to-speech/${cfg.voice_id ?? cfg.voice}?output_format=mp3_44100_128`, body,
|
|
164
|
+
{ 'xi-api-key': env('ELEVENLABS_API_KEY'), 'content-type': 'application/json' })
|
|
165
|
+
toTake(audio, dst)
|
|
166
|
+
},
|
|
167
|
+
async voices() {
|
|
168
|
+
const data = await get('https://api.elevenlabs.io/v1/voices', { 'xi-api-key': env('ELEVENLABS_API_KEY') })
|
|
169
|
+
return (data.voices ?? []).map((v) => `${v.voice_id} ${v.name} ${JSON.stringify(v.labels ?? {})}`)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const openai = {
|
|
174
|
+
label: 'OpenAI (or Azure OpenAI)',
|
|
175
|
+
check() {
|
|
176
|
+
if (process.env.AZURE_OPENAI_ENDPOINT) env('AZURE_OPENAI_API_KEY')
|
|
177
|
+
else env('OPENAI_API_KEY')
|
|
178
|
+
},
|
|
179
|
+
async synth(text, dst, cfg) {
|
|
180
|
+
const body = { model: cfg.model ?? 'gpt-4o-mini-tts', voice: cfg.voice ?? 'alloy', input: text, response_format: 'wav' }
|
|
181
|
+
if (cfg.instructions) body.instructions = cfg.instructions // e.g. "Calm, warm, unhurried explainer."
|
|
182
|
+
let url, headers
|
|
183
|
+
if (process.env.AZURE_OPENAI_ENDPOINT) {
|
|
184
|
+
url = `${process.env.AZURE_OPENAI_ENDPOINT.replace(/\/$/, '')}/openai/deployments/${cfg.deployment ?? body.model}/audio/speech?api-version=${cfg.api_version ?? '2025-03-01-preview'}`
|
|
185
|
+
headers = { 'api-key': env('AZURE_OPENAI_API_KEY'), 'content-type': 'application/json' }
|
|
186
|
+
} else {
|
|
187
|
+
url = 'https://api.openai.com/v1/audio/speech'
|
|
188
|
+
headers = { authorization: `Bearer ${env('OPENAI_API_KEY')}`, 'content-type': 'application/json' }
|
|
189
|
+
}
|
|
190
|
+
toTake(await post(url, body, headers), dst)
|
|
191
|
+
},
|
|
192
|
+
voices: async () => ['alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse (all speak every supported language; set "instructions" for tone)']
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const xml = (s) => s.replace(/[<>&'"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', "'": ''', '"': '"' })[c])
|
|
196
|
+
const azure = {
|
|
197
|
+
label: 'Azure AI Speech',
|
|
198
|
+
check(cfg) {
|
|
199
|
+
env('AZURE_SPEECH_KEY'); env('AZURE_SPEECH_REGION')
|
|
200
|
+
if (!cfg.voice) fail('set "voice", e.g. "en-US-AvaMultilingualNeural" or "es-ES-ElviraNeural"')
|
|
201
|
+
},
|
|
202
|
+
async synth(text, dst, cfg, locale) {
|
|
203
|
+
const ssml = `<speak version='1.0' xml:lang='${locale}'><voice name='${cfg.voice}'><prosody rate='${cfg.rate ?? '0%'}'>${xml(text)}</prosody></voice></speak>`
|
|
204
|
+
const audio = await post(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/v1`, ssml, {
|
|
205
|
+
'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY'), 'content-type': 'application/ssml+xml',
|
|
206
|
+
'X-Microsoft-OutputFormat': 'riff-48khz-16bit-mono-pcm', 'user-agent': 'emaki'
|
|
207
|
+
})
|
|
208
|
+
toTake(audio, dst)
|
|
209
|
+
},
|
|
210
|
+
async voices(cfg, locale) {
|
|
211
|
+
const data = await get(`https://${env('AZURE_SPEECH_REGION')}.tts.speech.microsoft.com/cognitiveservices/voices/list`, { 'Ocp-Apim-Subscription-Key': env('AZURE_SPEECH_KEY') })
|
|
212
|
+
return data.filter((v) => v.Locale.startsWith(locale) || v.Locale.split('-')[0] === lang(locale)).map((v) => `${v.ShortName} ${v.Gender}`)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function googleAuth() {
|
|
217
|
+
if (process.env.GOOGLE_TTS_API_KEY) return { 'X-Goog-Api-Key': process.env.GOOGLE_TTS_API_KEY }
|
|
218
|
+
if (which('gcloud')) {
|
|
219
|
+
const tok = spawnSync('gcloud', ['auth', 'print-access-token'], { encoding: 'utf-8' }).stdout?.trim()
|
|
220
|
+
if (tok) return { authorization: `Bearer ${tok}` }
|
|
221
|
+
}
|
|
222
|
+
return fail('set GOOGLE_TTS_API_KEY, or sign in with `gcloud auth login`')
|
|
223
|
+
}
|
|
224
|
+
const google = {
|
|
225
|
+
label: 'Google Cloud Text-to-Speech',
|
|
226
|
+
check(cfg) {
|
|
227
|
+
googleAuth()
|
|
228
|
+
if (!cfg.voice) fail('set "voice", e.g. "en-US-Neural2-F" or "es-ES-Chirp3-HD-Aoede"')
|
|
229
|
+
},
|
|
230
|
+
async synth(text, dst, cfg) {
|
|
231
|
+
const body = {
|
|
232
|
+
input: { text }, voice: { languageCode: cfg.voice.split('-').slice(0, 2).join('-'), name: cfg.voice },
|
|
233
|
+
audioConfig: { audioEncoding: 'LINEAR16', sampleRateHertz: 48000, speakingRate: cfg.speed ?? 1 }
|
|
234
|
+
}
|
|
235
|
+
const data = await post('https://texttospeech.googleapis.com/v1/text:synthesize', body, { ...googleAuth(), 'content-type': 'application/json' }, 'json')
|
|
236
|
+
toTake(Buffer.from(data.audioContent, 'base64'), dst)
|
|
237
|
+
},
|
|
238
|
+
async voices(cfg, locale) {
|
|
239
|
+
const data = await get(`https://texttospeech.googleapis.com/v1/voices?languageCode=${locale}`, googleAuth())
|
|
240
|
+
return (data.voices ?? []).map((v) => `${v.name} ${v.ssmlGender}`)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const polly = {
|
|
245
|
+
label: 'Amazon Polly',
|
|
246
|
+
check(cfg) {
|
|
247
|
+
if (!which('aws')) fail('install the AWS CLI and sign in (aws configure / SSO); credentials stay in your AWS profile')
|
|
248
|
+
if (!cfg.voice) fail('set "voice", e.g. "Joanna" (en-US) or "Lucia" (es-ES)')
|
|
249
|
+
},
|
|
250
|
+
async synth(text, dst, cfg, locale) {
|
|
251
|
+
inTemp((d) => {
|
|
252
|
+
const raw = join(d, 'raw.mp3')
|
|
253
|
+
sh('aws', ['polly', 'synthesize-speech', '--output-format', 'mp3', '--voice-id', cfg.voice, '--engine', cfg.engine ?? 'neural',
|
|
254
|
+
'--language-code', cfg.language ?? locale, '--text', text, ...(cfg.region ? ['--region', cfg.region] : []), raw])
|
|
255
|
+
toTake(raw, dst)
|
|
256
|
+
})
|
|
257
|
+
},
|
|
258
|
+
async voices(cfg, locale) {
|
|
259
|
+
const out = JSON.parse(String(sh('aws', ['polly', 'describe-voices', '--language-code', cfg.language ?? locale, '--engine', cfg.engine ?? 'neural', ...(cfg.region ? ['--region', cfg.region] : [])])))
|
|
260
|
+
return out.Voices.map((v) => `${v.Id} ${v.Gender}`)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const manual = {
|
|
265
|
+
label: 'Recorded by a person',
|
|
266
|
+
check() {},
|
|
267
|
+
async synth() { fail('manual: record the takes and place them as take-NN.wav; nothing to synthesise') },
|
|
268
|
+
voices: async () => ['Record one file per scene, in scene order, named take-01.wav, take-02.wav, …']
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export const PROVIDERS = { piper, kokoro, macos, espeak, elevenlabs, openai, azure, google, polly, manual }
|
|
272
|
+
export const OFFLINE = ['piper', 'kokoro', 'macos', 'espeak']
|
|
273
|
+
|
|
274
|
+
export function provider(name) {
|
|
275
|
+
if (!PROVIDERS[name]) fail(`unknown provider "${name}"; one of: ${Object.keys(PROVIDERS).join(', ')}`)
|
|
276
|
+
return PROVIDERS[name]
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The voice block for a locale: voice.default, then voice.<language>, then voice.<locale>; most specific wins. A block
|
|
280
|
+
* that switches provider starts fresh, so another provider's options (voice, model) don't carry over. */
|
|
281
|
+
export function voiceConfig(cfg, locale) {
|
|
282
|
+
const v = cfg.voice ?? {}
|
|
283
|
+
let out = {}
|
|
284
|
+
for (const k of ['default', lang(locale), locale]) {
|
|
285
|
+
const block = v[k] ?? {}
|
|
286
|
+
out = block.provider && block.provider !== out.provider ? { ...block } : { ...out, ...block }
|
|
287
|
+
}
|
|
288
|
+
return out
|
|
289
|
+
}
|
|
290
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Instagram Graph API (instagram_content_publish): verify the account, and post a Reel from a public video URL
|
|
2
|
+
// (Meta fetches the media itself): create the container, wait until it is processed, publish, read the permalink.
|
|
3
|
+
import { callJson } from '../http.js'
|
|
4
|
+
import { die } from '../util.js'
|
|
5
|
+
|
|
6
|
+
export function instagram({ userId, token, version = 'v21.0', fetch = globalThis.fetch, sleep = (ms) => new Promise((r) => setTimeout(r, ms)), tries = 60 }) {
|
|
7
|
+
const base = `https://graph.facebook.com/${version}`
|
|
8
|
+
const graph = (method, path, params = {}) => {
|
|
9
|
+
const q = new URLSearchParams({ ...params, access_token: token }).toString()
|
|
10
|
+
return method === 'GET'
|
|
11
|
+
? callJson(fetch, 'GET', `${base}/${path}?${q}`)
|
|
12
|
+
: callJson(fetch, method, `${base}/${path}`, { body: q, headers: { 'content-type': 'application/x-www-form-urlencoded' } })
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
/** The account this token posts as (read-only). */
|
|
16
|
+
async verify() {
|
|
17
|
+
return (await graph('GET', String(userId), { fields: 'username' })).username ?? String(userId)
|
|
18
|
+
},
|
|
19
|
+
/** Posts a Reel; returns { url (permalink), id }. */
|
|
20
|
+
async publishReel(videoUrl, caption, coverUrl, { shareToFeed = true } = {}) {
|
|
21
|
+
const params = { media_type: 'REELS', video_url: videoUrl, caption, share_to_feed: shareToFeed ? 'true' : 'false', ...(coverUrl ? { cover_url: coverUrl } : {}) }
|
|
22
|
+
const container = (await graph('POST', `${userId}/media`, params)).id
|
|
23
|
+
for (let i = 0; ; i++) {
|
|
24
|
+
const st = (await graph('GET', container, { fields: 'status_code' })).status_code
|
|
25
|
+
if (st === 'FINISHED') break
|
|
26
|
+
if (st === 'ERROR' || st === 'EXPIRED') die(`Instagram could not process the media (${st})`)
|
|
27
|
+
if (i + 1 >= tries) die('Instagram took too long to process the media')
|
|
28
|
+
await sleep(10000)
|
|
29
|
+
}
|
|
30
|
+
const id = (await graph('POST', `${userId}/media_publish`, { creation_id: container })).id
|
|
31
|
+
return { url: (await graph('GET', id, { fields: 'permalink' })).permalink ?? null, id }
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// YouTube Data API v3 with an OAuth refresh token (scopes youtube.upload + youtube.force-ssl): verify the channel,
|
|
2
|
+
// upload a video (resumable) with its caption track and custom thumbnail, and refresh an uploaded video in place.
|
|
3
|
+
import { randomBytes } from 'node:crypto'
|
|
4
|
+
import { call, callJson } from '../http.js'
|
|
5
|
+
import { die } from '../util.js'
|
|
6
|
+
|
|
7
|
+
const API = 'https://www.googleapis.com/youtube/v3', UPLOAD = 'https://www.googleapis.com/upload/youtube/v3'
|
|
8
|
+
|
|
9
|
+
/** multipart/related body from [contentType, bytes | string] parts. */
|
|
10
|
+
function multipart(parts) {
|
|
11
|
+
const boundary = 'emaki' + randomBytes(8).toString('hex')
|
|
12
|
+
const chunks = parts.flatMap(([type, data]) => [Buffer.from(`--${boundary}\r\nContent-Type: ${type}\r\n\r\n`), Buffer.from(data), Buffer.from('\r\n')])
|
|
13
|
+
return { body: Buffer.concat([...chunks, Buffer.from(`--${boundary}--\r\n`)]), type: `multipart/related; boundary=${boundary}` }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const languageName = (code) => {
|
|
17
|
+
try {
|
|
18
|
+
return new Intl.DisplayNames(['en'], { type: 'language' }).of(code) ?? code
|
|
19
|
+
} catch {
|
|
20
|
+
return code
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function youtube({ clientId, clientSecret, refreshToken, fetch = globalThis.fetch, log = console.info }) {
|
|
25
|
+
let access
|
|
26
|
+
const token = async () => (access ??= (await callJson(fetch, 'POST', 'https://oauth2.googleapis.com/token', {
|
|
27
|
+
body: new URLSearchParams({ client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken, grant_type: 'refresh_token' }).toString(),
|
|
28
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' }
|
|
29
|
+
})).access_token)
|
|
30
|
+
const auth = async (extra = {}) => ({ authorization: `Bearer ${await token()}`, ...extra })
|
|
31
|
+
const get = async (url) => callJson(fetch, 'GET', url, { headers: await auth() })
|
|
32
|
+
|
|
33
|
+
async function uploadCaption(videoId, srt, language = 'en') {
|
|
34
|
+
const m = multipart([['application/json; charset=UTF-8', JSON.stringify({ snippet: { videoId, language, name: languageName(language) } })], ['application/octet-stream', srt]])
|
|
35
|
+
await callJson(fetch, 'POST', `${UPLOAD}/captions?part=snippet`, { body: m.body, headers: await auth({ 'content-type': m.type }) })
|
|
36
|
+
}
|
|
37
|
+
async function setThumbnail(videoId, jpg) {
|
|
38
|
+
const m = multipart([['image/jpeg', jpg]])
|
|
39
|
+
await callJson(fetch, 'POST', `${UPLOAD}/thumbnails/set?videoId=${encodeURIComponent(videoId)}`, { body: m.body, headers: await auth({ 'content-type': m.type }) })
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
/** The channel this credential posts as (read-only). */
|
|
44
|
+
async verify() {
|
|
45
|
+
const r = await get(`${API}/channels?part=snippet&mine=true`)
|
|
46
|
+
if (!r.items?.length) die('this YouTube credential has no channel')
|
|
47
|
+
return r.items[0].snippet.title
|
|
48
|
+
},
|
|
49
|
+
async getVideo(id) {
|
|
50
|
+
const r = await get(`${API}/videos?part=snippet,contentDetails,status&id=${encodeURIComponent(id)}`)
|
|
51
|
+
if (!r.items?.length) die(`no YouTube video ${id} on this channel`)
|
|
52
|
+
return r.items[0]
|
|
53
|
+
},
|
|
54
|
+
/** Metadata in place; an omitted field keeps the remote value. The video file itself is never replaced. */
|
|
55
|
+
async updateMetadata(id, { title, description, tags, categoryId } = {}) {
|
|
56
|
+
const r = await get(`${API}/videos?part=snippet&id=${encodeURIComponent(id)}`)
|
|
57
|
+
if (!r.items?.length) die(`no YouTube video ${id} on this channel`)
|
|
58
|
+
const cur = r.items[0].snippet
|
|
59
|
+
const snippet = { title: title ?? cur.title, description: description ?? cur.description ?? '', tags: tags ?? cur.tags ?? [], categoryId: categoryId ?? cur.categoryId ?? '28' }
|
|
60
|
+
await callJson(fetch, 'PUT', `${API}/videos?part=snippet`, { body: JSON.stringify({ id, snippet }), headers: await auth({ 'content-type': 'application/json; charset=UTF-8' }) })
|
|
61
|
+
},
|
|
62
|
+
/** Replaces uploaded caption tracks with ours; automatic (ASR) tracks are left alone. */
|
|
63
|
+
async replaceCaptions(id, srt, language = 'en') {
|
|
64
|
+
const r = await get(`${API}/captions?part=snippet&videoId=${encodeURIComponent(id)}`)
|
|
65
|
+
for (const c of r.items ?? [])
|
|
66
|
+
if (String(c.snippet?.trackKind ?? '').toUpperCase() !== 'ASR') await call(fetch, 'DELETE', `${API}/captions?id=${encodeURIComponent(c.id)}`, { headers: await auth() })
|
|
67
|
+
await uploadCaption(id, srt, language)
|
|
68
|
+
},
|
|
69
|
+
setThumbnail,
|
|
70
|
+
/** Uploads a video; returns { id, url, privacy }. srt and thumb are optional. */
|
|
71
|
+
async publish(video, { title, description = '', tags = [], privacy = 'unlisted', srt, thumb, short = false, categoryId = '28', language = 'en' }) {
|
|
72
|
+
const meta = {
|
|
73
|
+
snippet: { title: title.slice(0, 100), description: description.slice(0, 5000), tags: tags.slice(0, 30), categoryId },
|
|
74
|
+
status: { privacyStatus: privacy, selfDeclaredMadeForKids: false }
|
|
75
|
+
}
|
|
76
|
+
const init = await call(fetch, 'POST', `${UPLOAD}/videos?uploadType=resumable&part=snippet,status`, {
|
|
77
|
+
body: JSON.stringify(meta),
|
|
78
|
+
headers: await auth({ 'content-type': 'application/json; charset=UTF-8', 'x-upload-content-type': 'video/mp4', 'x-upload-content-length': String(video.length) })
|
|
79
|
+
})
|
|
80
|
+
const session = init.headers.get('location')
|
|
81
|
+
if (!session) die('YouTube did not return an upload session (no Location header)')
|
|
82
|
+
const got = await callJson(fetch, 'PUT', session, { body: video, headers: await auth({ 'content-type': 'video/mp4' }) })
|
|
83
|
+
const id = got.id, set = got.status?.privacyStatus ?? privacy
|
|
84
|
+
if (set !== privacy) log(`warning: YouTube set privacy to "${set}" instead of "${privacy}"`)
|
|
85
|
+
if (srt) await uploadCaption(id, srt, language)
|
|
86
|
+
if (thumb) await setThumbnail(id, thumb)
|
|
87
|
+
return { id, url: short ? `https://www.youtube.com/shorts/${id}` : `https://www.youtube.com/watch?v=${id}`, privacy: set }
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/util.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// Small helpers shared by the commands. Errors that the user should read go through die(), which throws a
|
|
2
|
+
// MotionError: the CLI prints its message and exits 1; tests catch it.
|
|
3
|
+
import { execFileSync, spawnSync } from 'node:child_process'
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { dirname } from 'node:path'
|
|
6
|
+
|
|
7
|
+
export class MotionError extends Error {}
|
|
8
|
+
/** @returns {never} */
|
|
9
|
+
export function die(msg) {
|
|
10
|
+
throw new MotionError(msg)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const load = (p) => JSON.parse(readFileSync(p, 'utf-8'))
|
|
14
|
+
export function save(p, data) {
|
|
15
|
+
mkdirSync(dirname(p), { recursive: true })
|
|
16
|
+
writeFileSync(p, JSON.stringify(data, null, 2) + '\n')
|
|
17
|
+
}
|
|
18
|
+
export const exists = (p) => existsSync(p)
|
|
19
|
+
|
|
20
|
+
/** Runs a program (no shell) and returns stdout; dies with the tail of stderr on failure. */
|
|
21
|
+
export function run(cmd, args, opts = {}) {
|
|
22
|
+
const r = spawnSync(cmd, args, { encoding: 'utf-8', maxBuffer: 64 * 1024 * 1024, ...opts })
|
|
23
|
+
if (r.error) die(`${cmd}: ${r.error.code === 'ENOENT' ? 'not installed (not on PATH)' : r.error.message}`)
|
|
24
|
+
if (r.status) die(`${cmd} failed: ${(r.stderr || '').slice(-800)}`)
|
|
25
|
+
return r.stdout
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** True when `cmd` is on PATH. */
|
|
29
|
+
export function which(cmd) {
|
|
30
|
+
try {
|
|
31
|
+
execFileSync(process.platform === 'win32' ? 'where' : 'which', [cmd], { stdio: 'ignore' })
|
|
32
|
+
return true
|
|
33
|
+
} catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Length of an audio or video file in seconds (ffprobe). */
|
|
39
|
+
export const mediaLength = (p) =>
|
|
40
|
+
parseFloat(run('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', p]).trim())
|
|
41
|
+
|
|
42
|
+
/** True when a media file has an audio stream (ffprobe). */
|
|
43
|
+
export const hasAudio = (p) =>
|
|
44
|
+
run('ffprobe', ['-v', 'error', '-select_streams', 'a', '-show_entries', 'stream=codec_type', '-of', 'csv=p=0', p]).includes('audio')
|
|
45
|
+
|
|
46
|
+
/** key=value pairs from --opt, values parsed as JSON when they are JSON (numbers, booleans). */
|
|
47
|
+
export function parseOpts(items) {
|
|
48
|
+
const out = {}
|
|
49
|
+
for (const it of [items ?? []].flat()) {
|
|
50
|
+
const i = String(it).indexOf('=')
|
|
51
|
+
if (i < 0) continue
|
|
52
|
+
const k = it.slice(0, i), v = it.slice(i + 1)
|
|
53
|
+
try {
|
|
54
|
+
out[k] = JSON.parse(v)
|
|
55
|
+
} catch {
|
|
56
|
+
out[k] = v
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A scene's running time: its fitted `dur` (timing.<locale>.json, longer or shorter than the design), else the
|
|
63
|
+
* design time plus its pauses. The core's loadFilm resolves it the same way. */
|
|
64
|
+
export const runningTime = (scene, tm = {}) => tm.dur ?? scene.dur + (tm.holds ?? []).reduce((n, h) => n + h[1], 0)
|
|
65
|
+
export const pad2 = (n) => String(n).padStart(2, '0')
|
|
66
|
+
export const today = () => new Date().toISOString().slice(0, 10)
|