Spaces:
Paused
Paused
Download src/jiangnan/export/covers.ts from Ethscriptions/video: direct link, hf CLI and curl.
- Browser
- Download file 5.62 kB
-
https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/jiangnan/export/covers.ts
- Command line
-
hf download hf://spaces/Ethscriptions/video/src/jiangnan/export/covers.ts
-
curl -L -o covers.ts https://huggingface.co/spaces/Ethscriptions/video/resolve/main/src/jiangnan/export/covers.ts
5.62 kB
| // Covers: the 3D scene is re-rendered for each aspect ratio (no stretching), | |
| // then typeset with calligraphy titles. | |
| import type { Film } from '../engine/Film' | |
| import { drawSeal, FONT_BODY, FONT_TITLE } from '../player/overlay' | |
| import { createGrabber } from './frame' | |
| export type Ratio = '16:9' | '4:3' | '3:4' | |
| export const RATIOS: { id: Ratio; w: number; h: number; label: string }[] = [ | |
| { id: '16:9', w: 1920, h: 1080, label: '16:9 横版(B站 / 视频号 / 抖音横屏)' }, | |
| { id: '4:3', w: 1440, h: 1080, label: '4:3 横版(B站 / 西瓜)' }, | |
| { id: '3:4', w: 1080, h: 1440, label: '3:4 竖版(小红书 / 抖音 / 视频号)' }, | |
| ] | |
| export interface CoverDesign { key: string; name: string; t: number; layout: 'right-vertical' | 'top-center' | 'bottom-left' | 'left-vertical'; line: string } | |
| export const COVER_DESIGNS: CoverDesign[] = [ | |
| { key: 'bridge', name: '石桥夕照', t: 21.2, layout: 'right-vertical', line: '石桥半个月亮 水里补上另一半' }, | |
| { key: 'lantern', name: '灯火长街', t: 63.4, layout: 'top-center', line: '灯笼一盏接一盏亮起' }, | |
| { key: 'night', name: '星夜水乡', t: 84.2, layout: 'bottom-left', line: '从日落到星夜的 86 秒' }, | |
| { key: 'tree', name: '古樟斜阳', t: 45.5, layout: 'left-vertical', line: '挑着担子出去 又挑着担子回来' }, | |
| ] | |
| function vertical(g: CanvasRenderingContext2D, text: string, x: number, y: number, size: number, gap = 1.08) { | |
| Array.from(text).forEach((c, i) => g.fillText(c, x, y + i * size * gap)) | |
| } | |
| export async function renderCover(film: Film, d: CoverDesign, ratio: Ratio): Promise<HTMLCanvasElement> { | |
| const R = RATIOS.find((r) => r.id === ratio)! | |
| const W = R.w, H = R.h | |
| // supersampled + verified render (never a black cover after a GPU hiccup) | |
| const grabber = createGrabber(film, W, H, { sharpen: 0.2 }) | |
| let c: HTMLCanvasElement, g: CanvasRenderingContext2D | |
| try { | |
| await grabber.grab(d.t, false) | |
| c = grabber.comp | |
| g = grabber.g | |
| } finally { | |
| grabber.release() | |
| } | |
| const u = Math.min(W, H) | |
| const portrait = H > W | |
| g.textAlign = 'center' | |
| g.textBaseline = 'middle' | |
| const shadow = (b: number) => { g.shadowColor = 'rgba(20,8,0,0.6)'; g.shadowBlur = b } | |
| // legibility scrims | |
| const scrim = (x0: number, y0: number, x1: number, y1: number, a: number) => { | |
| const grd = g.createLinearGradient(x0, y0, x1, y1) | |
| grd.addColorStop(0, `rgba(8,6,10,${a})`) | |
| grd.addColorStop(1, 'rgba(8,6,10,0)') | |
| g.fillStyle = grd | |
| g.fillRect(0, 0, W, H) | |
| } | |
| if (d.layout === 'right-vertical') { | |
| scrim(W, 0, W * 0.55, 0, 0.45) | |
| shadow(u * 0.03) | |
| g.fillStyle = '#fff4e2' | |
| const size = u * (portrait ? 0.15 : 0.16) | |
| const x = W - u * (portrait ? 0.14 : 0.15) | |
| g.font = `${size}px ${FONT_TITLE}, serif` | |
| vertical(g, '暮归江南', x, H * 0.17, size) | |
| g.font = `${u * 0.036}px ${FONT_BODY}, serif` | |
| g.fillStyle = 'rgba(255,238,215,0.92)' | |
| const parts = d.line.split(' ') | |
| parts.forEach((p, i) => vertical(g, p, x - size * 0.95 - i * u * 0.05, H * 0.2 + i * u * 0.06, u * 0.036, 1.25)) | |
| g.shadowBlur = 0 | |
| drawSeal(g, x - u * 0.04, H * 0.17 + size * 4.35, u * 0.08, '江南') | |
| } else if (d.layout === 'top-center') { | |
| scrim(0, 0, 0, H * 0.5, 0.55) | |
| shadow(u * 0.035) | |
| g.fillStyle = '#fff3df' | |
| g.font = `${u * (portrait ? 0.17 : 0.15)}px ${FONT_TITLE}, serif` | |
| g.fillText('暮归江南', W / 2, H * (portrait ? 0.15 : 0.2)) | |
| g.font = `${u * 0.045}px ${FONT_BODY}, serif` | |
| g.fillStyle = 'rgba(255,230,200,0.95)' | |
| g.fillText(d.line, W / 2, H * (portrait ? 0.15 : 0.2) + u * 0.13) | |
| g.shadowBlur = 0 | |
| drawSeal(g, W / 2 + u * (portrait ? 0.36 : 0.32), H * (portrait ? 0.15 : 0.2) - u * 0.13, u * 0.07, '灯火') | |
| } else if (d.layout === 'bottom-left') { | |
| scrim(0, H, 0, H * 0.45, 0.65) | |
| shadow(u * 0.03) | |
| const x0 = W * 0.07 | |
| g.textAlign = 'left' | |
| g.fillStyle = '#fff3df' | |
| g.font = `${u * 0.16}px ${FONT_TITLE}, serif` | |
| g.fillText('暮归江南', x0, H - u * 0.3) | |
| g.font = `${u * 0.042}px ${FONT_BODY}, serif` | |
| g.fillStyle = 'rgba(235,225,255,0.92)' | |
| g.fillText(d.line, x0 + u * 0.01, H - u * 0.16) | |
| g.shadowBlur = 0 | |
| drawSeal(g, x0 + u * 0.66, H - u * 0.39, u * 0.07, '星夜') | |
| } else { | |
| scrim(0, 0, W * 0.5, 0, 0.5) | |
| shadow(u * 0.03) | |
| g.fillStyle = '#fff4e2' | |
| const size = u * 0.15 | |
| const x = u * 0.15 | |
| g.font = `${size}px ${FONT_TITLE}, serif` | |
| vertical(g, '暮归江南', x, H * 0.17, size) | |
| g.font = `${u * 0.036}px ${FONT_BODY}, serif` | |
| g.fillStyle = 'rgba(255,238,215,0.92)' | |
| d.line.split(' ').forEach((p, i) => vertical(g, p, x + size * 0.95 + i * u * 0.05, H * 0.2 + i * u * 0.06, u * 0.036, 1.25)) | |
| g.shadowBlur = 0 | |
| drawSeal(g, x - u * 0.04, H * 0.17 + size * 4.35, u * 0.08, '江南') | |
| } | |
| // small badge | |
| g.shadowBlur = 0 | |
| g.textAlign = 'left' | |
| g.font = `${u * 0.026}px ${FONT_BODY}, serif` | |
| const badge = '原创3D动画 · 86秒' | |
| const bw = g.measureText(badge).width + u * 0.05 | |
| const bx = d.layout === 'bottom-left' || d.layout === 'right-vertical' ? u * 0.05 : W - bw - u * 0.05 | |
| const by = d.layout === 'bottom-left' ? u * 0.05 : H - u * 0.1 | |
| g.fillStyle = 'rgba(10,8,12,0.45)' | |
| g.beginPath() | |
| g.roundRect(bx, by, bw, u * 0.05, u * 0.025) | |
| g.fill() | |
| g.fillStyle = 'rgba(255,236,210,0.95)' | |
| g.textBaseline = 'middle' | |
| g.fillText(badge, bx + u * 0.025, by + u * 0.026) | |
| return c | |
| } | |
| export function canvasToBlob(c: HTMLCanvasElement, type = 'image/jpeg', q = 0.92) { | |
| return new Promise<Blob>((res, rej) => c.toBlob((b) => (b ? res(b) : rej(new Error('导出失败'))), type, q)) | |
| } | |