@senseihq/emaki-cli 0.1.0 → 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/README.md +42 -7
- package/package.json +4 -2
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/modules/campaign/README.md +10 -3
- package/plugin/modules/campaign/day-script.template.md +2 -1
- package/plugin/modules/posting-desk/README.md +2 -2
- package/plugin/skills/emaki/references/03-content-model.md +7 -4
- package/plugin/skills/emaki/references/04-scene-authoring.md +3 -0
- package/plugin/skills/emaki/references/05-voice-and-video.md +41 -8
- package/plugin/skills/emaki/references/09-publishing.md +5 -2
- package/plugin/skills/story-media/SKILL.md +2 -0
- package/plugin/skills/story-publish/SKILL.md +18 -4
- package/src/api.js +12 -1
- package/src/build-media.js +166 -0
- package/src/build-video.js +113 -0
- package/src/campaign.js +138 -0
- package/src/capture.js +6 -118
- package/src/carousel.js +91 -0
- 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/day-script.js +66 -0
- package/src/desk-client.js +37 -0
- package/src/desk-plan.js +57 -0
- package/src/film-checks.js +102 -0
- package/src/films.js +7 -75
- package/src/fit.js +79 -0
- package/src/http.js +30 -0
- package/src/index.js +10 -184
- package/src/init.js +10 -13
- package/src/media.js +80 -0
- package/src/migrate.js +84 -0
- package/src/publish.js +136 -0
- package/src/render.js +125 -0
- package/src/show-page.js +75 -0
- package/src/stage.js +120 -0
- package/src/stories.js +66 -0
- 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/upload/instagram.js +34 -0
- package/src/upload/youtube.js +90 -0
- package/src/util.js +7 -0
- package/src/voice.js +8 -118
- package/src/youtube-text.js +58 -0
- package/templates/content/films/measure-the-right-work/film.json +9 -0
- package/templates/content/films/measure-the-right-work/look.css +1 -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/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/film/look.css +1 -0
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 }
|
package/src/tts-io.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// What every text-to-speech provider shares: the TTSError a check or a synthesis throws, running a command, a take
|
|
2
|
+
// in the shape fit and build expect, keys from the environment, and the HTTP calls the cloud providers make.
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
5
|
+
import { tmpdir } from 'node:os'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
export class TTSError extends Error {}
|
|
9
|
+
export const fail = (m) => { throw new TTSError(m) }
|
|
10
|
+
export const lang = (locale) => locale.split('-')[0]
|
|
11
|
+
|
|
12
|
+
export function sh(cmd, args, input) {
|
|
13
|
+
const r = spawnSync(cmd, args, { input, encoding: input == null ? 'utf-8' : undefined, maxBuffer: 256 * 1024 * 1024 })
|
|
14
|
+
if (r.error) fail(`${cmd}: ${r.error.code === 'ENOENT' ? 'not installed' : r.error.message}`)
|
|
15
|
+
if (r.status) fail(`${cmd}: ${String(r.stderr ?? '').slice(-400)}`)
|
|
16
|
+
return r.stdout
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Any audio file or bytes → 48 kHz mono WAV with 0.1 s head air at dst. */
|
|
20
|
+
export function toTake(src, dst) {
|
|
21
|
+
const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
|
|
22
|
+
try {
|
|
23
|
+
let file = src
|
|
24
|
+
if (typeof src !== 'string') { file = join(dir, 'raw'); writeFileSync(file, src) }
|
|
25
|
+
sh('ffmpeg', ['-v', 'error', '-y', '-i', file, '-af', 'adelay=100|100', '-ar', '48000', '-ac', '1', dst])
|
|
26
|
+
} finally {
|
|
27
|
+
rmSync(dir, { recursive: true, force: true })
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function inTemp(fn) {
|
|
32
|
+
const dir = mkdtempSync(join(tmpdir(), 'emaki-'))
|
|
33
|
+
try { return fn(dir) } finally { rmSync(dir, { recursive: true, force: true }) }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function env(...names) {
|
|
37
|
+
for (const n of names) if (process.env[n]) return process.env[n]
|
|
38
|
+
return fail(`set ${names.join(' or ')} in the environment (never in files)`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function post(url, body, headers, as = 'bytes') {
|
|
42
|
+
const r = await fetch(url, { method: 'POST', body: typeof body === 'string' || body instanceof Uint8Array ? body : JSON.stringify(body), headers })
|
|
43
|
+
if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status} ${(await r.text()).slice(0, 300)}`)
|
|
44
|
+
return as === 'json' ? r.json() : Buffer.from(await r.arrayBuffer())
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function get(url, headers) {
|
|
48
|
+
const r = await fetch(url, { headers })
|
|
49
|
+
if (!r.ok) fail(`${new URL(url).host}: HTTP ${r.status}`)
|
|
50
|
+
return r.json()
|
|
51
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The offline text-to-speech providers: Piper, Kokoro, macOS say and eSpeak NG. Each runs a local command and turns
|
|
2
|
+
// its output into a take (toTake). check(cfg, locale) throws TTSError when not ready; synth(text, dst, cfg, locale).
|
|
3
|
+
import { spawnSync } from 'node:child_process'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { fail, inTemp, lang, sh, toTake } from './tts-io.js'
|
|
7
|
+
import { which } from './util.js'
|
|
8
|
+
|
|
9
|
+
const piper = {
|
|
10
|
+
label: 'Piper (offline)',
|
|
11
|
+
check(cfg) {
|
|
12
|
+
if (!which('piper')) fail('install it: pip install piper-tts')
|
|
13
|
+
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"`)
|
|
14
|
+
},
|
|
15
|
+
async synth(text, dst, cfg) {
|
|
16
|
+
inTemp((d) => {
|
|
17
|
+
const raw = join(d, 'raw.wav')
|
|
18
|
+
const args = ['--model', cfg.model, '--output_file', raw]
|
|
19
|
+
if (cfg.speaker != null) args.push('--speaker', String(cfg.speaker))
|
|
20
|
+
if (cfg.length_scale) args.push('--length_scale', String(cfg.length_scale)) // >1 is slower
|
|
21
|
+
sh('piper', args, text)
|
|
22
|
+
if (!existsSync(raw)) fail('piper wrote nothing')
|
|
23
|
+
toTake(raw, dst)
|
|
24
|
+
})
|
|
25
|
+
},
|
|
26
|
+
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".']
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const KOKORO_LANG = { en: 'a', 'en-GB': 'b', es: 'e', fr: 'f', hi: 'h', it: 'i', ja: 'j', pt: 'p', zh: 'z' }
|
|
30
|
+
const KOKORO_PY = `import sys, json, numpy as np, soundfile as sf
|
|
31
|
+
from kokoro import KPipeline
|
|
32
|
+
a = json.loads(sys.argv[1]); text = sys.stdin.read()
|
|
33
|
+
pipe = KPipeline(lang_code=a['code'])
|
|
34
|
+
audio = [x for _, _, x in pipe(text, voice=a['voice'], speed=a['speed'])]
|
|
35
|
+
sf.write(a['out'], np.concatenate(audio), 24000)`
|
|
36
|
+
const kokoro = {
|
|
37
|
+
label: 'Kokoro (offline)',
|
|
38
|
+
check(cfg, locale) {
|
|
39
|
+
const py = cfg.python ?? 'python3'
|
|
40
|
+
const r = spawnSync(py, ['-c', 'import kokoro, soundfile'], { encoding: 'utf-8' })
|
|
41
|
+
if (r.error || r.status) fail(`install it: ${py} -m pip install kokoro soundfile (and espeak-ng for some languages)`)
|
|
42
|
+
if (!KOKORO_LANG[locale] && !KOKORO_LANG[lang(locale)]) fail(`kokoro has no voices for ${locale}`)
|
|
43
|
+
},
|
|
44
|
+
async synth(text, dst, cfg, locale) {
|
|
45
|
+
inTemp((d) => {
|
|
46
|
+
const out = join(d, 'raw.wav')
|
|
47
|
+
const code = cfg.lang_code ?? KOKORO_LANG[locale] ?? KOKORO_LANG[lang(locale)]
|
|
48
|
+
sh(cfg.python ?? 'python3', ['-c', KOKORO_PY, JSON.stringify({ code, voice: cfg.voice ?? 'af_heart', speed: cfg.speed ?? 1, out })], text)
|
|
49
|
+
toTake(out, dst)
|
|
50
|
+
})
|
|
51
|
+
},
|
|
52
|
+
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']
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const macos = {
|
|
56
|
+
label: 'macOS say (offline, drafts)',
|
|
57
|
+
check() { if (!which('say')) fail('`say` exists only on macOS') },
|
|
58
|
+
async synth(text, dst, cfg) {
|
|
59
|
+
inTemp((d) => {
|
|
60
|
+
const raw = join(d, 'raw.aiff')
|
|
61
|
+
sh('say', ['-o', raw, ...(cfg.voice ? ['-v', cfg.voice] : []), ...(cfg.rate ? ['-r', String(cfg.rate)] : []), text])
|
|
62
|
+
toTake(raw, dst)
|
|
63
|
+
})
|
|
64
|
+
},
|
|
65
|
+
async voices(cfg, locale) {
|
|
66
|
+
const all = String(sh('say', ['-v', '?'])).split('\n').filter(Boolean)
|
|
67
|
+
const l = lang(locale), mine = all.filter((x) => x.includes(` ${l}_`) || x.includes(` ${l}-`))
|
|
68
|
+
return mine.length ? mine : all.slice(0, 40)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const espeak = {
|
|
73
|
+
label: 'eSpeak NG (offline, robotic)',
|
|
74
|
+
check() { if (!which('espeak-ng')) fail('install espeak-ng (brew/apt/choco)') },
|
|
75
|
+
async synth(text, dst, cfg, locale) {
|
|
76
|
+
inTemp((d) => {
|
|
77
|
+
const raw = join(d, 'raw.wav')
|
|
78
|
+
sh('espeak-ng', ['-v', cfg.voice ?? locale, '-s', String(cfg.speed ?? 160), '-w', raw, text])
|
|
79
|
+
toTake(raw, dst)
|
|
80
|
+
})
|
|
81
|
+
},
|
|
82
|
+
voices: async (cfg, locale) => String(sh('espeak-ng', [`--voices=${lang(locale)}`])).split('\n').filter(Boolean)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { piper, kokoro, macos, espeak }
|
package/src/tts.js
CHANGED
|
@@ -20,246 +20,14 @@
|
|
|
20
20
|
// | google | cloud (Google) | GOOGLE_TTS_API_KEY, or `gcloud auth login` |
|
|
21
21
|
// | polly | cloud (AWS) | the `aws` CLI with credentials |
|
|
22
22
|
// | manual | a person | recorded take-NN.wav files |
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import { join } from 'node:path'
|
|
27
|
-
import { which } from './util.js'
|
|
23
|
+
import { azure, elevenlabs, google, openai, polly } from './tts-cloud.js'
|
|
24
|
+
import { fail, lang, TTSError, toTake } from './tts-io.js'
|
|
25
|
+
import { espeak, kokoro, macos, piper } from './tts-offline.js'
|
|
28
26
|
|
|
29
|
-
export
|
|
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
|
-
}
|
|
27
|
+
export { TTSError, toTake }
|
|
71
28
|
|
|
72
29
|
// ── 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
|
-
}
|
|
30
|
+
// The offline ones are in tts-offline.js, the cloud ones in tts-cloud.js; manual needs nothing.
|
|
263
31
|
|
|
264
32
|
const manual = {
|
|
265
33
|
label: 'Recorded by a person',
|
|
@@ -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
CHANGED
|
@@ -39,6 +39,10 @@ export function which(cmd) {
|
|
|
39
39
|
export const mediaLength = (p) =>
|
|
40
40
|
parseFloat(run('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', p]).trim())
|
|
41
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
|
+
|
|
42
46
|
/** key=value pairs from --opt, values parsed as JSON when they are JSON (numbers, booleans). */
|
|
43
47
|
export function parseOpts(items) {
|
|
44
48
|
const out = {}
|
|
@@ -55,5 +59,8 @@ export function parseOpts(items) {
|
|
|
55
59
|
return out
|
|
56
60
|
}
|
|
57
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)
|
|
58
65
|
export const pad2 = (n) => String(n).padStart(2, '0')
|
|
59
66
|
export const today = () => new Date().toISOString().slice(0, 10)
|