page.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. "use client";
  2. import { useState, useEffect } from "react";
  3. import { TEMPLATE_TYPES, PLATFORM_PRESET_KEYS } from "@pipeline/shared";
  4. const TEMPLATES_INFO: Record<string, { label: string; desc: string; color: string }> = {
  5. news: { label: "资讯报道", desc: "Breaking news style with headline cards and ticker", color: "#1a56db" },
  6. knowledge: { label: "知识讲解", desc: "Step-by-step explanation with key point cards", color: "#0d9488" },
  7. opinion: { label: "观点分享", desc: "Quote-style layout for editorial content", color: "#ea580c" },
  8. marketing: { label: "产品营销", desc: "Product showcase with CTA overlays", color: "#9333ea" },
  9. };
  10. const PLATFORMS_INFO: Record<string, string> = {
  11. bilibili: "Bilibili 1920x1080 16:9",
  12. "douyin-long": "抖音 竖屏长视频 1080x1920",
  13. "douyin-short": "抖音 竖屏短视频 1080x1920",
  14. "universal-16x9": "通用横屏 1920x1080",
  15. "universal-9x16": "通用竖屏 1080x1920",
  16. };
  17. const JSON_PLACEHOLDER = `{
  18. "title": "标题",
  19. "subtitle": "副标题",
  20. "cover": {
  21. "keyframes": [
  22. { "type": "text", "content": "要点预览" }
  23. ]
  24. },
  25. "scenes": [
  26. {
  27. "id": "scene-1",
  28. "narration": "配音文本",
  29. "keyframes": [
  30. { "type": "text", "content": "要点一" }
  31. ]
  32. }
  33. ],
  34. "outro": {
  35. "text": "感谢观看",
  36. "cta": "点赞 | 关注"
  37. }
  38. }`;
  39. type Step = "input" | "style" | "voice" | "review";
  40. type InputMode = "manual" | "source";
  41. interface SourceInfo {
  42. name: string;
  43. url: string;
  44. config: Record<string, any>;
  45. }
  46. export default function CreatePage() {
  47. const [step, setStep] = useState<Step>("input");
  48. const [inputMode, setInputMode] = useState<InputMode>("manual");
  49. const [text, setText] = useState("");
  50. const [template, setTemplate] = useState("news");
  51. const [platforms, setPlatforms] = useState<string[]>(["bilibili"]);
  52. const [ttsProvider, setTtsProvider] = useState("openai-tts");
  53. const [voiceId, setVoiceId] = useState("");
  54. const [noTts, setNoTts] = useState(false);
  55. const [submitting, setSubmitting] = useState(false);
  56. const [jobId, setJobId] = useState<string | null>(null);
  57. const [error, setError] = useState<string | null>(null);
  58. // Data source state
  59. const [sources, setSources] = useState<SourceInfo[]>([]);
  60. const [selectedSource, setSelectedSource] = useState("");
  61. const [sourceArgs, setSourceArgs] = useState<Record<string, string>>({});
  62. const [collecting, setCollecting] = useState(false);
  63. const [collectError, setCollectError] = useState<string | null>(null);
  64. useEffect(() => {
  65. fetch("/api/collect/sources")
  66. .then((res) => res.json())
  67. .then((data) => {
  68. setSources(data.sources ?? []);
  69. if (data.sources?.length > 0 && !selectedSource) {
  70. setSelectedSource(data.sources[0].name);
  71. }
  72. })
  73. .catch(() => {});
  74. }, []);
  75. const steps: { key: Step; label: string }[] = [
  76. { key: "input", label: "1. Input" },
  77. { key: "style", label: "2. Template" },
  78. { key: "voice", label: "3. Voice" },
  79. { key: "review", label: "4. Review" },
  80. ];
  81. const stepIndex = steps.findIndex((s) => s.key === step);
  82. async function handleCollect() {
  83. if (!selectedSource) return;
  84. setCollecting(true);
  85. setCollectError(null);
  86. try {
  87. const res = await fetch("/api/collect", {
  88. method: "POST",
  89. headers: { "Content-Type": "application/json" },
  90. body: JSON.stringify({ source: selectedSource, args: sourceArgs }),
  91. });
  92. const data = await res.json();
  93. if (data.error) {
  94. setCollectError(data.error);
  95. } else {
  96. setText(data.content);
  97. }
  98. } catch (err: any) {
  99. setCollectError(err.message);
  100. } finally {
  101. setCollecting(false);
  102. }
  103. }
  104. async function handleRender() {
  105. setSubmitting(true);
  106. setError(null);
  107. try {
  108. const payload: any = {
  109. input: { template, platforms, ttsProvider, voiceId },
  110. options: { noTts },
  111. };
  112. // Prefer already-collected (and possibly edited) text. Only fall back to
  113. // server-side collection if the textarea is still empty.
  114. if (text.trim()) {
  115. payload.input.text = text;
  116. } else if (inputMode === "source" && selectedSource) {
  117. payload.input.source = selectedSource;
  118. if (Object.keys(sourceArgs).length > 0) {
  119. payload.input.sourceArgs = sourceArgs;
  120. }
  121. }
  122. const res = await fetch("/api/render", {
  123. method: "POST",
  124. headers: { "Content-Type": "application/json" },
  125. body: JSON.stringify(payload),
  126. });
  127. const data = await res.json();
  128. if (data.jobId) {
  129. setJobId(data.jobId);
  130. } else {
  131. setError(data.error || "Unknown error");
  132. }
  133. } catch (err: any) {
  134. setError(err.message);
  135. } finally {
  136. setSubmitting(false);
  137. }
  138. }
  139. const canProceed =
  140. inputMode === "manual" ? text.trim().length > 0 : selectedSource.length > 0;
  141. const togglePlatform = (p: string) => {
  142. setPlatforms((prev) =>
  143. prev.includes(p) ? prev.filter((x) => x !== p) : [...prev, p]
  144. );
  145. };
  146. // Source-specific arg fields
  147. const currentSource = sources.find((s) => s.name === selectedSource);
  148. const needsOwnerRepo = currentSource?.name === "github-repo";
  149. if (jobId) {
  150. return (
  151. <div>
  152. <div className="page-header">
  153. <h1>Rendering...</h1>
  154. <p>Your video is being generated</p>
  155. </div>
  156. <div className="card" style={{ textAlign: "center", padding: 48 }}>
  157. <div className="animate-pulse" style={{ fontSize: 48, marginBottom: 16 }}>&#9881;</div>
  158. <p style={{ marginBottom: 16 }}>Job ID: <code>{jobId}</code></p>
  159. <a href={`/jobs/${jobId}`}>
  160. <button className="btn btn-primary">View Job Status</button>
  161. </a>
  162. </div>
  163. </div>
  164. );
  165. }
  166. return (
  167. <div>
  168. <div className="page-header">
  169. <h1>Create Video</h1>
  170. <p>Provide content manually or collect from external data sources</p>
  171. </div>
  172. <div className="steps">
  173. {steps.map((s, i) => (
  174. <div
  175. key={s.key}
  176. className={`step ${s.key === step ? "step-active" : ""} ${i < stepIndex ? "step-done" : ""}`}
  177. >
  178. {s.label}
  179. </div>
  180. ))}
  181. </div>
  182. <div className="card" style={{ padding: 32 }}>
  183. {step === "input" && (
  184. <div>
  185. {/* Mode toggle */}
  186. <div style={{ display: "flex", gap: 12, marginBottom: 20 }}>
  187. <button
  188. className={`btn ${inputMode === "manual" ? "btn-primary" : "btn-secondary"}`}
  189. onClick={() => setInputMode("manual")}
  190. >
  191. Manual Input
  192. </button>
  193. <button
  194. className={`btn ${inputMode === "source" ? "btn-primary" : "btn-secondary"}`}
  195. onClick={() => setInputMode("source")}
  196. >
  197. Data Source
  198. </button>
  199. </div>
  200. {inputMode === "manual" && (
  201. <div className="form-group">
  202. <label>Video JSON</label>
  203. <textarea
  204. className="form-textarea"
  205. value={text}
  206. onChange={(e) => setText(e.target.value)}
  207. placeholder={JSON_PLACEHOLDER}
  208. style={{ minHeight: 300, fontFamily: "monospace", fontSize: 13 }}
  209. />
  210. </div>
  211. )}
  212. {inputMode === "source" && (
  213. <div>
  214. <div className="form-group">
  215. <label>Data Source</label>
  216. <select
  217. className="form-select"
  218. value={selectedSource}
  219. onChange={(e) => {
  220. setSelectedSource(e.target.value);
  221. setSourceArgs({});
  222. setCollectError(null);
  223. }}
  224. >
  225. {sources.map((s) => (
  226. <option key={s.name} value={s.name}>{s.name}</option>
  227. ))}
  228. </select>
  229. </div>
  230. {needsOwnerRepo && (
  231. <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12 }}>
  232. <div className="form-group">
  233. <label>Owner</label>
  234. <input
  235. className="form-input"
  236. value={sourceArgs.owner ?? ""}
  237. onChange={(e) =>
  238. setSourceArgs((prev) => ({ ...prev, owner: e.target.value }))
  239. }
  240. placeholder="e.g. facebook"
  241. />
  242. </div>
  243. <div className="form-group">
  244. <label>Repo</label>
  245. <input
  246. className="form-input"
  247. value={sourceArgs.repo ?? ""}
  248. onChange={(e) =>
  249. setSourceArgs((prev) => ({ ...prev, repo: e.target.value }))
  250. }
  251. placeholder="e.g. react"
  252. />
  253. </div>
  254. </div>
  255. )}
  256. {currentSource && (
  257. <div style={{ fontSize: 13, color: "var(--text-muted)", marginBottom: 16 }}>
  258. URL: <code>{currentSource.url}</code>
  259. </div>
  260. )}
  261. <button
  262. className="btn btn-secondary"
  263. disabled={collecting || !selectedSource}
  264. onClick={handleCollect}
  265. style={{ marginBottom: 16 }}
  266. >
  267. {collecting ? "Collecting..." : "Collect Data"}
  268. </button>
  269. {collectError && (
  270. <div style={{ color: "var(--error)", marginBottom: 16, fontSize: 14 }}>
  271. {collectError}
  272. </div>
  273. )}
  274. {text && (
  275. <div className="form-group">
  276. <label>Collected Content (editable)</label>
  277. <textarea
  278. className="form-textarea"
  279. value={text}
  280. onChange={(e) => setText(e.target.value)}
  281. style={{ minHeight: 200, fontFamily: "monospace", fontSize: 13 }}
  282. />
  283. </div>
  284. )}
  285. </div>
  286. )}
  287. <button
  288. className="btn btn-primary"
  289. disabled={!canProceed}
  290. onClick={() => setStep("style")}
  291. >
  292. Next
  293. </button>
  294. </div>
  295. )}
  296. {step === "style" && (
  297. <div>
  298. <div className="form-group">
  299. <label>Template</label>
  300. <div className="card-grid">
  301. {TEMPLATE_TYPES.map((t) => (
  302. <div
  303. key={t}
  304. className={`card card-hover template-card ${template === t ? "selected" : ""}`}
  305. style={template === t ? { borderColor: "var(--primary)", boxShadow: "0 0 0 1px var(--primary)" } : {}}
  306. onClick={() => setTemplate(t)}
  307. >
  308. <div
  309. className="template-color-bar"
  310. style={{ background: TEMPLATES_INFO[t].color }}
  311. />
  312. <h3>{TEMPLATES_INFO[t].label}</h3>
  313. <p>{TEMPLATES_INFO[t].desc}</p>
  314. </div>
  315. ))}
  316. </div>
  317. </div>
  318. <div className="form-group">
  319. <label>Platforms (select one or more)</label>
  320. <div className="card-grid">
  321. {PLATFORM_PRESET_KEYS.map((p) => {
  322. const selected = platforms.includes(p);
  323. return (
  324. <div
  325. key={p}
  326. className={`card card-hover template-card ${selected ? "selected" : ""}`}
  327. style={selected ? { borderColor: "var(--primary)", boxShadow: "0 0 0 1px var(--primary)" } : {}}
  328. onClick={() => togglePlatform(p)}
  329. >
  330. <h3>{p}</h3>
  331. <p>{PLATFORMS_INFO[p]}</p>
  332. </div>
  333. );
  334. })}
  335. </div>
  336. </div>
  337. <div style={{ display: "flex", gap: 12 }}>
  338. <button className="btn btn-secondary" onClick={() => setStep("input")}>
  339. Back
  340. </button>
  341. <button
  342. className="btn btn-primary"
  343. disabled={platforms.length === 0}
  344. onClick={() => setStep("voice")}
  345. >
  346. Next
  347. </button>
  348. </div>
  349. </div>
  350. )}
  351. {step === "voice" && (
  352. <div>
  353. <div className="form-group">
  354. <label
  355. style={{
  356. display: "flex",
  357. alignItems: "center",
  358. gap: 8,
  359. cursor: "pointer",
  360. userSelect: "none",
  361. }}
  362. >
  363. <input
  364. type="checkbox"
  365. checked={noTts}
  366. onChange={(e) => setNoTts(e.target.checked)}
  367. style={{ width: 16, height: 16 }}
  368. />
  369. Skip TTS (generate silent video)
  370. </label>
  371. {noTts && (
  372. <p
  373. style={{
  374. fontSize: 13,
  375. color: "var(--text-muted)",
  376. marginTop: 8,
  377. marginLeft: 24,
  378. }}
  379. >
  380. No narration will be synthesized. Each scene uses its{" "}
  381. <code>duration</code> (default 5s) and subtitles are spread
  382. evenly across the timeline.
  383. </p>
  384. )}
  385. </div>
  386. <div
  387. className="form-group"
  388. style={{
  389. opacity: noTts ? 0.5 : 1,
  390. pointerEvents: noTts ? "none" : "auto",
  391. }}
  392. >
  393. <label>TTS Provider</label>
  394. <select
  395. className="form-select"
  396. value={ttsProvider}
  397. onChange={(e) => setTtsProvider(e.target.value)}
  398. >
  399. <option value="openai-tts">OpenAI TTS (Seed TTS)</option>
  400. <option value="fish-audio">Fish Audio</option>
  401. <option value="minimax">MiniMax</option>
  402. <option value="elevenlabs">ElevenLabs</option>
  403. </select>
  404. </div>
  405. <div
  406. className="form-group"
  407. style={{
  408. opacity: noTts ? 0.5 : 1,
  409. pointerEvents: noTts ? "none" : "auto",
  410. }}
  411. >
  412. <label>Voice ID (optional)</label>
  413. <input
  414. className="form-input"
  415. value={voiceId}
  416. onChange={(e) => setVoiceId(e.target.value)}
  417. placeholder="Leave empty for default voice"
  418. />
  419. </div>
  420. <div style={{ display: "flex", gap: 12 }}>
  421. <button className="btn btn-secondary" onClick={() => setStep("style")}>
  422. Back
  423. </button>
  424. <button className="btn btn-primary" onClick={() => setStep("review")}>
  425. Next
  426. </button>
  427. </div>
  428. </div>
  429. )}
  430. {step === "review" && (
  431. <div>
  432. <h3 style={{ marginBottom: 20 }}>Review Configuration</h3>
  433. <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16, marginBottom: 24 }}>
  434. <div className="card">
  435. <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 4 }}>Template</div>
  436. <div style={{ fontWeight: 600 }}>{TEMPLATES_INFO[template]?.label} ({template})</div>
  437. </div>
  438. <div className="card">
  439. <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 4 }}>Platforms</div>
  440. <div style={{ fontWeight: 600 }}>{platforms.join(", ")}</div>
  441. </div>
  442. <div className="card">
  443. <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 4 }}>TTS Provider</div>
  444. <div style={{ fontWeight: 600 }}>
  445. {noTts ? "Skipped (silent)" : ttsProvider}
  446. </div>
  447. </div>
  448. <div className="card">
  449. <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 4 }}>Input</div>
  450. <div style={{ fontWeight: 600 }}>
  451. {text.trim()
  452. ? `${text.length} chars`
  453. : inputMode === "source"
  454. ? `Source: ${selectedSource} (will collect at render time)`
  455. : "empty"}
  456. </div>
  457. </div>
  458. </div>
  459. <div className="card" style={{ marginBottom: 24 }}>
  460. <div style={{ fontSize: 12, color: "var(--text-muted)", marginBottom: 8 }}>
  461. {inputMode === "source" ? "Source Content" : "JSON Preview"}
  462. </div>
  463. <pre style={{ fontSize: 13, color: "var(--text-muted)", maxHeight: 120, overflow: "hidden" }}>
  464. {text.slice(0, 500)}{text.length > 500 ? "\n..." : ""}
  465. </pre>
  466. </div>
  467. {error && (
  468. <div style={{ color: "var(--error)", marginBottom: 16 }}>{error}</div>
  469. )}
  470. <div style={{ display: "flex", gap: 12 }}>
  471. <button className="btn btn-secondary" onClick={() => setStep("voice")}>
  472. Back
  473. </button>
  474. <button
  475. className="btn btn-primary"
  476. disabled={submitting}
  477. onClick={handleRender}
  478. >
  479. {submitting ? "Starting..." : "Generate Video"}
  480. </button>
  481. </div>
  482. </div>
  483. )}
  484. </div>
  485. </div>
  486. );
  487. }