Files
devbox/js/tools-creator.js
gitea f947a682b0 创作者工具:视频/音乐解析、视音频转格式、网盘直链
- 服务端新增 lib/creator.mjs:yt-dlp 解析、ffmpeg 边转边传、蓝奏/阿里/百度直链
- server.mjs 增加 /api/creator/{status,cfg,resolve,convert,download,netdisk},各操作扣 1 积分
- 前端新增 js/tools-creator.js 与「创作者」分组(5 个工具),配套样式
- install.sh 顺手装 ffmpeg / yt-dlp;README、INSTALL 补依赖与用法
2026-11-20 13:40:04 +08:00

283 lines
14 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* DevBox · 创作者:视频解析 / 音乐解析 / 视频转格式 / 音频转格式 / 网盘直链 */
(function () {
'use strict'
const { el, panelTitle, copy, output, toast, bytes } = DB
const pane = (title, right) => {
const p = el('div', { class: 'panel' })
if (title) p.append(panelTitle(title, right))
return p
}
const btn = (label, fn, cls = '') => el('button', { class: 'btn ' + cls, text: label, onclick: fn })
const field = (label, control) => el('label', { class: 'field' }, el('span', { text: label }), control)
const hint = (t) => el('div', { class: 'hint', text: t })
const err = () => el('div', { class: 'err' })
async function post(url, body) {
const r = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body || {}) })
const j = await r.json().catch(() => ({ ok: false, error: '返回不是 JSON(' + r.status + ')' }))
if (!r.ok && !j.error) j.error = 'HTTP ' + r.status
if (j.user) { try { DB.auth.setUser(j.user) } catch {} }
return j
}
async function get(url) {
const r = await fetch(url)
return r.json().catch(() => ({ ok: false, error: 'HTTP ' + r.status }))
}
const fmtDur = (s) => {
s = Math.round(Number(s) || 0)
if (!s) return ''
const h = Math.floor(s / 3600)
const m = Math.floor((s % 3600) / 60)
const x = s % 60
return (h ? h + ':' + String(m).padStart(2, '0') : m) + ':' + String(x).padStart(2, '0')
}
/* 媒体卡片列表 */
function mediaTable(list, kindLabel, onPick) {
const wrap = el('div', { class: 'result-list' })
if (!list.length) return el('div', { class: 'hint', text: '没有可用的' + kindLabel + '格式' })
for (const f of list) {
const row = el('div', { class: 'row' },
el('span', { class: 'tag', text: f.quality || f.abr ? (f.quality || Math.round(f.abr) + 'k') : f.ext }),
el('span', { class: 'txt', text: [f.ext, f.vcodec || f.acodec, f.size ? bytes(f.size) : '', f.note].filter(Boolean).join(' · ') }),
btn('复制直链', async () => { await copy(f.url); toast('直链已复制') }),
btn('下载', () => { onPick(f) })
)
wrap.append(row)
}
return wrap
}
function saveJob(job) {
const a = el('a', { href: '/api/creator/download?t=' + encodeURIComponent(job.token), download: job.name })
document.body.append(a)
a.click()
a.remove()
toast('开始下载 ' + job.name)
}
/* ================= 视频解析 ================= */
DB.tool({
id: 'video-parse',
name: '视频解析',
group: 'creator',
icon: 'download',
desc: '粘 B 站 / 抖音 / YouTube 等链接,列出所有清晰度和直链,可复制或直接下载',
keywords: ['视频', '解析', '直链', 'bilibili', 'b站', '抖音', 'youtube', '下载', 'video', 'parse'],
mount(root) {
const input = el('textarea', { rows: 3, placeholder: '粘贴视频链接,例如 https://www.bilibili.com/video/BVxxxx 或 https://youtu.be/xxx' })
const cookie = el('textarea', { rows: 2, placeholder: '可选:Cookie(B 站大会员 / 需要登录的站点才填)' })
const out = output({ rows: 10 })
const errBox = err()
const head = el('div', { class: 'media-head' })
const list = el('div')
const go = async () => {
errBox.textContent = ''
head.textContent = ''
list.textContent = ''
out.value = '解析中…'
const j = await post('/api/creator/resolve', { url: input.value.trim(), cookie: cookie.value.trim() })
if (!j.ok) {
out.value = ''
errBox.textContent = j.error || '解析失败'
if (j.user) DB.auth.setUser(j.user)
return
}
out.value = ''
head.append(
j.thumbnail ? el('img', { src: j.thumbnail, class: 'thumb', referrerpolicy: 'no-referrer' }) : '',
el('div', {},
el('div', { class: 'media-title', text: j.title || '(无标题)' }),
el('div', { class: 'hint', text: [j.uploader, j.duration ? fmtDur(j.duration) : '', j.source].filter(Boolean).join(' · ') }),
j.note ? el('div', { class: 'hint', text: j.note }) : ''
)
)
list.append(
el('div', { class: 'media-sub', text: '视频(' + j.video.length + ')' }),
mediaTable(j.video, '视频', (f) => toConvert(f, input.value.trim(), 'video')),
el('div', { class: 'media-sub', text: '音频(' + j.audio.length + ')' }),
mediaTable(j.audio, '音频', (f) => toConvert(f, input.value.trim(), 'audio'))
)
const raw = el('details', {}, el('summary', { text: '原始 JSON' }), output({ rows: 6 }))
raw.querySelector('textarea').value = JSON.stringify(j, null, 2)
list.append(raw)
}
root.append(
pane('视频链接', btn('解析', go, 'primary')),
field('链接', input),
field('Cookie(可选)', cookie),
errBox,
pane('结果'),
head,
list
)
hintText(root)
}
})
function hintText(root) {
root.append(
el('div', { class: 'hint', text: '每次解析消耗 1 积分。B 站 412 是风控拦截,把浏览器里的 Cookie 粘进来即可。' })
)
}
async function toConvert(fmt, pageUrl, kind) {
const format = kind === 'audio' ? 'mp3' : (fmt.ext === 'webm' ? 'webm' : 'mp4')
const j = await post('/api/creator/convert', { url: fmt.url, kind, format, title: (fmt.id || '') + '-' + kind })
if (!j.ok) return toast(j.error || '转码失败', true)
saveJob(j)
}
/* ================= 音乐解析 ================= */
DB.tool({
id: 'music-parse',
name: '音乐解析',
group: 'creator',
icon: 'bolt',
desc: '从视频链接里抽出音频,直接试听、复制音频直链或转成 MP3',
keywords: ['音乐', '音频', 'mp3', '解析', 'music', 'audio', '试听'],
mount(root) {
const input = el('textarea', { rows: 3, placeholder: '粘贴视频 / 音乐页面链接' })
const out = output({ rows: 8 })
const errBox = err()
const list = el('div')
const audio = el('audio', { controls: true, style: { width: '100%', display: 'none' } })
const go = async () => {
errBox.textContent = ''
list.textContent = ''
out.value = '解析中…'
const j = await post('/api/creator/resolve', { url: input.value.trim(), audioOnly: true })
out.value = ''
if (!j.ok) { errBox.textContent = j.error || '解析失败'; return }
list.append(
el('div', { class: 'media-title', text: j.title || '(无标题)' }),
el('div', { class: 'hint', text: [j.uploader, j.duration ? fmtDur(j.duration) : '', j.source].filter(Boolean).join(' · ') }),
mediaTable(j.audio, '音频', (f) => { audio.src = f.url; audio.style.display = 'block'; audio.play().catch(() => toast('浏览器不让自动播放,点一下播放键', true)) }),
el('div', { class: 'row' }, btn('转成 MP3', async () => {
const job = await post('/api/creator/convert', { url: input.value.trim(), kind: 'audio', format: 'mp3', title: j.title })
if (!job.ok) return toast(job.error || '转换失败', true)
saveJob(job)
}, 'primary'))
)
}
root.append(pane('链接', btn('解析音频', go, 'primary')), field('链接', input), errBox, audio, pane('结果'), list)
hintText(root)
}
})
/* ================= 格式转换(视频 / 音频共用) ================= */
function convertTool({ id, name, icon, kind, desc, keywords }) {
DB.tool({
id, name, group: 'creator', icon, desc, keywords,
mount(root) {
const input = el('textarea', { rows: 3, placeholder: '视频页链接,或任意 http(s) 直链(mp4 / m3u8 / mp3 …)' })
const format = el('select', {})
const cookie = el('textarea', { rows: 2, placeholder: '可选 Cookie' })
const start = el('input', { type: 'number', min: 0, step: 1, placeholder: '0' })
const dur = el('input', { type: 'number', min: 0, step: 1, placeholder: '全部' })
const errBox = err()
const out = output({ rows: 6 })
const go = async () => {
errBox.textContent = ''
out.value = '准备任务…'
const j = await post('/api/creator/convert', {
url: input.value.trim(), kind, format: format.value, cookie: cookie.value.trim(),
start: Number(start.value || 0), duration: Number(dur.value || 0)
})
out.value = ''
if (!j.ok) { errBox.textContent = j.error || '转换失败'; return }
out.value = '输出:' + j.name + '\n格式:' + j.format + '(' + j.mime + ')\n开始下载…'
saveJob(j)
}
const fill = async () => {
const st = await get('/api/creator/status')
format.textContent = ''
const arr = (st.formats && st.formats[kind]) || []
for (const f of arr) format.append(el('option', { value: f, text: f.toUpperCase() }))
if (!arr.length) format.append(el('option', { value: kind === 'audio' ? 'mp3' : 'mp4', text: kind === 'audio' ? 'MP3' : 'MP4' }))
}
fill()
root.append(
pane(name, btn('开始转换', go, 'primary')),
field('来源链接', input),
el('div', { class: 'grid2' }, field('输出格式', format), field('Cookie(可选)', cookie)),
el('div', { class: 'grid2' }, field('从第几秒开始', start), field('转多少秒(留空=全部)', dur)),
errBox, pane('状态'), out
)
root.append(el('div', { class: 'hint', text: '服务器用 ffmpeg 现转现传,不下存临时文件;大文件耗时长,超时会被自动终止。每次 1 积分。' }))
}
})
}
convertTool({ id: 'video-convert', name: '视频转格式', icon: 'swap', kind: 'video', desc: '链接或直链 → MP4 / MKV / WebM / GIF,可截取片段', keywords: ['视频', '转格式', 'mp4', 'mkv', 'webm', 'gif', 'ffmpeg', 'convert'] })
convertTool({ id: 'audio-convert', name: '音频转格式', icon: 'bolt', kind: 'audio', desc: '音频或视频链接 → MP3 / M4A / WAV / FLAC / OGG / AAC', keywords: ['音频', '音乐', '转格式', 'mp3', 'wav', 'flac', 'aac', 'convert'] })
/* ================= 网盘直链 ================= */
DB.tool({
id: 'netdisk',
name: '网盘直链',
group: 'creator',
icon: 'globe',
desc: '蓝奏云 / 阿里云盘 / 百度网盘分享链接 → 提取码 + 直链,可复制或直接下载',
keywords: ['网盘', '直链', '蓝奏云', '阿里云盘', '百度网盘', 'lanzou', 'pan', 'netdisk'],
mount(root) {
const input = el('textarea', { rows: 2, placeholder: '分享链接,例如 https://wwx.lanzou.com/xxxx 或 https://www.aliyundrive.com/s/xxxx' })
const pwd = el('input', { type: 'text', placeholder: '提取码(没有就留空)' })
const cookie = el('input', { type: 'text', placeholder: '可选:本次使用自己的 Cookie / refresh_token' })
const errBox = err()
const list = el('div')
const cfgBox = el('div')
const cfgState = el('div', { class: 'hint' })
const loadCfg = async () => {
const c = await get('/api/creator/cfg')
cfgState.textContent = '当前配置:阿里 ' + (c.aliyun ? '已填' : '未填') + ' · 百度 ' + (c.baidu ? '已填' : '未填') + ' · 通用 Cookie ' + (c.cookie ? '已填' : '未填')
}
const saveCfg = async () => {
await post('/api/creator/cfg', { aliyun: aliyun.value.trim(), baidu: baidu.value.trim(), cookie: gen.value.trim() })
toast('已保存')
loadCfg()
}
const aliyun = el('textarea', { rows: 2, placeholder: '阿里云盘 refresh_token' })
const baidu = el('textarea', { rows: 2, placeholder: '百度网盘 Cookie(含 BDUSS=…)' })
const gen = el('textarea', { rows: 2, placeholder: '通用 Cookie(B 站等解析用)' })
cfgBox.append(
pane('解析设置(存服务器,仅自己可见)', btn('保存设置', saveCfg)),
field('阿里云盘 token', aliyun), field('百度网盘 Cookie', baidu), field('通用 Cookie', gen), cfgState
)
cfgBox.style.display = 'none'
const go = async () => {
errBox.textContent = ''
list.textContent = ''
const j = await post('/api/creator/netdisk', { url: input.value.trim(), pwd: pwd.value.trim(), cookie: cookie.value.trim() })
if (!j.ok) { errBox.textContent = j.error || j.message || '解析失败'; return }
list.append(
el('div', { class: 'media-title', text: (j.kind || '') + ' · ' + (j.name || '') }),
...(j.links || []).map((l) =>
el('div', { class: 'row' },
el('span', { class: 'txt', text: l.label + (l.note ? '(' + l.note + ')' : '') }),
btn('复制', async () => { await copy(l.url); toast('已复制') }),
btn('打开', () => window.open(l.url, '_blank', 'noopener'))
)
),
j.note ? el('div', { class: 'hint', text: j.note }) : ''
)
}
root.append(
pane('网盘链接', btn('取直链', go, 'primary')),
field('分享链接', input),
field('提取码', pwd),
field('临时 Cookie / token(可选)', cookie),
errBox,
pane('结果'),
list,
btn('解析设置(阿里 / 百度)', () => { cfgBox.style.display = cfgBox.style.display === 'none' ? '' : 'none' }),
cfgBox
)
loadCfg()
root.append(el('div', { class: 'hint', text: '蓝奏云直接可用;阿里云盘需要 refresh_token;百度直链需要 Cookie(且非会员会被限速)。每次 1 积分。' }))
}
})
})()