server.js 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. const express = require('express');
  2. const { exec, spawn } = require('child_process');
  3. const path = require('path');
  4. const fs = require('fs');
  5. const cors = require('cors');
  6. const crypto = require('crypto');
  7. const multer = require('multer');
  8. const { Readable, Transform } = require('stream');
  9. const { pipeline } = require('stream/promises');
  10. const LOCAL_ENV_PATH = path.join(__dirname, '.env');
  11. if (fs.existsSync(LOCAL_ENV_PATH)) {
  12. const envText = fs.readFileSync(LOCAL_ENV_PATH, 'utf-8');
  13. for (const rawLine of envText.split(/\r?\n/)) {
  14. const line = rawLine.trim();
  15. if (!line || line.startsWith('#')) {
  16. continue;
  17. }
  18. const separatorIndex = line.indexOf('=');
  19. if (separatorIndex <= 0) {
  20. continue;
  21. }
  22. const key = line.slice(0, separatorIndex).trim();
  23. let value = line.slice(separatorIndex + 1).trim();
  24. if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
  25. value = value.slice(1, -1);
  26. }
  27. if (key && process.env[key] === undefined) {
  28. process.env[key] = value;
  29. }
  30. }
  31. }
  32. const app = express();
  33. const PORT = 3000;
  34. const PROJECT_ROOT = __dirname;
  35. const DATA_DIR = path.join(PROJECT_ROOT, 'data');
  36. const DATA_VIDEO_DIR = path.join(DATA_DIR, 'videos');
  37. const DATA_REMIX_ASSET_DIR = path.join(DATA_DIR, 'remix-assets');
  38. const LEGACY_VIDEO_DIR = path.join(PROJECT_ROOT, 'src', 'video');
  39. const VOICE_SPEAKER_ID_DOC_PATH = path.join(PROJECT_ROOT, 'docs', '音色创建', 'speaker_id.md');
  40. const MANIFEST_PATH = path.join(DATA_DIR, 'manifest.json');
  41. const LEGACY_MANIFEST_PATH = path.join(LEGACY_VIDEO_DIR, 'manifest.json');
  42. const WHISPER_DIR = path.join(PROJECT_ROOT, 'Whisper');
  43. const downloadTasks = new Map();
  44. const transcriptTasks = new Map();
  45. const TRANSCRIPT_TEMP_DIR = path.join(DATA_DIR, 'douyin-transcripts');
  46. const DOUYIN_API_BASE_URL = (process.env.DOUYIN_API_BASE_URL || 'https://server.fmode.cn/api/voc-social').replace(/\/+$/, '');
  47. const TRANSCRIPTION_GATEWAY_BASE_URL = (process.env.IFLYTEK_GATEWAY_BASE_URL || 'https://server.fmode.cn/api/apig/transcription').replace(/\/+$/, '');
  48. const DOUYIN_GATEWAY_MAX_ATTEMPTS = Math.max(1, Number(process.env.DOUYIN_GATEWAY_MAX_ATTEMPTS || 4));
  49. const DOUYIN_API_ROUTES = {
  50. searchVideos: { method: 'POST', path: '/douyin/search/fetch_general_search_v2' },
  51. challengeSearch: { method: 'POST', path: '/douyin/search/fetch_challenge_search_v2' },
  52. videoDetail: { method: 'GET', path: '/douyin/app/v3/fetch_one_video_v3' },
  53. userProfileWeb: { method: 'GET', path: '/douyin/web/handler_user_profile_v2' },
  54. userProfileApp: { method: 'GET', path: '/douyin/app/v3/handler_user_profile' },
  55. userPosts: { method: 'GET', path: '/douyin/app/v3/fetch_user_post_videos' },
  56. comments: { method: 'GET', path: '/douyin/app/v3/fetch_video_comments' },
  57. replies: { method: 'GET', path: '/douyin/app/v3/fetch_video_comment_replies' },
  58. };
  59. const QINIU_ACCESS_KEY = process.env.QINIU_AK || 'EXsA-z_n4LGmWrwC088bygcGJtAditnWQe2nH-ZE';
  60. const QINIU_SECRET_KEY = process.env.QINIU_SK || 'HWTL92OL-Tup0-8ex8A9jnG3OaJzTxlF4OwiiDsX';
  61. const QINIU_BUCKET = 'nova-repos';
  62. const QINIU_CDN_DOMAIN = 'https://repos.fmode.cn';
  63. const QINIU_CDN_PREFIX = 'x/openclaw-skills';
  64. const QINIU_UPLOAD_URL = 'https://up-z2.qiniup.com';
  65. const VOLC_SPEECH_BASE_URL = 'https://openspeech.bytedance.com';
  66. const VOLC_TTS_PROXY_BASE_URL = process.env.VOLC_TTS_PROXY_BASE_URL || 'https://server.fmode.cn/api/volcengine/tts';
  67. const VOLC_SPEECH_API_KEY = process.env.VOLC_SPEECH_API_KEY || process.env.BYTEDANCE_SPEECH_API_KEY || process.env.VOICE_CREATION_API_KEY || 'ark-372b5829-8bd6-45a5-a409-0b98b48ac5ce-06368';
  68. const VOLC_TTS_RESOURCE_ID = process.env.VOLC_TTS_RESOURCE_ID || '';
  69. const VOLC_SPEECH_APP_KEY = process.env.VOLC_SPEECH_APP_KEY || process.env.VOLC_SPEECH_APP_ID || '';
  70. const VOLC_SPEECH_ACCESS_KEY = process.env.VOLC_SPEECH_ACCESS_KEY || '';
  71. app.use(cors());
  72. app.use(express.json({ limit: '50mb' }));
  73. // 静态文件:提供视频文件的访问
  74. const staticVideoOptions = {
  75. setHeaders: (res, filePath) => {
  76. if (filePath.match(/\.(mp4|mov|webm|mkv|avi)$/i)) {
  77. res.setHeader('Content-Type', 'video/mp4');
  78. res.setHeader('Accept-Ranges', 'bytes');
  79. }
  80. }
  81. };
  82. app.use('/api/video', express.static(DATA_VIDEO_DIR, staticVideoOptions));
  83. app.use('/api/video', express.static(LEGACY_VIDEO_DIR, staticVideoOptions));
  84. // ==================== 文件上传配置 ====================
  85. const uploadStorage = multer.diskStorage({
  86. destination: (req, file, cb) => {
  87. const uploadDir = DATA_VIDEO_DIR;
  88. if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir, { recursive: true });
  89. cb(null, uploadDir);
  90. },
  91. filename: (req, file, cb) => {
  92. // 保留原始文件名,如有冲突则加时间戳
  93. const originalName = Buffer.from(file.originalname, 'latin1').toString('utf8');
  94. const ext = path.extname(originalName);
  95. const baseName = path.basename(originalName, ext);
  96. const targetPath = path.join(DATA_VIDEO_DIR, originalName);
  97. if (fs.existsSync(targetPath)) {
  98. cb(null, `${baseName}-${Date.now()}${ext}`);
  99. } else {
  100. cb(null, originalName);
  101. }
  102. }
  103. });
  104. const assetUpload = multer({
  105. storage: multer.memoryStorage(),
  106. // 200MB 以支持动作迁移 / 素材拼接场景的参考视频上传
  107. limits: { fileSize: 200 * 1024 * 1024 }
  108. });
  109. const upload = multer({
  110. storage: uploadStorage,
  111. limits: { fileSize: 500 * 1024 * 1024 }, // 500MB
  112. fileFilter: (req, file, cb) => {
  113. const allowedTypes = ['video/mp4', 'video/quicktime', 'video/x-msvideo', 'video/webm', 'video/x-matroska'];
  114. if (allowedTypes.includes(file.mimetype) || file.originalname.match(/\.(mp4|mov|avi|webm|mkv)$/i)) {
  115. cb(null, true);
  116. } else {
  117. cb(new Error('仅支持视频文件(mp4, mov, avi, webm, mkv)'));
  118. }
  119. }
  120. });
  121. // ==================== 工具函数 ====================
  122. // 确保 data 目录存在
  123. if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
  124. if (!fs.existsSync(DATA_VIDEO_DIR)) fs.mkdirSync(DATA_VIDEO_DIR, { recursive: true });
  125. if (!fs.existsSync(DATA_REMIX_ASSET_DIR)) fs.mkdirSync(DATA_REMIX_ASSET_DIR, { recursive: true });
  126. function ensureManifestFile() {
  127. if (fs.existsSync(MANIFEST_PATH)) return;
  128. if (fs.existsSync(LEGACY_MANIFEST_PATH)) {
  129. fs.copyFileSync(LEGACY_MANIFEST_PATH, MANIFEST_PATH);
  130. return;
  131. }
  132. fs.writeFileSync(MANIFEST_PATH, '[]', 'utf-8');
  133. }
  134. function readManifest() {
  135. ensureManifestFile();
  136. const raw = fs.readFileSync(MANIFEST_PATH, 'utf-8');
  137. return JSON.parse(raw);
  138. }
  139. function writeManifest(data) {
  140. ensureManifestFile();
  141. fs.writeFileSync(MANIFEST_PATH, JSON.stringify(data, null, 2), 'utf-8');
  142. }
  143. function resolveVideoPath(filename) {
  144. const runtimePath = path.join(DATA_VIDEO_DIR, filename);
  145. if (fs.existsSync(runtimePath)) return runtimePath;
  146. const legacyPath = path.join(LEGACY_VIDEO_DIR, filename);
  147. if (fs.existsSync(legacyPath)) return legacyPath;
  148. return runtimePath;
  149. }
  150. function removeFileIfExists(filePath) {
  151. try {
  152. if (filePath && fs.existsSync(filePath)) {
  153. fs.unlinkSync(filePath);
  154. }
  155. } catch (err) {
  156. // 忽略竞态条件 / 权限问题导致的删除失败,避免 ENOENT 等异常中断主流程
  157. if (err && err.code !== 'ENOENT') {
  158. console.warn(`⚠️ 删除文件失败 ${filePath}: ${err.message}`);
  159. }
  160. }
  161. }
  162. function removeDirectoryIfExists(dirPath) {
  163. if (fs.existsSync(dirPath)) {
  164. fs.rmSync(dirPath, { recursive: true, force: true });
  165. }
  166. }
  167. function toBase64Url(input) {
  168. return Buffer.from(input)
  169. .toString('base64')
  170. .replace(/\+/g, '-')
  171. .replace(/\//g, '_');
  172. }
  173. function buildQiniuUploadToken(key) {
  174. const deadline = Math.floor(Date.now() / 1000) + 3600;
  175. const putPolicy = {
  176. scope: `${QINIU_BUCKET}:${key}`,
  177. deadline
  178. };
  179. const encodedPutPolicy = toBase64Url(JSON.stringify(putPolicy));
  180. const sign = crypto
  181. .createHmac('sha1', QINIU_SECRET_KEY)
  182. .update(encodedPutPolicy)
  183. .digest('base64')
  184. .replace(/\+/g, '-')
  185. .replace(/\//g, '_');
  186. return `${QINIU_ACCESS_KEY}:${sign}:${encodedPutPolicy}`;
  187. }
  188. function buildDigitalHumanAssetKey(fileName, kind) {
  189. const rawName = Buffer.from(fileName || `asset-${Date.now()}`, 'latin1').toString('utf8');
  190. const ext = path.extname(rawName).toLowerCase();
  191. const safeExt = ext && /^[.a-z0-9]+$/i.test(ext) ? ext : '';
  192. const baseName = path.basename(rawName, ext).replace(/[^a-zA-Z0-9_-]/g, '_') || `asset-${Date.now()}`;
  193. const date = new Date();
  194. const yyyy = date.getFullYear();
  195. const mm = String(date.getMonth() + 1).padStart(2, '0');
  196. const dd = String(date.getDate()).padStart(2, '0');
  197. const timestamp = `${yyyy}${mm}${dd}-${Date.now()}`;
  198. return `${QINIU_CDN_PREFIX}/digital-human/${kind}/${yyyy}${mm}${dd}/${timestamp}-${baseName}${safeExt}`;
  199. }
  200. function createRequestId() {
  201. return typeof crypto.randomUUID === 'function'
  202. ? crypto.randomUUID()
  203. : `${Date.now()}-${crypto.randomBytes(8).toString('hex')}`;
  204. }
  205. function buildSpeechHeaders(contentType = 'application/json') {
  206. const headers = {
  207. 'Content-Type': contentType,
  208. 'X-Api-Request-Id': createRequestId()
  209. };
  210. if (VOLC_SPEECH_API_KEY) {
  211. headers['X-Api-Key'] = VOLC_SPEECH_API_KEY;
  212. return headers;
  213. }
  214. if (VOLC_SPEECH_APP_KEY && VOLC_SPEECH_ACCESS_KEY) {
  215. headers['X-Api-App-Key'] = VOLC_SPEECH_APP_KEY;
  216. headers['X-Api-Access-Key'] = VOLC_SPEECH_ACCESS_KEY;
  217. return headers;
  218. }
  219. throw new Error('未配置火山语音鉴权,请设置 VOLC_SPEECH_API_KEY 或 VOLC_SPEECH_APP_KEY + VOLC_SPEECH_ACCESS_KEY');
  220. }
  221. function normalizeOfficialSpeechResourceId(value) {
  222. const resourceId = String(value || '').trim();
  223. return /^seed-(tts|icl)-/i.test(resourceId) ? resourceId : '';
  224. }
  225. function inferOfficialSpeechResourceId(speaker, requestedResourceId = '') {
  226. const explicitResourceId = normalizeOfficialSpeechResourceId(requestedResourceId);
  227. if (explicitResourceId) {
  228. return explicitResourceId;
  229. }
  230. const configuredResourceId = normalizeOfficialSpeechResourceId(VOLC_TTS_RESOURCE_ID);
  231. if (configuredResourceId) {
  232. return configuredResourceId;
  233. }
  234. const normalizedSpeaker = String(speaker || '').trim();
  235. if (/^(S_|icl_|saturn_|dit_)/i.test(normalizedSpeaker)) {
  236. return 'seed-icl-2.0';
  237. }
  238. return 'seed-tts-2.0';
  239. }
  240. function clampNumber(value, fallback, min, max) {
  241. const numericValue = Number(value);
  242. if (!Number.isFinite(numericValue)) {
  243. return fallback;
  244. }
  245. return Math.min(max, Math.max(min, numericValue));
  246. }
  247. function clampOptionalNumber(value, min, max) {
  248. if (value === null || value === undefined || String(value).trim() === '') {
  249. return null;
  250. }
  251. const numericValue = Number(value);
  252. if (!Number.isFinite(numericValue)) {
  253. return null;
  254. }
  255. return Math.min(max, Math.max(min, numericValue));
  256. }
  257. function parseOptionalBoolean(value, fallback = false) {
  258. if (value === null || value === undefined || value === '') {
  259. return fallback;
  260. }
  261. if (typeof value === 'boolean') {
  262. return value;
  263. }
  264. if (typeof value === 'number') {
  265. return value !== 0;
  266. }
  267. const normalizedValue = String(value).trim().toLowerCase();
  268. if (['true', '1', 'yes', 'on'].includes(normalizedValue)) {
  269. return true;
  270. }
  271. if (['false', '0', 'no', 'off'].includes(normalizedValue)) {
  272. return false;
  273. }
  274. return fallback;
  275. }
  276. function normalizeVoiceSynthesisErrorMessage(message) {
  277. const rawMessage = String(message || '').trim();
  278. if (/resource ID is mismatched with speaker related resource/i.test(rawMessage)) {
  279. return '当前本地 X-Api-Key 与所选 speaker_id 不属于同一语音资源,无法直接使用 speaker_id 合成。请先通过音色复刻获取 timbreId 后再合成,或更换与该 speaker_id 匹配的 X-Api-Key。';
  280. }
  281. return rawMessage || '语音合成失败';
  282. }
  283. function mapVoiceDesignStatus(status) {
  284. switch (Number(status)) {
  285. case 0:
  286. return '未找到';
  287. case 1:
  288. return '训练中';
  289. case 2:
  290. return '可用';
  291. case 3:
  292. return '失败';
  293. case 4:
  294. return '已激活';
  295. default:
  296. return '未知';
  297. }
  298. }
  299. function mapVoiceCloneStatus(status) {
  300. switch (String(status ?? '')) {
  301. case '0':
  302. return '未占用';
  303. case '1':
  304. return '可用';
  305. case '2':
  306. return '训练中';
  307. case '3':
  308. return '失败';
  309. case '404':
  310. return '已删除';
  311. default:
  312. return '未知';
  313. }
  314. }
  315. function normalizeBearerToken(token) {
  316. const rawToken = String(token || '').trim();
  317. if (!rawToken) {
  318. return '';
  319. }
  320. return /^Bearer\s+/i.test(rawToken) ? rawToken : `Bearer ${rawToken}`;
  321. }
  322. function inferAudioFormat(file) {
  323. const mimeType = String(file?.mimetype || '').toLowerCase();
  324. if (mimeType.includes('mpeg') || mimeType.includes('mp3')) {
  325. return 'mp3';
  326. }
  327. if (mimeType.includes('wav')) {
  328. return 'wav';
  329. }
  330. if (mimeType.includes('m4a') || mimeType.includes('mp4')) {
  331. return 'm4a';
  332. }
  333. if (mimeType.includes('aac')) {
  334. return 'aac';
  335. }
  336. if (mimeType.includes('flac')) {
  337. return 'flac';
  338. }
  339. if (mimeType.includes('ogg') || mimeType.includes('opus')) {
  340. return 'ogg_opus';
  341. }
  342. if (mimeType.includes('pcm')) {
  343. return 'pcm';
  344. }
  345. const ext = path.extname(file?.originalname || '').toLowerCase();
  346. switch (ext) {
  347. case '.wav':
  348. return 'wav';
  349. case '.m4a':
  350. return 'm4a';
  351. case '.aac':
  352. return 'aac';
  353. case '.flac':
  354. return 'flac';
  355. case '.ogg':
  356. case '.opus':
  357. return 'ogg_opus';
  358. case '.pcm':
  359. return 'pcm';
  360. case '.mp3':
  361. default:
  362. return 'mp3';
  363. }
  364. }
  365. async function readFetchResponse(response) {
  366. const rawText = await response.text();
  367. let payload = null;
  368. try {
  369. payload = rawText ? JSON.parse(rawText) : null;
  370. } catch {
  371. payload = null;
  372. }
  373. return {
  374. rawText,
  375. payload
  376. };
  377. }
  378. function extractPrimaryVoiceModel(timbre) {
  379. if (!Array.isArray(timbre?.models) || timbre.models.length === 0) {
  380. return {};
  381. }
  382. return timbre.models[0] || {};
  383. }
  384. function readVoiceSpeakerIdOptions() {
  385. if (!fs.existsSync(VOICE_SPEAKER_ID_DOC_PATH)) {
  386. return [];
  387. }
  388. const rawText = fs.readFileSync(VOICE_SPEAKER_ID_DOC_PATH, 'utf-8');
  389. const seen = new Set();
  390. const options = [];
  391. for (const rawLine of rawText.split(/\r?\n/)) {
  392. const speakerId = rawLine.trim().match(/^S_[A-Za-z0-9]+$/)?.[0] || '';
  393. if (!speakerId || seen.has(speakerId)) {
  394. continue;
  395. }
  396. seen.add(speakerId);
  397. options.push(speakerId);
  398. }
  399. return options;
  400. }
  401. function upsertVoiceProfile(profile) {
  402. const profiles = readDataFile('voice-profiles');
  403. const existingIndex = profiles.findIndex((item) => (
  404. (!!profile.timbre_id && item.timbre_id === profile.timbre_id)
  405. || (!!profile.id && item.id === profile.id)
  406. ));
  407. if (existingIndex >= 0) {
  408. const existingProfile = profiles[existingIndex];
  409. const nextProfile = {
  410. ...existingProfile,
  411. ...profile,
  412. id: existingProfile.id || profile.id,
  413. created_at: existingProfile.created_at || profile.created_at,
  414. updated_at: profile.updated_at || new Date().toISOString()
  415. };
  416. profiles[existingIndex] = nextProfile;
  417. writeDataFile('voice-profiles', profiles);
  418. return nextProfile;
  419. }
  420. profiles.unshift(profile);
  421. writeDataFile('voice-profiles', profiles);
  422. return profile;
  423. }
  424. async function requestVolcTtsJson(endpoint, requestBody) {
  425. const response = await fetch(`${VOLC_TTS_PROXY_BASE_URL}/${endpoint}`, {
  426. method: 'POST',
  427. headers: {
  428. 'Content-Type': 'application/json'
  429. },
  430. body: JSON.stringify(requestBody)
  431. });
  432. const { rawText, payload } = await readFetchResponse(response);
  433. if (!response.ok) {
  434. const error = new Error(payload?.error?.message || payload?.message || payload?.error || rawText || 'TTS 请求失败');
  435. error.status = response.status;
  436. error.detail = payload || rawText || '';
  437. throw error;
  438. }
  439. return payload;
  440. }
  441. async function proxyVolcTtsStream(endpoint, requestBody, res) {
  442. const response = await fetch(`${VOLC_TTS_PROXY_BASE_URL}/${endpoint}`, {
  443. method: 'POST',
  444. headers: {
  445. 'Content-Type': 'application/json'
  446. },
  447. body: JSON.stringify(requestBody)
  448. });
  449. if (!response.ok) {
  450. const { rawText, payload } = await readFetchResponse(response);
  451. const error = new Error(payload?.error?.message || payload?.message || payload?.error || rawText || 'TTS 流式请求失败');
  452. error.status = response.status;
  453. error.detail = payload || rawText || '';
  454. throw error;
  455. }
  456. res.status(response.status);
  457. res.setHeader('Content-Type', response.headers.get('content-type') || 'application/x-ndjson; charset=utf-8');
  458. res.setHeader('Cache-Control', 'no-cache, no-transform');
  459. if (!response.body) {
  460. res.end();
  461. return;
  462. }
  463. await pipeline(Readable.fromWeb(response.body), res);
  464. }
  465. async function synthesizeVoiceAudio({ token, speaker, timbreId, text, ssml, xApiResourceId, model, isStream = false, audioParams = {}, additions = {} }) {
  466. const normalizedSpeaker = String(speaker || '').trim();
  467. const fallbackSpeaker = String(timbreId || '').trim();
  468. const resolvedSpeaker = normalizedSpeaker || fallbackSpeaker;
  469. const requestedResourceId = String(xApiResourceId || '').trim();
  470. const headerResourceId = inferOfficialSpeechResourceId(resolvedSpeaker, requestedResourceId);
  471. const resolvedModel = String(model || '').trim();
  472. const serializedAdditions = Object.keys(additions).length > 0 ? JSON.stringify(additions) : '';
  473. const requestBody = {
  474. req_params: {
  475. ...(ssml ? { ssml } : { text }),
  476. speaker: resolvedSpeaker,
  477. audio_params: {
  478. format: audioParams.format || 'mp3',
  479. sample_rate: audioParams.sampleRate ?? 24000,
  480. ...(audioParams.speechRate === null || audioParams.speechRate === undefined ? {} : { speech_rate: audioParams.speechRate }),
  481. ...(audioParams.loudnessRate === null || audioParams.loudnessRate === undefined ? {} : { loudness_rate: audioParams.loudnessRate }),
  482. ...(audioParams.emotion ? { emotion: audioParams.emotion } : {}),
  483. ...(audioParams.emotionScale === null || audioParams.emotionScale === undefined ? {} : { emotion_scale: audioParams.emotionScale }),
  484. ...(audioParams.enableSubtitle ? { enable_subtitle: true } : {})
  485. },
  486. ...(serializedAdditions ? { additions: serializedAdditions } : {}),
  487. ...(resolvedModel ? { model: resolvedModel } : {})
  488. }
  489. };
  490. const headers = buildSpeechHeaders();
  491. headers['X-Api-Resource-Id'] = headerResourceId;
  492. headers['Connection'] = 'keep-alive';
  493. const response = await fetch(`${VOLC_SPEECH_BASE_URL}/api/v3/tts/unidirectional`, {
  494. method: 'POST',
  495. headers,
  496. body: JSON.stringify(requestBody)
  497. });
  498. const { rawText, payload } = await readFetchResponse(response);
  499. if (!response.ok) {
  500. const error = new Error(normalizeVoiceSynthesisErrorMessage(payload?.error?.message || payload?.message || payload?.error || rawText || 'TTS 请求失败'));
  501. error.status = response.status;
  502. error.detail = payload || rawText || '';
  503. throw error;
  504. }
  505. if (Number(payload?.code) !== 200 || !payload?.data?.audioUrl) {
  506. const error = new Error(normalizeVoiceSynthesisErrorMessage(payload?.error?.message || payload?.message || '语音合成失败'));
  507. error.status = 502;
  508. error.detail = payload || '';
  509. throw error;
  510. }
  511. return {
  512. audioUrl: payload.data.audioUrl,
  513. workId: payload.data.workId || '',
  514. payload
  515. };
  516. }
  517. function createHttpError(message, status = 500, detail = '') {
  518. const error = new Error(message || '请求失败');
  519. error.status = status;
  520. error.detail = detail;
  521. return error;
  522. }
  523. function inferAudioFormatFromUrl(url) {
  524. try {
  525. const parsed = new URL(String(url || '').trim());
  526. return inferAudioFormat({ originalname: parsed.pathname || '', mimetype: '' });
  527. } catch {
  528. return inferAudioFormat({ originalname: String(url || '').trim(), mimetype: '' });
  529. }
  530. }
  531. function normalizeVoiceClonePayload(body) {
  532. const audioData = body?.audioData && typeof body.audioData === 'object' ? body.audioData : {};
  533. const extraParams = body?.extra_params && typeof body.extra_params === 'object' ? body.extra_params : {};
  534. return {
  535. token: normalizeBearerToken(body?.token),
  536. name: String(body?.name || body?.displayName || '').trim(),
  537. timbreId: String(body?.timbreId || '').trim(),
  538. speakerId: String(body?.speaker_id || body?.speakerId || '').trim(),
  539. audioData: {
  540. url: String(audioData?.url || '').trim(),
  541. base64: String(audioData?.base64 || '').trim(),
  542. format: String(audioData?.format || '').trim().toLowerCase(),
  543. text: String(audioData?.text || body?.audioText || '').trim()
  544. },
  545. language: Number(body?.language ?? 0) === 1 ? 1 : 0,
  546. demoText: String(extraParams?.demo_text || body?.sampleText || '').trim(),
  547. sourceAudioName: String(body?.sourceAudioName || body?.source_audio_name || '').trim(),
  548. sourceAudioFormat: String(body?.sourceAudioFormat || body?.source_audio_format || '').trim().toLowerCase()
  549. };
  550. }
  551. function normalizeVoiceSynthesisPayload(body) {
  552. const rawAudioParams = body?.audio_params && typeof body.audio_params === 'object'
  553. ? body.audio_params
  554. : (body?.audioParams && typeof body.audioParams === 'object' ? body.audioParams : {});
  555. const rawAdditions = body?.additions && typeof body.additions === 'object' ? body.additions : {};
  556. const formatCandidate = String(rawAudioParams?.format || '').trim().toLowerCase();
  557. const format = ['mp3', 'ogg_opus', 'pcm'].includes(formatCandidate) ? formatCandidate : 'mp3';
  558. const sampleRateCandidate = Number(rawAudioParams?.sampleRate ?? rawAudioParams?.sample_rate);
  559. const sampleRate = [8000, 16000, 22050, 24000, 32000, 44100, 48000].includes(sampleRateCandidate) ? sampleRateCandidate : 24000;
  560. const speechRate = clampNumber(rawAudioParams?.speechRate ?? rawAudioParams?.speech_rate, 0, -50, 100);
  561. const loudnessRate = clampNumber(rawAudioParams?.loudnessRate ?? rawAudioParams?.loudness_rate, 0, -50, 100);
  562. const emotion = String(rawAudioParams?.emotion || '').trim();
  563. const emotionScale = clampOptionalNumber(rawAudioParams?.emotionScale ?? rawAudioParams?.emotion_scale, 1, 5);
  564. const enableSubtitle = parseOptionalBoolean(rawAudioParams?.enableSubtitle ?? rawAudioParams?.enable_subtitle, false);
  565. const silenceDuration = clampOptionalNumber(rawAdditions?.silenceDuration ?? rawAdditions?.silence_duration, 0, 30000);
  566. const enableLanguageDetector = parseOptionalBoolean(rawAdditions?.enableLanguageDetector ?? rawAdditions?.enable_language_detector, false);
  567. const disableMarkdownFilter = parseOptionalBoolean(rawAdditions?.disableMarkdownFilter ?? rawAdditions?.disable_markdown_filter, false);
  568. const disableEmojiFilter = parseOptionalBoolean(rawAdditions?.disableEmojiFilter ?? rawAdditions?.disable_emoji_filter, false);
  569. const explicitLanguage = String((rawAdditions?.explicitLanguage ?? rawAdditions?.explicit_language) || '').trim();
  570. return {
  571. token: normalizeBearerToken(body?.token),
  572. text: String(body?.text || '').trim(),
  573. ssml: String(body?.ssml || '').trim(),
  574. timbreId: String(body?.timbreId || body?.timbre_id || '').trim(),
  575. speakerId: String(body?.speaker_id || body?.speakerId || body?.speaker || '').trim(),
  576. isStream: parseOptionalBoolean(body?.isStream ?? body?.is_stream, true),
  577. xApiResourceId: String(body?.x_api_resource_id || body?.xApiResourceId || '').trim(),
  578. model: String(body?.model || '').trim(),
  579. audioParams: {
  580. format,
  581. sampleRate,
  582. speechRate,
  583. loudnessRate,
  584. emotion,
  585. emotionScale,
  586. enableSubtitle
  587. },
  588. additions: {
  589. ...(silenceDuration === null ? {} : { silence_duration: silenceDuration }),
  590. ...(enableLanguageDetector ? { enable_language_detector: true } : {}),
  591. ...(disableMarkdownFilter ? { disable_markdown_filter: true } : {}),
  592. ...(disableEmojiFilter ? { disable_emoji_filter: true } : {}),
  593. ...(explicitLanguage ? { explicit_language: explicitLanguage } : {})
  594. }
  595. };
  596. }
  597. function buildProxySynthesisPayload(payload) {
  598. return {
  599. token: payload.token,
  600. ...(payload.text ? { text: payload.text } : {}),
  601. ...(payload.ssml ? { ssml: payload.ssml } : {}),
  602. ...(payload.timbreId ? { timbreId: payload.timbreId } : {}),
  603. ...(payload.speakerId ? { speaker_id: payload.speakerId } : {}),
  604. isStream: !!payload.isStream,
  605. ...(payload.xApiResourceId ? { x_api_resource_id: payload.xApiResourceId } : {}),
  606. ...(payload.model ? { model: payload.model } : {}),
  607. audio_params: {
  608. format: payload.audioParams.format,
  609. sample_rate: payload.audioParams.sampleRate,
  610. speech_rate: payload.audioParams.speechRate,
  611. loudness_rate: payload.audioParams.loudnessRate,
  612. ...(payload.audioParams.emotion ? { emotion: payload.audioParams.emotion } : {}),
  613. ...(payload.audioParams.emotionScale === null || payload.audioParams.emotionScale === undefined ? {} : { emotion_scale: payload.audioParams.emotionScale }),
  614. ...(payload.audioParams.enableSubtitle ? { enable_subtitle: true } : {})
  615. },
  616. ...(Object.keys(payload.additions).length > 0 ? { additions: payload.additions } : {})
  617. };
  618. }
  619. async function executeVoiceCloneRequest(payload) {
  620. if (!payload.token) {
  621. throw createHttpError('缺少 token 参数', 400);
  622. }
  623. if (!payload.name) {
  624. throw createHttpError('缺少音色名称 name', 400);
  625. }
  626. if (!payload.timbreId && !payload.speakerId) {
  627. throw createHttpError('timbreId与speaker_id不能同时为空', 400);
  628. }
  629. if (!payload.audioData.url && !payload.audioData.base64) {
  630. throw createHttpError('音频url或base64至少提供一个', 400);
  631. }
  632. if (payload.demoText && (payload.demoText.length < 4 || payload.demoText.length > 80)) {
  633. throw createHttpError('试听文本长度需在 4-80 字之间', 400);
  634. }
  635. const clonePayload = await requestVolcTtsJson('voice_clone', {
  636. token: payload.token,
  637. name: payload.name,
  638. ...(payload.timbreId ? { timbreId: payload.timbreId } : {}),
  639. ...(payload.speakerId ? { speaker_id: payload.speakerId } : {}),
  640. audioData: {
  641. ...(payload.audioData.url ? { url: payload.audioData.url } : {}),
  642. ...(payload.audioData.base64 ? { base64: payload.audioData.base64 } : {}),
  643. ...(payload.audioData.format ? { format: payload.audioData.format } : {}),
  644. ...(payload.audioData.text ? { text: payload.audioData.text } : {})
  645. },
  646. language: payload.language,
  647. ...(payload.demoText ? { extra_params: { demo_text: payload.demoText } } : {})
  648. });
  649. if (Number(clonePayload?.code) !== 200 || !clonePayload?.data?.timbre?.objectId) {
  650. throw createHttpError(clonePayload?.error?.message || clonePayload?.message || '音色复刻失败', 502, clonePayload || '');
  651. }
  652. const timbre = clonePayload.data.timbre;
  653. const primaryModel = extractPrimaryVoiceModel(timbre);
  654. const now = new Date().toISOString();
  655. const profile = upsertVoiceProfile({
  656. id: `VOICE-${Date.now()}`,
  657. name: payload.name,
  658. creation_mode: 'clone',
  659. speaker_id: timbre?.speaker_id || payload.speakerId,
  660. timbre_id: timbre?.objectId || payload.timbreId || '',
  661. sample_text: payload.demoText,
  662. source_audio_text: payload.audioData.text,
  663. source_audio_name: payload.sourceAudioName,
  664. source_audio_format: payload.sourceAudioFormat || payload.audioData.format || inferAudioFormatFromUrl(payload.audioData.url),
  665. text_prompt: '',
  666. language: payload.language,
  667. status: Number.isFinite(Number(timbre?.status)) ? Number(timbre.status) : null,
  668. status_label: mapVoiceCloneStatus(timbre?.status),
  669. demo_audio: primaryModel?.demo_audio || '',
  670. available_training_times: null,
  671. image_prompt_name: '',
  672. x_api_resource_id: Array.isArray(primaryModel?.x_api_resource_id) ? String(primaryModel.x_api_resource_id[0] || '') : '',
  673. model_version: String(primaryModel?.version || ''),
  674. icl_speaker_id: String(primaryModel?.icl_speaker_id || ''),
  675. occupied: !!timbre?.occupied,
  676. synthesized_audio_url: '',
  677. synthesized_work_id: '',
  678. last_synthesis_text: '',
  679. latest_audio_url: primaryModel?.demo_audio || '',
  680. message: clonePayload?.data?.tip || '音色复刻成功',
  681. request_id: clonePayload?.request_id || '',
  682. created_at: now,
  683. updated_at: now
  684. });
  685. return {
  686. clonePayload,
  687. profile
  688. };
  689. }
  690. function findVoiceProfileForSynthesis(payload) {
  691. const profiles = readDataFile('voice-profiles');
  692. if (payload.timbreId) {
  693. const byTimbreId = profiles.find((item) => item.timbre_id === payload.timbreId);
  694. if (byTimbreId) {
  695. return byTimbreId;
  696. }
  697. }
  698. if (payload.speakerId) {
  699. return profiles.find((item) => (
  700. String(item.icl_speaker_id || '').trim() === payload.speakerId
  701. || String(item.speaker_id || '').trim() === payload.speakerId
  702. )) || null;
  703. }
  704. return null;
  705. }
  706. function persistSynthesisProfile(profile, payload, synthesis) {
  707. if (!profile) {
  708. return null;
  709. }
  710. return upsertVoiceProfile({
  711. ...profile,
  712. synthesized_audio_url: synthesis.audioUrl,
  713. synthesized_work_id: synthesis.workId,
  714. last_synthesis_text: payload.text,
  715. last_synthesis_ssml: payload.ssml,
  716. last_synthesis_x_api_resource_id: payload.xApiResourceId || profile.x_api_resource_id || '',
  717. last_synthesis_model: payload.model,
  718. last_synthesis_format: payload.audioParams.format,
  719. last_synthesis_sample_rate: payload.audioParams.sampleRate,
  720. last_synthesis_speech_rate: payload.audioParams.speechRate,
  721. last_synthesis_loudness_rate: payload.audioParams.loudnessRate,
  722. last_synthesis_emotion: payload.audioParams.emotion,
  723. last_synthesis_emotion_scale: payload.audioParams.emotionScale,
  724. last_synthesis_enable_subtitle: !!payload.audioParams.enableSubtitle,
  725. last_synthesis_silence_duration: payload.additions?.silence_duration ?? null,
  726. last_synthesis_enable_language_detector: !!payload.additions?.enable_language_detector,
  727. last_synthesis_disable_markdown_filter: !!payload.additions?.disable_markdown_filter,
  728. last_synthesis_disable_emoji_filter: !!payload.additions?.disable_emoji_filter,
  729. last_synthesis_explicit_language: String(payload.additions?.explicit_language || '').trim(),
  730. latest_audio_url: synthesis.audioUrl || profile.latest_audio_url || profile.demo_audio || '',
  731. updated_at: new Date().toISOString()
  732. });
  733. }
  734. async function executeVoiceSynthesisRequest(payload) {
  735. if (!payload.token) {
  736. throw createHttpError('缺少 token 参数', 400);
  737. }
  738. if (!payload.text && !payload.ssml) {
  739. throw createHttpError('文本内容不能为空,text与ssml不能同时为空', 400);
  740. }
  741. const matchedProfile = findVoiceProfileForSynthesis(payload);
  742. if (payload.timbreId) {
  743. const proxyPayload = await requestVolcTtsJson('unidirectional', buildProxySynthesisPayload({
  744. ...payload,
  745. isStream: false
  746. }));
  747. if (Number(proxyPayload?.code) !== 200 || !proxyPayload?.data?.audioUrl) {
  748. throw createHttpError(proxyPayload?.error?.message || proxyPayload?.message || '语音合成失败', 502, proxyPayload || '');
  749. }
  750. const profile = persistSynthesisProfile(matchedProfile, payload, {
  751. audioUrl: proxyPayload.data.audioUrl,
  752. workId: proxyPayload.data.workId || ''
  753. });
  754. return {
  755. response: {
  756. ...proxyPayload,
  757. ...(profile ? { profile } : {})
  758. },
  759. profile
  760. };
  761. }
  762. if (!payload.speakerId) {
  763. throw createHttpError('音色id不能为空', 400);
  764. }
  765. const synthesis = await synthesizeVoiceAudio({
  766. token: payload.token,
  767. speaker: payload.speakerId,
  768. timbreId: matchedProfile?.timbre_id || '',
  769. text: payload.text,
  770. ssml: payload.ssml,
  771. xApiResourceId: payload.xApiResourceId || matchedProfile?.x_api_resource_id || '',
  772. model: payload.model,
  773. isStream: false,
  774. audioParams: payload.audioParams,
  775. additions: payload.additions
  776. });
  777. const profile = persistSynthesisProfile(matchedProfile, payload, synthesis);
  778. return {
  779. response: {
  780. code: 200,
  781. data: {
  782. workId: synthesis.workId,
  783. audioUrl: synthesis.audioUrl
  784. },
  785. ...(profile ? { profile } : {})
  786. },
  787. profile
  788. };
  789. }
  790. // 通用 JSON 数据文件读写
  791. function readDataFile(name) {
  792. const p = path.join(DATA_DIR, `${name}.json`);
  793. if (!fs.existsSync(p)) { fs.writeFileSync(p, '[]', 'utf-8'); return []; }
  794. return JSON.parse(fs.readFileSync(p, 'utf-8'));
  795. }
  796. function writeDataFile(name, data) {
  797. fs.writeFileSync(path.join(DATA_DIR, `${name}.json`), JSON.stringify(data, null, 2), 'utf-8');
  798. }
  799. function isSafeRemoteUrl(url) {
  800. try {
  801. const parsed = new URL(url);
  802. return ['http:', 'https:'].includes(parsed.protocol);
  803. } catch {
  804. return false;
  805. }
  806. }
  807. function sanitizeFilename(filename) {
  808. const safeName = String(filename || '')
  809. .replace(/[<>:"/\\|?*\x00-\x1F]/g, '_')
  810. .trim();
  811. return safeName || `video-${Date.now()}.mp4`;
  812. }
  813. function ensureVideoFilename(filename, sourceUrl = '') {
  814. const safeName = sanitizeFilename(filename);
  815. if (path.extname(safeName)) {
  816. return safeName;
  817. }
  818. try {
  819. const parsed = new URL(sourceUrl);
  820. const sourceExt = path.extname(parsed.pathname || '').toLowerCase();
  821. if (sourceExt) {
  822. return `${safeName}${sourceExt}`;
  823. }
  824. } catch {}
  825. return `${safeName}.mp4`;
  826. }
  827. function ensureUniqueVideoFilename(filename) {
  828. const ext = path.extname(filename) || '.mp4';
  829. const baseName = path.basename(filename, ext);
  830. let candidate = filename;
  831. let counter = 1;
  832. while (fs.existsSync(path.join(DATA_VIDEO_DIR, candidate)) || fs.existsSync(path.join(LEGACY_VIDEO_DIR, candidate))) {
  833. candidate = `${baseName}-${Date.now()}-${counter}${ext}`;
  834. counter += 1;
  835. }
  836. return candidate;
  837. }
  838. function createManagedVideoEntry({ filename, title, description, tags, thumbnail, duration, resolution, awemeId, authorName, size }) {
  839. const ext = path.extname(filename).replace('.', '').toLowerCase() || 'mp4';
  840. const now = new Date().toISOString();
  841. return {
  842. id: `VID-${Date.now()}`,
  843. title: title || path.basename(filename, path.extname(filename)),
  844. filename,
  845. size: size || 0,
  846. duration: Number(duration) || 0,
  847. created_at: now,
  848. modified_at: now,
  849. category: 'downloaded',
  850. tags: Array.isArray(tags) ? tags : [],
  851. description: description || '',
  852. thumbnail: thumbnail || '',
  853. source: 'downloaded',
  854. aweme_id: awemeId || '',
  855. metadata: {
  856. resolution: resolution || '未知',
  857. format: ext,
  858. authorName: authorName || ''
  859. }
  860. };
  861. }
  862. function normalizeRemoteUrls(primaryUrl, urls = []) {
  863. return [primaryUrl, ...(Array.isArray(urls) ? urls : [])].filter((url, index, list) => (
  864. typeof url === 'string'
  865. && isSafeRemoteUrl(url)
  866. && list.indexOf(url) === index
  867. ));
  868. }
  869. async function fetchRemoteVideoResponse(urls, requestHeaders = {}) {
  870. let lastError = null;
  871. for (const currentUrl of urls) {
  872. try {
  873. const response = await fetch(currentUrl, {
  874. method: 'GET',
  875. headers: requestHeaders,
  876. redirect: 'follow'
  877. });
  878. if (!response.ok) {
  879. const detail = await response.text().catch(() => '');
  880. lastError = new Error(`远程下载失败: ${response.status} ${response.statusText}${detail ? ` ${detail.slice(0, 200)}` : ''}`);
  881. continue;
  882. }
  883. if (!response.body) {
  884. lastError = new Error('远程响应缺少视频流');
  885. continue;
  886. }
  887. return { url: currentUrl, response };
  888. } catch (error) {
  889. lastError = error;
  890. }
  891. }
  892. throw lastError || new Error('没有可用的远程视频地址');
  893. }
  894. // ==================== Whisper 转录 ====================
  895. // POST /api/whisper/transcribe
  896. // body: { videoId, language?, model? }
  897. app.post('/api/whisper/transcribe', async (req, res) => {
  898. const { videoId, language = 'Chinese', model = 'base' } = req.body;
  899. if (!videoId) {
  900. return res.status(400).json({ error: '缺少 videoId 参数' });
  901. }
  902. // 从 manifest 查找视频
  903. const manifest = readManifest();
  904. const video = manifest.find(v => v.id === videoId);
  905. if (!video) {
  906. return res.status(404).json({ error: `未找到视频: ${videoId}` });
  907. }
  908. const videoPath = resolveVideoPath(video.filename);
  909. if (!fs.existsSync(videoPath)) {
  910. return res.status(404).json({ error: `视频文件不存在: ${video.filename}` });
  911. }
  912. // 为该视频创建专属输出目录
  913. const baseName = video.filename.replace(/\.[^.]+$/, '');
  914. const outputDir = path.join(WHISPER_DIR, baseName);
  915. if (!fs.existsSync(outputDir)) {
  916. fs.mkdirSync(outputDir, { recursive: true });
  917. }
  918. console.log(`🎙️ 开始 Whisper 转录: ${video.filename}`);
  919. console.log(` 模型: ${model}, 语言: ${language}`);
  920. console.log(` 输出目录: ${outputDir}`);
  921. // 执行 Whisper 命令
  922. const cmd = `whisper "${videoPath}" --model ${model} --language ${language} --output_dir "${outputDir}"`;
  923. try {
  924. const result = await new Promise((resolve, reject) => {
  925. const process = exec(cmd, {
  926. cwd: PROJECT_ROOT,
  927. timeout: 10 * 60 * 1000, // 10分钟超时
  928. maxBuffer: 10 * 1024 * 1024
  929. });
  930. let stdout = '';
  931. let stderr = '';
  932. process.stdout.on('data', (data) => {
  933. stdout += data;
  934. console.log(` [whisper] ${data.toString().trim()}`);
  935. });
  936. process.stderr.on('data', (data) => {
  937. stderr += data;
  938. });
  939. process.on('close', (code) => {
  940. if (code === 0) {
  941. resolve({ stdout, stderr });
  942. } else {
  943. reject(new Error(`Whisper 退出码: ${code}\n${stderr}`));
  944. }
  945. });
  946. process.on('error', (err) => {
  947. reject(new Error(`无法启动 Whisper: ${err.message}`));
  948. });
  949. });
  950. // 读取生成的文件
  951. const txtFile = path.join(outputDir, `${baseName}.txt`);
  952. const srtFile = path.join(outputDir, `${baseName}.srt`);
  953. const transcript = fs.existsSync(txtFile) ? fs.readFileSync(txtFile, 'utf-8') : '';
  954. const srt = fs.existsSync(srtFile) ? fs.readFileSync(srtFile, 'utf-8') : '';
  955. if (!transcript) {
  956. return res.status(500).json({ error: 'Whisper 执行完成但未生成文字稿' });
  957. }
  958. // 更新 manifest 中该视频的 whisper 字段
  959. const whisperPaths = {
  960. transcript: `Whisper/${baseName}/${baseName}.txt`,
  961. srt: `Whisper/${baseName}/${baseName}.srt`
  962. };
  963. // 检查是否存在其他输出文件
  964. const jsonFile = path.join(outputDir, `${baseName}.json`);
  965. const vttFile = path.join(outputDir, `${baseName}.vtt`);
  966. const tsvFile = path.join(outputDir, `${baseName}.tsv`);
  967. if (fs.existsSync(jsonFile)) whisperPaths.segments = `Whisper/${baseName}/${baseName}.json`;
  968. video.whisper = whisperPaths;
  969. writeManifest(manifest);
  970. console.log(`✅ Whisper 转录完成: ${baseName}`);
  971. res.json({
  972. success: true,
  973. videoId: video.id,
  974. transcript,
  975. srt,
  976. whisper: whisperPaths,
  977. outputDir: `Whisper/${baseName}`
  978. });
  979. } catch (err) {
  980. console.error(`❌ Whisper 转录失败:`, err.message);
  981. res.status(500).json({
  982. error: `Whisper 转录失败: ${err.message}`,
  983. hint: '请确保已安装 Whisper: pip install openai-whisper'
  984. });
  985. }
  986. });
  987. // GET /api/whisper/status — 检查 Whisper 是否可用
  988. app.get('/api/whisper/status', (req, res) => {
  989. exec('whisper --help', { timeout: 5000 }, (err) => {
  990. if (err) {
  991. res.json({ available: false, message: '未检测到 Whisper,请执行: pip install openai-whisper' });
  992. } else {
  993. res.json({ available: true, message: 'Whisper 已安装' });
  994. }
  995. });
  996. });
  997. // ==================== Manifest 管理 ====================
  998. // 已迁移至云函数 manifestManager(cloud-functions/01-manifestManager.js),由 cloud-api.interceptor 透明转发。
  999. // 以下 Express 路由保留为备份/降级参考,正常情况下不会被调用。
  1000. /* MIGRATED-TO-CLOUD: manifestManager — START
  1001. // GET /api/manifest — 获取完整 manifest
  1002. app.get('/api/manifest', (req, res) => {
  1003. try {
  1004. const manifest = readManifest();
  1005. res.json(manifest);
  1006. } catch (err) {
  1007. res.status(500).json({ error: `读取 manifest 失败: ${err.message}` });
  1008. }
  1009. });
  1010. // PUT /api/manifest/:videoId — 更新指定视频条目
  1011. app.put('/api/manifest/:videoId', (req, res) => {
  1012. try {
  1013. const manifest = readManifest();
  1014. const idx = manifest.findIndex(v => v.id === req.params.videoId);
  1015. if (idx === -1) {
  1016. return res.status(404).json({ error: `未找到视频: ${req.params.videoId}` });
  1017. }
  1018. // 合并更新字段
  1019. manifest[idx] = { ...manifest[idx], ...req.body };
  1020. writeManifest(manifest);
  1021. res.json({ success: true, video: manifest[idx] });
  1022. } catch (err) {
  1023. res.status(500).json({ error: `更新 manifest 失败: ${err.message}` });
  1024. }
  1025. });
  1026. // POST /api/manifest — 添加新视频条目
  1027. app.post('/api/manifest', (req, res) => {
  1028. try {
  1029. const manifest = readManifest();
  1030. const newEntry = req.body;
  1031. if (!newEntry.id || !newEntry.filename) {
  1032. return res.status(400).json({ error: '缺少 id 或 filename' });
  1033. }
  1034. // 检查重复
  1035. if (manifest.find(v => v.id === newEntry.id)) {
  1036. return res.status(409).json({ error: `视频 ${newEntry.id} 已存在` });
  1037. }
  1038. manifest.push(newEntry);
  1039. writeManifest(manifest);
  1040. res.json({ success: true, video: newEntry });
  1041. } catch (err) {
  1042. res.status(500).json({ error: `添加视频失败: ${err.message}` });
  1043. }
  1044. });
  1045. // DELETE /api/manifest/:videoId — 删除指定视频条目及关联文件
  1046. app.delete('/api/manifest/:videoId', (req, res) => {
  1047. try {
  1048. const manifest = readManifest();
  1049. const idx = manifest.findIndex(v => v.id === req.params.videoId);
  1050. if (idx === -1) {
  1051. return res.status(404).json({ error: `未找到视频: ${req.params.videoId}` });
  1052. }
  1053. const [video] = manifest.splice(idx, 1);
  1054. const filename = typeof video.filename === 'string' ? video.filename : '';
  1055. // 先清理关联文件,所有步骤都做容错;最后再持久化 manifest,避免中途异常导致状态不一致
  1056. if (filename) {
  1057. try {
  1058. const videoPath = resolveVideoPath(filename);
  1059. removeFileIfExists(videoPath);
  1060. } catch (cleanupErr) {
  1061. console.warn(`⚠️ 删除视频文件失败 (${filename}):`, cleanupErr.message);
  1062. }
  1063. } else {
  1064. console.warn(`⚠️ 视频条目 ${video.id} 缺少 filename 字段,跳过文件清理`);
  1065. }
  1066. try {
  1067. const remixFilePath = path.join(REMIXES_DIR, `${video.id}.json`);
  1068. removeFileIfExists(remixFilePath);
  1069. } catch (cleanupErr) {
  1070. console.warn(`⚠️ 删除 remix 文件失败 (${video.id}):`, cleanupErr.message);
  1071. }
  1072. if (filename) {
  1073. try {
  1074. const baseName = filename.replace(/\.[^.]+$/, '');
  1075. const whisperOutputDir = path.join(WHISPER_DIR, baseName);
  1076. removeDirectoryIfExists(whisperOutputDir);
  1077. } catch (cleanupErr) {
  1078. console.warn(`⚠️ 删除 Whisper 输出目录失败 (${filename}):`, cleanupErr.message);
  1079. }
  1080. }
  1081. writeManifest(manifest);
  1082. console.log(`🗑️ 视频已删除: ${video.title || filename || video.id}`);
  1083. res.json({ success: true, videoId: video.id, filename });
  1084. } catch (err) {
  1085. console.error('❌ DELETE /api/manifest 失败:', err);
  1086. res.status(500).json({ error: `删除视频失败: ${err.message}` });
  1087. }
  1088. });
  1089. MIGRATED-TO-CLOUD: manifestManager — END */
  1090. // ==================== 视频上传 ====================
  1091. // POST /api/upload/video — 上传视频文件
  1092. app.post('/api/upload/video', upload.single('video'), (req, res) => {
  1093. try {
  1094. if (!req.file) {
  1095. return res.status(400).json({ error: '未收到视频文件' });
  1096. }
  1097. const file = req.file;
  1098. const filename = file.filename;
  1099. const ext = path.extname(filename).replace('.', '').toLowerCase();
  1100. // 生成视频 ID
  1101. const videoId = `VID-${Date.now()}`;
  1102. // 获取文件大小
  1103. const fileStat = fs.statSync(file.path);
  1104. // 创建 manifest 条目
  1105. const videoEntry = {
  1106. id: videoId,
  1107. title: req.body.title || path.basename(filename, path.extname(filename)),
  1108. filename: filename,
  1109. size: fileStat.size,
  1110. duration: 0, // 前端可以通过 video 元素获取
  1111. category: 'uploaded',
  1112. tags: req.body.tags ? JSON.parse(req.body.tags) : [],
  1113. description: req.body.description || '用户上传的视频',
  1114. source: 'uploaded',
  1115. metadata: {
  1116. resolution: '未知',
  1117. format: ext || 'mp4'
  1118. }
  1119. };
  1120. // 添加到 manifest
  1121. const manifest = readManifest();
  1122. manifest.push(videoEntry);
  1123. writeManifest(manifest);
  1124. console.log(`📤 视频上传成功: ${filename} (${(fileStat.size / 1024 / 1024).toFixed(1)}MB) → ${videoId}`);
  1125. res.json({
  1126. success: true,
  1127. video: videoEntry,
  1128. filepath: `/backend/video/${filename}`
  1129. });
  1130. } catch (err) {
  1131. console.error('❌ 视频上传失败:', err.message);
  1132. res.status(500).json({ error: `上传失败: ${err.message}` });
  1133. }
  1134. });
  1135. // 上传错误处理
  1136. app.use((err, req, res, next) => {
  1137. if (err instanceof multer.MulterError) {
  1138. if (err.code === 'LIMIT_FILE_SIZE') {
  1139. return res.status(413).json({ error: '文件大小超过限制(最大 500MB)' });
  1140. }
  1141. return res.status(400).json({ error: `上传错误: ${err.message}` });
  1142. }
  1143. if (err) {
  1144. return res.status(400).json({ error: err.message });
  1145. }
  1146. next();
  1147. });
  1148. app.post('/api/remix/extract-audio', (req, res) => {
  1149. const { videoId } = req.body || {};
  1150. if (!videoId) {
  1151. return res.status(400).json({ error: '缺少 videoId 参数' });
  1152. }
  1153. const manifest = readManifest();
  1154. const video = manifest.find(v => v.id === videoId);
  1155. if (!video) {
  1156. return res.status(404).json({ error: `未找到视频: ${videoId}` });
  1157. }
  1158. const videoPath = resolveVideoPath(video.filename);
  1159. if (!fs.existsSync(videoPath)) {
  1160. return res.status(404).json({ error: `视频文件不存在: ${video.filename}` });
  1161. }
  1162. const safeBaseName = path.basename(video.filename, path.extname(video.filename)).replace(/[^a-zA-Z0-9._-]/g, '_') || `audio-${Date.now()}`;
  1163. const outputFilename = `${safeBaseName}-${Date.now()}.wav`;
  1164. const outputPath = path.join(DATA_REMIX_ASSET_DIR, outputFilename);
  1165. const ffmpegArgs = ['-y', '-i', videoPath, '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', outputPath];
  1166. const process = spawn('ffmpeg', ffmpegArgs, { cwd: PROJECT_ROOT });
  1167. let stderr = '';
  1168. process.stderr.on('data', (data) => {
  1169. stderr += data.toString();
  1170. });
  1171. process.on('error', (err) => {
  1172. removeFileIfExists(outputPath);
  1173. const message = /ENOENT/i.test(err.message)
  1174. ? '未检测到 ffmpeg,请先安装 ffmpeg 并确保已加入系统 PATH 环境变量'
  1175. : `无法启动 ffmpeg:${err.message}`;
  1176. res.status(500).json({ error: message });
  1177. });
  1178. process.on('close', (code) => {
  1179. if (code !== 0 || !fs.existsSync(outputPath)) {
  1180. removeFileIfExists(outputPath);
  1181. const lastErrorLine = stderr.split('\n').filter(Boolean).slice(-1)[0] || '';
  1182. const normalizedMessage = /ffmpeg/i.test(stderr) && /not recognized|not found|no such file/i.test(stderr)
  1183. ? '未检测到 ffmpeg,请先安装 ffmpeg 并确保已加入系统 PATH 环境变量'
  1184. : `音频提取失败${lastErrorLine ? `: ${lastErrorLine}` : ''}`;
  1185. return res.status(500).json({ error: normalizedMessage });
  1186. }
  1187. res.setHeader('Content-Type', 'audio/wav');
  1188. res.setHeader('Content-Disposition', `attachment; filename="${outputFilename}"`);
  1189. const stream = fs.createReadStream(outputPath);
  1190. stream.on('close', () => {
  1191. removeFileIfExists(outputPath);
  1192. });
  1193. stream.on('error', () => {
  1194. removeFileIfExists(outputPath);
  1195. if (!res.headersSent) {
  1196. res.status(500).json({ error: '音频文件读取失败' });
  1197. } else {
  1198. res.end();
  1199. }
  1200. });
  1201. stream.pipe(res);
  1202. });
  1203. });
  1204. // POST /api/extract-audio-mp3 — 提取视频音轨为 MP3(轻量,适合发给 Gemini 音频识别)
  1205. app.post('/api/extract-audio-mp3', (req, res) => {
  1206. const { videoId } = req.body || {};
  1207. if (!videoId) {
  1208. return res.status(400).json({ error: '缺少 videoId 参数' });
  1209. }
  1210. const manifest = readManifest();
  1211. const video = manifest.find(v => v.id === videoId);
  1212. if (!video) {
  1213. console.warn(`⚠️ extract-audio-mp3: 未找到 videoId=${videoId}`);
  1214. return res.status(404).json({ error: `未找到视频: ${videoId}` });
  1215. }
  1216. const videoPath = resolveVideoPath(video.filename);
  1217. if (!fs.existsSync(videoPath)) {
  1218. console.warn(`⚠️ extract-audio-mp3: 视频文件不存在: ${videoPath}`);
  1219. return res.status(404).json({ error: `视频文件不存在: ${video.filename}` });
  1220. }
  1221. const safeBaseName = path.basename(video.filename, path.extname(video.filename)).replace(/[^a-zA-Z0-9._-]/g, '_') || `audio-${Date.now()}`;
  1222. // 使用 AAC(ffmpeg 内置,无需 libmp3lame),封装为 m4a;Gemini 支持 audio/mp4
  1223. const outputFilename = `${safeBaseName}-${Date.now()}.m4a`;
  1224. const outputPath = path.join(DATA_REMIX_ASSET_DIR, outputFilename);
  1225. const outputMimeType = 'audio/mp4';
  1226. console.log(`🎵 开始音频提取: ${videoPath} → ${outputPath}`);
  1227. // 64kbps 单声道 16kHz:体积小且足够语音识别
  1228. const ffmpegArgs = ['-y', '-i', videoPath, '-vn', '-c:a', 'aac', '-b:a', '64k', '-ar', '16000', '-ac', '1', outputPath];
  1229. const proc = spawn('ffmpeg', ffmpegArgs, { cwd: PROJECT_ROOT });
  1230. let stderr = '';
  1231. let responded = false;
  1232. const safeRespond = (status, body) => {
  1233. if (responded) return;
  1234. responded = true;
  1235. res.status(status).json(body);
  1236. };
  1237. proc.stderr.on('data', (data) => { stderr += data.toString(); });
  1238. proc.on('error', (err) => {
  1239. removeFileIfExists(outputPath);
  1240. const message = /ENOENT/i.test(err.message)
  1241. ? '未检测到 ffmpeg,请先安装 ffmpeg 并确保已加入系统 PATH 环境变量'
  1242. : `无法启动 ffmpeg:${err.message}`;
  1243. console.error(`❌ extract-audio-mp3 spawn error: ${err.message}`);
  1244. safeRespond(500, { error: message });
  1245. });
  1246. proc.on('close', (code) => {
  1247. if (code !== 0 || !fs.existsSync(outputPath)) {
  1248. const tail = stderr.split('\n').filter(Boolean).slice(-5).join(' | ');
  1249. console.error(`❌ ffmpeg exit code=${code} 输出文件存在=${fs.existsSync(outputPath)}`);
  1250. console.error(` stderr 末尾: ${tail}`);
  1251. removeFileIfExists(outputPath);
  1252. const lastErrorLine = stderr.split('\n').filter(Boolean).slice(-1)[0] || '';
  1253. return safeRespond(500, { error: `音频提取失败 (exit=${code})${lastErrorLine ? `: ${lastErrorLine}` : ''}` });
  1254. }
  1255. const stats = fs.statSync(outputPath);
  1256. const sizeMB = (stats.size / 1024 / 1024).toFixed(2);
  1257. console.log(`🎵 音频提取完成: ${outputFilename} (${sizeMB}MB)`);
  1258. // 返回 base64 编码的 mp3(方便前端直接发给 Gemini)
  1259. const audioBuffer = fs.readFileSync(outputPath);
  1260. const audioBase64 = audioBuffer.toString('base64');
  1261. removeFileIfExists(outputPath);
  1262. safeRespond(200, {
  1263. success: true,
  1264. audio: {
  1265. base64: audioBase64,
  1266. mimeType: outputMimeType,
  1267. sizeMB: parseFloat(sizeMB),
  1268. filename: outputFilename
  1269. }
  1270. });
  1271. });
  1272. });
  1273. app.post('/api/remix/upload-asset', assetUpload.single('file'), async (req, res) => {
  1274. try {
  1275. if (!req.file) {
  1276. return res.status(400).json({ error: '未收到素材文件' });
  1277. }
  1278. const mimeType = req.file.mimetype || 'application/octet-stream';
  1279. const kind = mimeType.startsWith('audio/')
  1280. ? 'audio'
  1281. : mimeType.startsWith('video/')
  1282. ? 'video'
  1283. : 'image';
  1284. const key = buildDigitalHumanAssetKey(req.file.originalname, kind);
  1285. const token = buildQiniuUploadToken(key);
  1286. const formData = new FormData();
  1287. formData.append('token', token);
  1288. formData.append('key', key);
  1289. formData.append('file', new Blob([req.file.buffer], { type: mimeType }), path.basename(key));
  1290. const response = await fetch(QINIU_UPLOAD_URL, {
  1291. method: 'POST',
  1292. body: formData
  1293. });
  1294. const text = await response.text();
  1295. let payload = null;
  1296. try {
  1297. payload = text ? JSON.parse(text) : null;
  1298. } catch {
  1299. payload = null;
  1300. }
  1301. if (!response.ok) {
  1302. return res.status(response.status).json({
  1303. error: payload?.error || payload?.message || text || '七牛素材上传失败',
  1304. detail: payload || text || ''
  1305. });
  1306. }
  1307. const uploadedKey = payload?.key || key;
  1308. const url = `${QINIU_CDN_DOMAIN.replace(/\/$/, '')}/${uploadedKey}`;
  1309. if (!uploadedKey || !url) {
  1310. return res.status(500).json({ error: '七牛未返回素材 Key', detail: payload || text || '' });
  1311. }
  1312. res.json({
  1313. success: true,
  1314. url,
  1315. key: uploadedKey,
  1316. mimeType,
  1317. kind
  1318. });
  1319. } catch (error) {
  1320. console.error('❌ 上传重塑素材失败:', error);
  1321. res.status(500).json({ error: `上传重塑素材失败: ${error.message}` });
  1322. }
  1323. });
  1324. // ==================== 文件操作 ====================
  1325. // GET /api/files/whisper/:videoId — 获取指定视频的 Whisper 输出文件列表
  1326. app.get('/api/files/whisper/:videoId', (req, res) => {
  1327. try {
  1328. const manifest = readManifest();
  1329. const video = manifest.find(v => v.id === req.params.videoId);
  1330. if (!video) {
  1331. return res.status(404).json({ error: `未找到视频: ${req.params.videoId}` });
  1332. }
  1333. const baseName = video.filename.replace(/\.[^.]+$/, '');
  1334. const outputDir = path.join(WHISPER_DIR, baseName);
  1335. if (!fs.existsSync(outputDir)) {
  1336. return res.json({ files: [], exists: false });
  1337. }
  1338. const files = fs.readdirSync(outputDir).map(f => ({
  1339. name: f,
  1340. path: `Whisper/${baseName}/${f}`,
  1341. size: fs.statSync(path.join(outputDir, f)).size
  1342. }));
  1343. res.json({ files, exists: true });
  1344. } catch (err) {
  1345. res.status(500).json({ error: err.message });
  1346. }
  1347. });
  1348. // GET /api/files/read — 读取项目内文件内容
  1349. app.get('/api/files/read', (req, res) => {
  1350. const filePath = req.query.path;
  1351. if (!filePath) {
  1352. return res.status(400).json({ error: '缺少 path 参数' });
  1353. }
  1354. const fullPath = path.join(PROJECT_ROOT, filePath);
  1355. // 安全检查:不允许读取项目目录外的文件
  1356. if (!fullPath.startsWith(PROJECT_ROOT)) {
  1357. return res.status(403).json({ error: '路径不在项目目录内' });
  1358. }
  1359. if (!fs.existsSync(fullPath)) {
  1360. return res.status(404).json({ error: '文件不存在' });
  1361. }
  1362. const ext = path.extname(fullPath).toLowerCase();
  1363. if (['.json'].includes(ext)) {
  1364. res.json(JSON.parse(fs.readFileSync(fullPath, 'utf-8')));
  1365. } else {
  1366. res.type('text/plain').send(fs.readFileSync(fullPath, 'utf-8'));
  1367. }
  1368. });
  1369. app.post('/api/download/video', (req, res) => {
  1370. const {
  1371. url,
  1372. urls,
  1373. filename,
  1374. title,
  1375. description,
  1376. tags,
  1377. thumbnail,
  1378. duration,
  1379. resolution,
  1380. awemeId,
  1381. authorName
  1382. } = req.body || {};
  1383. if (!url || typeof url !== 'string') {
  1384. return res.status(400).json({ error: '缺少 url 参数' });
  1385. }
  1386. const candidateUrls = normalizeRemoteUrls(url, urls);
  1387. if (candidateUrls.length === 0) {
  1388. return res.status(400).json({ error: '无效的视频地址' });
  1389. }
  1390. const safeFilename = ensureUniqueVideoFilename(ensureVideoFilename(filename || title || awemeId || 'douyin-video.mp4', url));
  1391. const filePath = path.join(DATA_VIDEO_DIR, safeFilename);
  1392. const taskId = `DL-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`;
  1393. const task = {
  1394. id: taskId,
  1395. status: 'pending',
  1396. progress: 0,
  1397. downloadedBytes: 0,
  1398. totalBytes: 0,
  1399. filename: safeFilename,
  1400. created_at: new Date().toISOString()
  1401. };
  1402. downloadTasks.set(taskId, task);
  1403. res.json({ success: true, taskId, filename: safeFilename });
  1404. (async () => {
  1405. try {
  1406. task.status = 'downloading';
  1407. console.log(`📥 开始下载远程视频: ${safeFilename} ← ${candidateUrls[0]}`);
  1408. const { url: resolvedUrl, response } = await fetchRemoteVideoResponse(candidateUrls, {
  1409. 'Accept': '*/*',
  1410. 'User-Agent': 'Mozilla/5.0',
  1411. 'Referer': 'https://www.douyin.com/',
  1412. 'Origin': 'https://www.douyin.com'
  1413. });
  1414. task.sourceUrl = resolvedUrl;
  1415. const totalBytes = Number.parseInt(response.headers.get('content-length') || '0', 10) || 0;
  1416. task.totalBytes = totalBytes;
  1417. let downloadedBytes = 0;
  1418. let chunkCount = 0;
  1419. const progressStream = new Transform({
  1420. transform(chunk, encoding, callback) {
  1421. downloadedBytes += chunk.length;
  1422. chunkCount += 1;
  1423. task.downloadedBytes = downloadedBytes;
  1424. task.progress = totalBytes > 0
  1425. ? Math.min(99, Math.round((downloadedBytes / totalBytes) * 100))
  1426. : Math.min(95, Math.max(task.progress || 0, Math.min(95, chunkCount)));
  1427. callback(null, chunk);
  1428. }
  1429. });
  1430. await pipeline(
  1431. Readable.fromWeb(response.body),
  1432. progressStream,
  1433. fs.createWriteStream(filePath)
  1434. );
  1435. const stat = fs.statSync(filePath);
  1436. const manifest = readManifest();
  1437. const videoEntry = createManagedVideoEntry({
  1438. filename: safeFilename,
  1439. title,
  1440. description,
  1441. tags,
  1442. thumbnail,
  1443. duration,
  1444. resolution,
  1445. awemeId,
  1446. authorName,
  1447. size: stat.size
  1448. });
  1449. manifest.push(videoEntry);
  1450. writeManifest(manifest);
  1451. task.status = 'completed';
  1452. task.progress = 100;
  1453. task.completed_at = new Date().toISOString();
  1454. task.video = videoEntry;
  1455. console.log(`✅ 远程视频下载完成: ${safeFilename} (${(stat.size / 1024 / 1024).toFixed(1)}MB)`);
  1456. } catch (error) {
  1457. removeFileIfExists(filePath);
  1458. task.status = 'failed';
  1459. task.error = error.message;
  1460. task.failed_at = new Date().toISOString();
  1461. console.error('❌ 远程视频下载失败:', error);
  1462. }
  1463. })();
  1464. });
  1465. app.get('/api/download/video/:taskId', (req, res) => {
  1466. const task = downloadTasks.get(req.params.taskId);
  1467. if (!task) {
  1468. return res.status(404).json({ error: '未找到下载任务' });
  1469. }
  1470. res.json(task);
  1471. });
  1472. // ==================== 视频流代理 ====================
  1473. app.get('/api/video-proxy', async (req, res) => {
  1474. const { url, filename = 'douyin-video.mp4', download } = req.query;
  1475. if (!url || typeof url !== 'string') {
  1476. return res.status(400).json({ error: '缺少 url 参数' });
  1477. }
  1478. if (!isSafeRemoteUrl(url)) {
  1479. return res.status(400).json({ error: '无效的视频地址' });
  1480. }
  1481. try {
  1482. const upstreamHeaders = {
  1483. 'Accept': req.headers.accept || '*/*',
  1484. 'User-Agent': req.headers['user-agent'] || 'Mozilla/5.0',
  1485. 'Referer': 'https://www.douyin.com/',
  1486. 'Origin': 'https://www.douyin.com'
  1487. };
  1488. if (req.headers.range) {
  1489. upstreamHeaders.Range = req.headers.range;
  1490. }
  1491. const response = await fetch(url, {
  1492. method: 'GET',
  1493. headers: upstreamHeaders,
  1494. redirect: 'follow'
  1495. });
  1496. if (!response.ok && response.status !== 206) {
  1497. const errorText = await response.text().catch(() => '');
  1498. return res.status(response.status).json({
  1499. error: '远程视频请求失败',
  1500. status: response.status,
  1501. detail: errorText
  1502. });
  1503. }
  1504. res.status(response.status);
  1505. const passthroughHeaders = [
  1506. 'content-type',
  1507. 'content-length',
  1508. 'content-range',
  1509. 'accept-ranges',
  1510. 'etag',
  1511. 'last-modified',
  1512. 'cache-control'
  1513. ];
  1514. passthroughHeaders.forEach((headerName) => {
  1515. const value = response.headers.get(headerName);
  1516. if (value) {
  1517. res.setHeader(headerName, value);
  1518. }
  1519. });
  1520. if (!response.headers.get('content-type')) {
  1521. res.setHeader('Content-Type', 'video/mp4');
  1522. }
  1523. res.setHeader(
  1524. 'Content-Disposition',
  1525. download === '1'
  1526. ? `attachment; filename*=UTF-8''${encodeURIComponent(filename)}`
  1527. : `inline; filename*=UTF-8''${encodeURIComponent(filename)}`
  1528. );
  1529. if (!response.body) {
  1530. return res.end();
  1531. }
  1532. Readable.fromWeb(response.body).pipe(res);
  1533. } catch (error) {
  1534. console.error('视频代理失败:', error);
  1535. res.status(500).json({ error: `视频代理失败: ${error.message}` });
  1536. }
  1537. });
  1538. // ==================== AI 重塑记录 ====================
  1539. const REMIXES_DIR = path.join(DATA_DIR, 'remixes');
  1540. if (!fs.existsSync(REMIXES_DIR)) fs.mkdirSync(REMIXES_DIR, { recursive: true });
  1541. function readRemixes(videoId) {
  1542. const p = path.join(REMIXES_DIR, `${videoId}.json`);
  1543. if (!fs.existsSync(p)) return [];
  1544. return JSON.parse(fs.readFileSync(p, 'utf-8'));
  1545. }
  1546. function writeRemixes(videoId, data) {
  1547. fs.writeFileSync(path.join(REMIXES_DIR, `${videoId}.json`), JSON.stringify(data, null, 2), 'utf-8');
  1548. }
  1549. // 已迁移至云函数 remixManager(cloud-functions/05-remixManager.js)。
  1550. /* MIGRATED-TO-CLOUD: remixManager — START
  1551. // GET /api/remixes/:videoId — 获取某视频的所有重塑记录
  1552. app.get('/api/remixes/:videoId', (req, res) => {
  1553. res.json(readRemixes(req.params.videoId));
  1554. });
  1555. // POST /api/remixes/:videoId — 创建或更新一条重塑记录
  1556. // body: { remixId, ...remixData }
  1557. app.post('/api/remixes/:videoId', (req, res) => {
  1558. const remixes = readRemixes(req.params.videoId);
  1559. const { remixId } = req.body;
  1560. const idx = remixes.findIndex(r => r.remixId === remixId);
  1561. if (idx >= 0) {
  1562. // 更新已有记录(合并 segments)
  1563. remixes[idx] = { ...remixes[idx], ...req.body, updated_at: new Date().toISOString() };
  1564. } else {
  1565. // 新增记录
  1566. remixes.unshift({ ...req.body, created_at: new Date().toISOString(), updated_at: new Date().toISOString() });
  1567. }
  1568. writeRemixes(req.params.videoId, remixes);
  1569. res.json({ success: true });
  1570. });
  1571. // DELETE /api/remixes/:videoId/:remixId — 删除某条重塑记录
  1572. app.delete('/api/remixes/:videoId/:remixId', (req, res) => {
  1573. let remixes = readRemixes(req.params.videoId);
  1574. remixes = remixes.filter(r => r.remixId !== req.params.remixId);
  1575. writeRemixes(req.params.videoId, remixes);
  1576. res.json({ success: true });
  1577. });
  1578. // GET /api/remixes — 获取所有视频的重塑记录汇总
  1579. app.get('/api/remixes', (req, res) => {
  1580. const files = fs.readdirSync(REMIXES_DIR).filter(f => f.endsWith('.json'));
  1581. const all = {};
  1582. files.forEach(f => {
  1583. const videoId = f.replace('.json', '');
  1584. all[videoId] = JSON.parse(fs.readFileSync(path.join(REMIXES_DIR, f), 'utf-8'));
  1585. });
  1586. res.json(all);
  1587. });
  1588. MIGRATED-TO-CLOUD: remixManager — END */
  1589. // ==================== 一键成片(Quickly 代理) ====================
  1590. const QUICKLY_APP_KEY = 'ZmNmOGRhNjYzZTAx';
  1591. const QUICKLY_APP_SECRET = 'eaa12154c248cad9159a9d6ea8bedf46';
  1592. const QUICKLY_ACCOUNT_ID = '12859_117409';
  1593. const QUICKLY_CALLBACK_URL = 'https://server.fmode.cn/api/functions/cut/onemerge';
  1594. const QUICKLY_RELAY_URL = 'https://server.fmode.cn/api/functions';
  1595. // POST /api/quickly/create — 代理一键成片请求(服务端签名)
  1596. app.post('/api/quickly/create', async (req, res) => {
  1597. try {
  1598. const { videoUrls, options = {} } = req.body;
  1599. if (!videoUrls || !Array.isArray(videoUrls) || videoUrls.length === 0) {
  1600. return res.status(400).json({ error: '缺少 videoUrls 参数' });
  1601. }
  1602. const timestamp = Date.now().toString();
  1603. const signStr = timestamp + '#' + QUICKLY_APP_SECRET;
  1604. const sign = crypto.createHash('md5').update(signStr).digest('hex');
  1605. const materialList = videoUrls.map(url => ({ type: 'video', value: url }));
  1606. const preId = timestamp;
  1607. const apiBody = {
  1608. account_id: QUICKLY_ACCOUNT_ID,
  1609. callback_url: QUICKLY_CALLBACK_URL,
  1610. material_list: materialList,
  1611. tags: options.tags || '视频,AI生成',
  1612. proportion: options.proportion || '9:16',
  1613. video_duration: options.videoDuration || { min: 10, max: 30 },
  1614. pre_id: preId,
  1615. compose_number: 1,
  1616. ai_voice: options.aiVoice ?? 1,
  1617. ai_bgm: options.aiBgm ?? 1,
  1618. ai_flower: 1,
  1619. ai_subtitle: options.aiSubtitle ?? 0,
  1620. original_voice: 0
  1621. };
  1622. const relayData = JSON.stringify({
  1623. apiPath: '/v2/video/vlog/create',
  1624. apiBody: apiBody,
  1625. appKey: QUICKLY_APP_KEY,
  1626. timestamp: timestamp,
  1627. sign: sign
  1628. });
  1629. const body = JSON.stringify({ action: 'relay', relayData: relayData });
  1630. console.log(`📦 一键成片 - timestamp=${timestamp}, sign=${sign}`);
  1631. console.log(` 材料: ${videoUrls.length} 个视频`);
  1632. // 使用 Node 原生 fetch 发送请求
  1633. const response = await fetch(QUICKLY_RELAY_URL, {
  1634. method: 'POST',
  1635. headers: { 'Content-Type': 'application/json' },
  1636. body: body
  1637. });
  1638. const result = await response.json();
  1639. console.log('📦 一键成片 - 响应:', JSON.stringify(result).substring(0, 300));
  1640. res.json(result);
  1641. } catch (err) {
  1642. console.error('❌ 一键成片代理失败:', err.message);
  1643. res.status(500).json({ error: err.message });
  1644. }
  1645. });
  1646. // POST /api/quickly/query — 代理查询一键成片结果(通过 Parse 云函数)
  1647. app.post('/api/quickly/query', async (req, res) => {
  1648. try {
  1649. const { taskId } = req.body;
  1650. if (!taskId) return res.status(400).json({ error: '缺少 taskId 参数' });
  1651. const body = JSON.stringify({
  1652. id: 'sWvRr8RvPT',
  1653. _ApplicationId: 'ncloudmaster',
  1654. action: 'query',
  1655. taskId: taskId
  1656. });
  1657. const QUICKLY_QUERY_URL = 'https://server.fmode.cn/api/functions';
  1658. const response = await fetch(QUICKLY_QUERY_URL, {
  1659. method: 'POST',
  1660. headers: { 'Content-Type': 'application/json' },
  1661. body: body
  1662. });
  1663. const result = await response.json();
  1664. res.json(result);
  1665. } catch (err) {
  1666. console.error('❌ 一键成片查询失败:', err.message);
  1667. res.status(500).json({ error: err.message });
  1668. }
  1669. });
  1670. // ==================== 视频合成(图片+音频 → 视频)====================
  1671. const COMPOSITE_DIR = path.join(DATA_DIR, 'composite');
  1672. if (!fs.existsSync(COMPOSITE_DIR)) fs.mkdirSync(COMPOSITE_DIR, { recursive: true });
  1673. // 下载远程文件到本地
  1674. async function downloadFile(url, destPath) {
  1675. const response = await fetch(url, { redirect: 'follow' });
  1676. if (!response.ok) throw new Error(`下载失败 (${response.status}): ${url}`);
  1677. const buffer = Buffer.from(await response.arrayBuffer());
  1678. fs.writeFileSync(destPath, buffer);
  1679. return destPath;
  1680. }
  1681. // 获取媒体时长(秒)。ffprobe format=duration 同时适用于音频与视频。
  1682. function getMediaDuration(mediaPath) {
  1683. return new Promise((resolve, reject) => {
  1684. const proc = spawn('ffprobe', [
  1685. '-v', 'error', '-show_entries', 'format=duration',
  1686. '-of', 'default=noprint_wrappers=1:nokey=1', mediaPath
  1687. ]);
  1688. let stdout = '';
  1689. proc.stdout.on('data', d => stdout += d.toString());
  1690. proc.on('error', reject);
  1691. proc.on('close', code => {
  1692. const dur = parseFloat(stdout.trim());
  1693. if (code !== 0 || isNaN(dur)) reject(new Error('无法获取媒体时长'));
  1694. else resolve(dur);
  1695. });
  1696. });
  1697. }
  1698. // 向后兼容别名
  1699. const getAudioDuration = getMediaDuration;
  1700. // 将图片转为 jpg(确保 ffmpeg 兼容性)
  1701. function convertImageToJpg(inputPath, outputPath) {
  1702. return new Promise((resolve, reject) => {
  1703. const proc = spawn('ffmpeg', ['-y', '-i', inputPath, '-frames:v', '1', outputPath]);
  1704. let stderr = '';
  1705. proc.stderr.on('data', d => stderr += d.toString());
  1706. proc.on('error', reject);
  1707. proc.on('close', code => {
  1708. if (code !== 0) reject(new Error(`图片转换失败: ${stderr.split('\n').filter(Boolean).slice(-1)[0]}`));
  1709. else resolve(outputPath);
  1710. });
  1711. });
  1712. }
  1713. // ====================================================================
  1714. // 拼接管线统一规格(所有段必须一致,否则 concat -c copy 会失败):
  1715. // 视频:1920x1080 yuv420p h264_mf
  1716. // 音频:AAC 192k 44100Hz stereo
  1717. // ====================================================================
  1718. const SEG_VF = 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black';
  1719. const SEG_AUDIO_ARGS = ['-c:a', 'aac', '-b:a', '192k', '-ar', '44100', '-ac', '2'];
  1720. // 单个片段:图片 + 音频 → 视频(case A,原有逻辑)
  1721. function createSegmentVideo(imagePath, audioPath, outputPath, duration) {
  1722. return new Promise(async (resolve, reject) => {
  1723. try {
  1724. // 先将图片统一转为 jpg 确保兼容性
  1725. const jpgPath = imagePath.replace(/\.[^.]+$/, '') + '_converted.jpg';
  1726. await convertImageToJpg(imagePath, jpgPath);
  1727. const args = [
  1728. '-y',
  1729. '-loop', '1', '-i', jpgPath,
  1730. '-i', audioPath,
  1731. '-c:v', 'h264_mf',
  1732. ...SEG_AUDIO_ARGS,
  1733. '-vf', SEG_VF,
  1734. '-pix_fmt', 'yuv420p',
  1735. '-t', String(duration),
  1736. '-shortest',
  1737. outputPath
  1738. ];
  1739. const proc = spawn('ffmpeg', args);
  1740. let stderr = '';
  1741. proc.stderr.on('data', d => stderr += d.toString());
  1742. proc.on('error', err => reject(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg' : err.message)));
  1743. proc.on('close', code => {
  1744. removeFileIfExists(jpgPath);
  1745. if (code !== 0) reject(new Error(`片段合成失败: ${stderr.split('\n').filter(Boolean).slice(-2).join(' ')}`));
  1746. else resolve(outputPath);
  1747. });
  1748. } catch (err) {
  1749. reject(err);
  1750. }
  1751. });
  1752. }
  1753. /**
  1754. * Case B: 视频 + 音频 → 替换音轨,**按音频时长对齐**(不是 -shortest)。
  1755. *
  1756. * 对齐策略(音频是叙事主干,宁可冻结画面也不能丢解说):
  1757. * - videoDuration >= audioDuration + 0.05 → `-t audioDuration` 截断视频
  1758. * - videoDuration < audioDuration - 0.05 → `tpad=stop_mode=clone` 用末帧补齐到 audioDuration
  1759. * - 长度接近 → 直接 `-t audioDuration`
  1760. *
  1761. * 输出统一规格(1920x1080 yuv420p)以便后续 concat -c copy。
  1762. *
  1763. * @param {number} audioDuration 音频时长(秒),必填
  1764. * @param {number=} videoDuration 视频原时长(秒),可选;缺省时退化为旧的 -shortest 行为
  1765. */
  1766. function createVideoAudioSegment(videoPath, audioPath, outputPath, audioDuration, videoDuration) {
  1767. return new Promise((resolve, reject) => {
  1768. const target = Number(audioDuration);
  1769. if (!target || target <= 0) {
  1770. reject(new Error('createVideoAudioSegment 需要正数 audioDuration'));
  1771. return;
  1772. }
  1773. // 构建 -vf 滤镜链。若视频短于音频,append tpad 冻结末帧补齐。
  1774. let vfChain = SEG_VF;
  1775. let needPad = false;
  1776. if (typeof videoDuration === 'number' && videoDuration > 0 &&
  1777. videoDuration < target - 0.05) {
  1778. const padSec = (target - videoDuration).toFixed(3);
  1779. vfChain += `,tpad=stop_mode=clone:stop_duration=${padSec}`;
  1780. needPad = true;
  1781. }
  1782. const args = [
  1783. '-y',
  1784. '-i', videoPath,
  1785. '-i', audioPath,
  1786. '-map', '0:v:0',
  1787. '-map', '1:a:0',
  1788. '-c:v', 'h264_mf',
  1789. '-vf', vfChain,
  1790. '-pix_fmt', 'yuv420p',
  1791. ...SEG_AUDIO_ARGS,
  1792. '-t', String(target),
  1793. outputPath,
  1794. ];
  1795. console.log(` ↳ case B align: audio=${target.toFixed(2)}s, video=${videoDuration ? videoDuration.toFixed(2) + 's' : '?'}, ${needPad ? 'PAD freeze last frame' : 'TRIM/EXACT'}`);
  1796. const proc = spawn('ffmpeg', args);
  1797. let stderr = '';
  1798. proc.stderr.on('data', d => stderr += d.toString());
  1799. proc.on('error', err => reject(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg' : err.message)));
  1800. proc.on('close', code => {
  1801. if (code !== 0) reject(new Error(`视频+音频片段合成失败: ${stderr.split('\n').filter(Boolean).slice(-2).join(' ')}`));
  1802. else resolve(outputPath);
  1803. });
  1804. });
  1805. }
  1806. /**
  1807. * Case C: 纯视频段 → 加静音轨并重编码到统一规格。
  1808. * 用于「无解说配音」场景。视频原音轨被丢弃。
  1809. */
  1810. function createVideoOnlySegment(videoPath, outputPath) {
  1811. return new Promise((resolve, reject) => {
  1812. const args = [
  1813. '-y',
  1814. '-i', videoPath,
  1815. '-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
  1816. '-map', '0:v:0',
  1817. '-map', '1:a:0',
  1818. '-c:v', 'h264_mf',
  1819. '-vf', SEG_VF,
  1820. '-pix_fmt', 'yuv420p',
  1821. ...SEG_AUDIO_ARGS,
  1822. '-shortest',
  1823. outputPath,
  1824. ];
  1825. const proc = spawn('ffmpeg', args);
  1826. let stderr = '';
  1827. proc.stderr.on('data', d => stderr += d.toString());
  1828. proc.on('error', err => reject(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg' : err.message)));
  1829. proc.on('close', code => {
  1830. if (code !== 0) reject(new Error(`纯视频片段合成失败: ${stderr.split('\n').filter(Boolean).slice(-2).join(' ')}`));
  1831. else resolve(outputPath);
  1832. });
  1833. });
  1834. }
  1835. /**
  1836. * Case D: 图片 + 静音 → 静态画面段(指定时长)。
  1837. * 用于「图片 + 无配音」组合,duration 由调用方决定(默认 3s)。
  1838. */
  1839. function createImageOnlySegment(imagePath, outputPath, duration) {
  1840. return new Promise(async (resolve, reject) => {
  1841. try {
  1842. const jpgPath = imagePath.replace(/\.[^.]+$/, '') + '_converted.jpg';
  1843. await convertImageToJpg(imagePath, jpgPath);
  1844. const args = [
  1845. '-y',
  1846. '-loop', '1', '-i', jpgPath,
  1847. '-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100',
  1848. '-map', '0:v:0',
  1849. '-map', '1:a:0',
  1850. '-c:v', 'h264_mf',
  1851. '-vf', SEG_VF,
  1852. '-pix_fmt', 'yuv420p',
  1853. ...SEG_AUDIO_ARGS,
  1854. '-t', String(duration),
  1855. '-shortest',
  1856. outputPath,
  1857. ];
  1858. const proc = spawn('ffmpeg', args);
  1859. let stderr = '';
  1860. proc.stderr.on('data', d => stderr += d.toString());
  1861. proc.on('error', err => reject(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg' : err.message)));
  1862. proc.on('close', code => {
  1863. removeFileIfExists(jpgPath);
  1864. if (code !== 0) reject(new Error(`静音图片片段合成失败: ${stderr.split('\n').filter(Boolean).slice(-2).join(' ')}`));
  1865. else resolve(outputPath);
  1866. });
  1867. } catch (err) {
  1868. reject(err);
  1869. }
  1870. });
  1871. }
  1872. /**
  1873. * 段调度:根据 segment 字段路由到 4 种 case 之一。
  1874. *
  1875. * 输入字段:
  1876. * - imageUrl?: 图片 URL
  1877. * - videoUrl?: 视频 URL(如即梦图生视频结果;优先级高于 imageUrl)
  1878. * - audioUrl?: 音频 URL(可选,缺省视为静音段)
  1879. * - duration?: 静态图模式下的画面停留秒数(默认 3)
  1880. *
  1881. * 路由:
  1882. * case A: imageUrl + audioUrl → createSegmentVideo(按音频时长)
  1883. * case B: videoUrl + audioUrl → createVideoAudioSegment(按短的)
  1884. * case C: videoUrl 无 audioUrl → createVideoOnlySegment(按视频原长)
  1885. * case D: imageUrl 无 audioUrl → createImageOnlySegment(按 duration)
  1886. *
  1887. * @returns {Promise<{ path: string, duration: number|null, mode: 'A'|'B'|'C'|'D' }>}
  1888. */
  1889. async function composeOneSegment({ seg, jobDir, i }) {
  1890. const segVideoPath = path.join(jobDir, `seg-${i}.mp4`);
  1891. const hasImage = !!seg.imageUrl;
  1892. const hasVideo = !!seg.videoUrl;
  1893. const hasAudio = !!seg.audioUrl;
  1894. if (!hasImage && !hasVideo) {
  1895. throw new Error(`片段 ${seg.id != null ? seg.id : i} 缺少 imageUrl/videoUrl`);
  1896. }
  1897. // 下载音频(若有)
  1898. let audioPath = null;
  1899. let audioDuration = null;
  1900. if (hasAudio) {
  1901. const ext = (seg.audioUrl.match(/\.(mp3|wav|aac|ogg|m4a)/i) || ['.mp3'])[0] || '.mp3';
  1902. audioPath = path.join(jobDir, `audio-${i}${ext}`);
  1903. await downloadFile(seg.audioUrl, audioPath);
  1904. audioDuration = await getAudioDuration(audioPath);
  1905. }
  1906. if (hasVideo) {
  1907. const videoPath = path.join(jobDir, `video-${i}.mp4`);
  1908. await downloadFile(seg.videoUrl, videoPath);
  1909. // 探测原视频时长用于音频对齐(trim 或 freeze-last-frame pad);
  1910. // 探测失败 → 退化为旧的 -shortest 语义,仍能产出可用片段。
  1911. let videoDuration;
  1912. try {
  1913. videoDuration = await getMediaDuration(videoPath);
  1914. } catch (e) {
  1915. console.warn(` ⚠️ 片段 ${i + 1} 探测视频时长失败,退化为 -shortest 对齐:`, e?.message || e);
  1916. }
  1917. if (hasAudio) {
  1918. await createVideoAudioSegment(videoPath, audioPath, segVideoPath, audioDuration, videoDuration);
  1919. return { path: segVideoPath, duration: audioDuration, mode: 'B' };
  1920. } else {
  1921. await createVideoOnlySegment(videoPath, segVideoPath);
  1922. return { path: segVideoPath, duration: null, mode: 'C' };
  1923. }
  1924. }
  1925. // hasImage
  1926. const ext = (seg.imageUrl.match(/\.(jpg|jpeg|png|webp|gif)/i) || ['.jpg'])[0] || '.jpg';
  1927. const imgPath = path.join(jobDir, `img-${i}${ext}`);
  1928. await downloadFile(seg.imageUrl, imgPath);
  1929. if (hasAudio) {
  1930. await createSegmentVideo(imgPath, audioPath, segVideoPath, audioDuration);
  1931. return { path: segVideoPath, duration: audioDuration, mode: 'A' };
  1932. } else {
  1933. const dur = Math.max(0.5, Number(seg.duration) || 3);
  1934. await createImageOnlySegment(imgPath, segVideoPath, dur);
  1935. return { path: segVideoPath, duration: dur, mode: 'D' };
  1936. }
  1937. }
  1938. // 拼接多个视频片段
  1939. function concatVideos(segmentPaths, outputPath) {
  1940. return new Promise((resolve, reject) => {
  1941. // 创建 concat 文件列表
  1942. const listPath = outputPath + '.txt';
  1943. const listContent = segmentPaths.map(p => `file '${p.replace(/\\/g, '/')}'`).join('\n');
  1944. fs.writeFileSync(listPath, listContent, 'utf-8');
  1945. const args = [
  1946. '-y', '-f', 'concat', '-safe', '0',
  1947. '-i', listPath,
  1948. '-c', 'copy',
  1949. outputPath
  1950. ];
  1951. const proc = spawn('ffmpeg', args);
  1952. let stderr = '';
  1953. proc.stderr.on('data', d => stderr += d.toString());
  1954. proc.on('error', err => reject(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg' : err.message)));
  1955. proc.on('close', code => {
  1956. removeFileIfExists(listPath);
  1957. if (code !== 0) reject(new Error(`视频拼接失败: ${stderr.split('\n').filter(Boolean).slice(-2).join(' ')}`));
  1958. else resolve(outputPath);
  1959. });
  1960. });
  1961. }
  1962. // POST /api/video/composite — 合成视频(图片+音频 → 最终视频)
  1963. app.post('/api/video/composite', async (req, res) => {
  1964. const { segments, title } = req.body;
  1965. // segments: [{ imageUrl, audioUrl, id }]
  1966. if (!Array.isArray(segments) || segments.length === 0) {
  1967. return res.status(400).json({ error: '缺少 segments 参数' });
  1968. }
  1969. const jobId = `VG-${Date.now()}`;
  1970. const jobDir = path.join(COMPOSITE_DIR, jobId);
  1971. fs.mkdirSync(jobDir, { recursive: true });
  1972. try {
  1973. console.log(`🎬 开始合成视频: ${jobId}, ${segments.length} 个片段`);
  1974. // 1. 下载并合成每段(四态分发:image+audio / video+audio / video-only / image-only)
  1975. const segmentPaths = [];
  1976. for (let i = 0; i < segments.length; i++) {
  1977. const seg = segments[i];
  1978. if (!seg.imageUrl && !seg.videoUrl) {
  1979. console.warn(`⚠️ 片段 ${seg.id || i} 缺少 imageUrl/videoUrl,跳过`);
  1980. continue;
  1981. }
  1982. console.log(` 📥 处理片段 ${i + 1}/${segments.length}...`);
  1983. const { path: segPath, duration, mode } = await composeOneSegment({ seg, jobDir, i });
  1984. console.log(` 🎞️ 已完成片段 ${i + 1}/${segments.length} [mode=${mode}${duration ? `, ${duration.toFixed(1)}s` : ''}]`);
  1985. segmentPaths.push(segPath);
  1986. }
  1987. if (segmentPaths.length === 0) {
  1988. return res.status(400).json({ error: '没有有效的素材片段' });
  1989. }
  1990. // 2. 拼接所有片段
  1991. const safeTitle = String(title || 'video').replace(/[^a-zA-Z0-9\u4e00-\u9fff_-]/g, '_').substring(0, 50);
  1992. const finalFilename = `${safeTitle}-${jobId}.mp4`;
  1993. const finalPath = path.join(COMPOSITE_DIR, finalFilename);
  1994. console.log(` 🔗 拼接 ${segmentPaths.length} 个片段...`);
  1995. if (segmentPaths.length === 1) {
  1996. // 只有一个片段,直接复制
  1997. fs.copyFileSync(segmentPaths[0], finalPath);
  1998. } else {
  1999. await concatVideos(segmentPaths, finalPath);
  2000. }
  2001. // 3. 清理临时文件
  2002. try {
  2003. fs.rmSync(jobDir, { recursive: true, force: true });
  2004. } catch {}
  2005. const fileSize = fs.statSync(finalPath).size;
  2006. console.log(`✅ 视频合成完成: ${finalFilename} (${(fileSize / 1024 / 1024).toFixed(1)}MB)`);
  2007. res.json({
  2008. success: true,
  2009. videoUrl: `/api/video/composite/${finalFilename}`,
  2010. filename: finalFilename,
  2011. size: fileSize,
  2012. segments: segmentPaths.length
  2013. });
  2014. } catch (err) {
  2015. console.error(`❌ 视频合成失败:`, err.message);
  2016. // 清理
  2017. try { fs.rmSync(jobDir, { recursive: true, force: true }); } catch {}
  2018. res.status(500).json({ error: `视频合成失败: ${err.message}` });
  2019. }
  2020. });
  2021. /**
  2022. * POST /api/video/composite/stream — [已废弃] SSE 流式合成端点
  2023. *
  2024. * 2026-05-22 起,「主题生视频」「素材合成视频」改为浏览器端 ffmpeg.wasm 合成。
  2025. * 旧路由已删除;仅保留同步版 /api/video/composite 给 app.ts 的旧 vg 模块使用。
  2026. */
  2027. app.post('/api/video/composite/stream', async (req, res) => {
  2028. return res.status(410).json({
  2029. error: 'deprecated: client now composes in browser via ffmpeg.wasm; see BrowserFfmpegService.composeVideo()',
  2030. });
  2031. });
  2032. // GET /api/video/composite/:filename — 下载合成视频
  2033. app.get('/api/video/composite/:filename', (req, res) => {
  2034. const filePath = path.join(COMPOSITE_DIR, req.params.filename);
  2035. if (!fs.existsSync(filePath)) {
  2036. return res.status(404).json({ error: '视频文件不存在' });
  2037. }
  2038. res.setHeader('Content-Type', 'video/mp4');
  2039. fs.createReadStream(filePath).pipe(res);
  2040. });
  2041. // ==================== 声音训练辅助:自动选择未训练的 speaker_id ====================
  2042. // 已被认为"已训练 / 已占用"的状态枚举(trainStatus 文档中的 State 值)
  2043. const VOICE_TRAINED_STATES = new Set(['training', 'success', 'active', 'expired', 'reclaimed']);
  2044. const DEFAULT_VOICE_TOKEN = process.env.VOLC_TTS_TOKEN || 'Bearer r:f0333969e312a40e4703e8fe4ed1c600';
  2045. // 从 trainStatus 响应中尽力解析出已被训练 / 占用的 speaker_id 集合
  2046. function collectTrainedSpeakerIdsFromResponse(payload) {
  2047. const found = new Set();
  2048. const seen = new WeakSet();
  2049. const visit = (node) => {
  2050. if (!node || typeof node !== 'object') return;
  2051. if (seen.has(node)) return;
  2052. seen.add(node);
  2053. if (Array.isArray(node)) { node.forEach(visit); return; }
  2054. const speakerId = node.speaker_id || node.SpeakerID || node.speakerId || node.Speaker_id || node.speakerID;
  2055. const state = node.State || node.state || node.status || node.Status;
  2056. if (typeof speakerId === 'string' && /^S_[A-Za-z0-9]+$/.test(speakerId.trim())) {
  2057. const stateLower = String(state || '').trim().toLowerCase();
  2058. if (stateLower && VOICE_TRAINED_STATES.has(stateLower)) {
  2059. found.add(speakerId.trim());
  2060. }
  2061. }
  2062. Object.values(node).forEach(visit);
  2063. };
  2064. visit(payload);
  2065. return found;
  2066. }
  2067. // 已迁移至云函数 voiceManager(cloud-functions/06-voiceManager.js)。
  2068. /* MIGRATED-TO-CLOUD: voiceManager — START
  2069. // POST /api/voice/auto-speaker-id — 自动挑选一个未训练 / 未占用的 speaker_id
  2070. app.post('/api/voice/auto-speaker-id', async (req, res) => {
  2071. try {
  2072. const pool = readVoiceSpeakerIdOptions();
  2073. if (pool.length === 0) {
  2074. return res.status(500).json({ error: '本地未配置 speaker_id 池(docs/音色创建/speaker_id.md 为空)' });
  2075. }
  2076. const profiles = readDataFile('voice-profiles');
  2077. const usedLocally = new Set(
  2078. profiles
  2079. .map((p) => String(p?.speaker_id || '').trim())
  2080. .filter((id) => /^S_[A-Za-z0-9]+$/.test(id))
  2081. );
  2082. const token = normalizeBearerToken(req.body?.token) || DEFAULT_VOICE_TOKEN;
  2083. let usedRemotely = new Set();
  2084. let remoteOk = false;
  2085. let remoteError = '';
  2086. try {
  2087. const trainStatusPayload = await requestVolcTtsJson('trainStatus', {
  2088. token,
  2089. speakerIdList: pool
  2090. });
  2091. usedRemotely = collectTrainedSpeakerIdsFromResponse(trainStatusPayload);
  2092. remoteOk = true;
  2093. } catch (e) {
  2094. const detail = e?.detail;
  2095. const detailText = typeof detail === 'string'
  2096. ? detail
  2097. : (detail ? JSON.stringify(detail) : '');
  2098. remoteError = e?.message || detailText || (typeof e === 'string' ? e : (e ? JSON.stringify(e) : 'trainStatus 调用失败'));
  2099. }
  2100. const used = new Set([...usedLocally, ...usedRemotely]);
  2101. const available = pool.find((id) => !used.has(id));
  2102. if (!available) {
  2103. return res.status(409).json({
  2104. error: '当前所有候选 speaker_id 均已被训练或占用,请补充新的 speaker_id',
  2105. pool,
  2106. used: [...used],
  2107. used_local: [...usedLocally],
  2108. used_remote: [...usedRemotely],
  2109. remote_check: remoteOk,
  2110. remote_error: remoteError || undefined
  2111. });
  2112. }
  2113. res.json({
  2114. speaker_id: available,
  2115. pool,
  2116. used_local: [...usedLocally],
  2117. used_remote: [...usedRemotely],
  2118. remote_check: remoteOk,
  2119. remote_error: remoteError || undefined
  2120. });
  2121. } catch (err) {
  2122. res.status(500).json({ error: err?.message || '检测可用 speaker_id 失败' });
  2123. }
  2124. });
  2125. // POST /api/voice-profiles/sync — 前端复刻成功后回写本地占用记录
  2126. // 临时方案:远程 trainStatus 不稳定,使用 data/voice-profiles.json 作为 speaker_id 占用源
  2127. app.post('/api/voice-profiles/sync', (req, res) => {
  2128. try {
  2129. const body = req.body || {};
  2130. const speakerId = String(body.speaker_id || '').trim();
  2131. const timbreId = String(body.timbre_id || body.id || '').trim();
  2132. if (!/^S_[A-Za-z0-9]+$/.test(speakerId)) {
  2133. return res.status(400).json({ error: '缺少有效的 speaker_id' });
  2134. }
  2135. if (!timbreId) {
  2136. return res.status(400).json({ error: '缺少 timbre_id / id' });
  2137. }
  2138. const nowIso = new Date().toISOString();
  2139. const profile = {
  2140. id: timbreId,
  2141. name: String(body.name || '').trim() || '未命名音色',
  2142. creation_mode: 'clone',
  2143. speaker_id: speakerId,
  2144. timbre_id: timbreId,
  2145. demo_audio: String(body.demo_audio || '').trim(),
  2146. latest_audio_url: String(body.latest_audio_url || body.demo_audio || '').trim(),
  2147. x_api_resource_id: String(body.x_api_resource_id || '').trim(),
  2148. icl_speaker_id: String(body.icl_speaker_id || '').trim(),
  2149. model_version: String(body.model_version || '').trim(),
  2150. status: body.status === undefined || body.status === null ? null : body.status,
  2151. status_label: String(body.status_label || '').trim(),
  2152. message: String(body.message || '').trim(),
  2153. created_at: body.created_at || nowIso,
  2154. updated_at: nowIso
  2155. };
  2156. const saved = upsertVoiceProfile(profile);
  2157. res.json({ success: true, profile: saved });
  2158. } catch (err) {
  2159. res.status(500).json({ error: err?.message || '保存音色记录失败' });
  2160. }
  2161. });
  2162. MIGRATED-TO-CLOUD: voiceManager — END */
  2163. // ==================== 任务管理 ====================
  2164. // 已迁移至云函数 taskManager(cloud-functions/02-taskManager.js)。
  2165. /* MIGRATED-TO-CLOUD: taskManager — START
  2166. // GET /api/tasks — 获取所有任务
  2167. app.get('/api/tasks', (req, res) => {
  2168. res.json(readDataFile('tasks'));
  2169. });
  2170. // POST /api/tasks — 创建新任务
  2171. app.post('/api/tasks', (req, res) => {
  2172. const tasks = readDataFile('tasks');
  2173. const task = { ...req.body, created_at: new Date().toISOString(), updated_at: new Date().toISOString() };
  2174. // 确保始终有有效 ID(即使前端传了空 ID)
  2175. if (!task.id) task.id = `TASK-${Date.now()}`;
  2176. tasks.unshift(task);
  2177. writeDataFile('tasks', tasks);
  2178. res.json({ success: true, task });
  2179. });
  2180. // PUT /api/tasks/:id — 更新任务
  2181. app.put('/api/tasks/:id', (req, res) => {
  2182. const tasks = readDataFile('tasks');
  2183. const idx = tasks.findIndex(t => t.id === req.params.id);
  2184. if (idx === -1) return res.status(404).json({ error: `任务不存在: ${req.params.id}` });
  2185. tasks[idx] = { ...tasks[idx], ...req.body, updated_at: new Date().toISOString() };
  2186. writeDataFile('tasks', tasks);
  2187. res.json({ success: true, task: tasks[idx] });
  2188. });
  2189. // DELETE /api/tasks/:id — 删除任务
  2190. app.delete('/api/tasks/:id', (req, res) => {
  2191. let tasks = readDataFile('tasks');
  2192. const len = tasks.length;
  2193. tasks = tasks.filter(t => t.id !== req.params.id);
  2194. if (tasks.length === len) return res.status(404).json({ error: `任务不存在: ${req.params.id}` });
  2195. writeDataFile('tasks', tasks);
  2196. res.json({ success: true });
  2197. });
  2198. MIGRATED-TO-CLOUD: taskManager — END */
  2199. // ==================== 生成历史 ====================
  2200. // 已迁移至云函数 historyManager(cloud-functions/03-historyManager.js)。
  2201. /* MIGRATED-TO-CLOUD: historyManager — START
  2202. // GET /api/history — 获取历史记录
  2203. app.get('/api/history', (req, res) => {
  2204. res.json(readDataFile('history'));
  2205. });
  2206. // POST /api/history — 添加历史记录
  2207. app.post('/api/history', (req, res) => {
  2208. const history = readDataFile('history');
  2209. const record = { id: `HIS-${Date.now()}`, created_at: new Date().toISOString(), ...req.body };
  2210. history.unshift(record);
  2211. writeDataFile('history', history);
  2212. res.json({ success: true, record });
  2213. });
  2214. // DELETE /api/history/:id — 删除历史记录
  2215. app.delete('/api/history/:id', (req, res) => {
  2216. let history = readDataFile('history');
  2217. const len = history.length;
  2218. history = history.filter(h => h.id !== req.params.id);
  2219. if (history.length === len) return res.status(404).json({ error: `记录不存在: ${req.params.id}` });
  2220. writeDataFile('history', history);
  2221. res.json({ success: true });
  2222. });
  2223. // DELETE /api/history — 清空所有历史
  2224. app.delete('/api/history', (req, res) => {
  2225. writeDataFile('history', []);
  2226. res.json({ success: true });
  2227. });
  2228. MIGRATED-TO-CLOUD: historyManager — END */
  2229. // ==================== 生成结果 ====================
  2230. // 已迁移至云函数 resultManager(cloud-functions/04-resultManager.js)。
  2231. /* MIGRATED-TO-CLOUD: resultManager — START
  2232. // GET /api/results — 获取所有结果
  2233. app.get('/api/results', (req, res) => {
  2234. res.json(readDataFile('results'));
  2235. });
  2236. // POST /api/results — 添加结果
  2237. app.post('/api/results', (req, res) => {
  2238. const results = readDataFile('results');
  2239. const result = { id: `RES-${Date.now()}`, created_at: new Date().toISOString(), ...req.body };
  2240. results.unshift(result);
  2241. writeDataFile('results', results);
  2242. res.json({ success: true, result });
  2243. });
  2244. // PUT /api/results/:id — 更新结果
  2245. app.put('/api/results/:id', (req, res) => {
  2246. const results = readDataFile('results');
  2247. const idx = results.findIndex(r => r.id === req.params.id);
  2248. if (idx === -1) return res.status(404).json({ error: `结果不存在: ${req.params.id}` });
  2249. results[idx] = { ...results[idx], ...req.body };
  2250. writeDataFile('results', results);
  2251. res.json({ success: true, result: results[idx] });
  2252. });
  2253. // DELETE /api/results/:id — 删除结果
  2254. app.delete('/api/results/:id', (req, res) => {
  2255. let results = readDataFile('results');
  2256. const len = results.length;
  2257. results = results.filter(r => r.id !== req.params.id);
  2258. if (results.length === len) return res.status(404).json({ error: `结果不存在: ${req.params.id}` });
  2259. writeDataFile('results', results);
  2260. res.json({ success: true });
  2261. });
  2262. MIGRATED-TO-CLOUD: resultManager — END */
  2263. // ==================== LLM 大模型代理 ====================
  2264. const LLM_BASE_URL = 'http://server.fmode.cn:9999';
  2265. const LLM_API_KEY = 'sk-MFBOnsAtZiqlwwMgMLKCFmPy55pMohQEGMqsIw3aJrIgvoEO';
  2266. // 非流式调用已迁移至云函数 proxyHub(cloud-functions/08-proxyHub.js)。
  2267. // 但流式 SSE 调用必须走本地 Express(云函数不支持流式输出),由前端 fetch 直连。
  2268. // 入口:POST /api/llm/chat/stream
  2269. app.post('/api/llm/chat/stream', async (req, res) => {
  2270. try {
  2271. const { model, messages, temperature, max_tokens, ...rest } = req.body || {};
  2272. if (!Array.isArray(messages) || messages.length === 0) {
  2273. return res.status(400).json({ error: '缺少 messages 参数' });
  2274. }
  2275. const payload = {
  2276. model: model || 'gpt-4o',
  2277. messages,
  2278. temperature: temperature ?? 0.7,
  2279. max_tokens: max_tokens || 4096,
  2280. ...rest,
  2281. stream: true, // 强制流式
  2282. };
  2283. const url = `${LLM_BASE_URL}/v1/chat/completions`;
  2284. console.log(`🤖 [Stream] LLM Chat 请求: model=${payload.model}, messages=${messages.length}条`);
  2285. const upstream = await fetch(url, {
  2286. method: 'POST',
  2287. headers: {
  2288. 'Content-Type': 'application/json',
  2289. 'Authorization': `Bearer ${LLM_API_KEY}`,
  2290. },
  2291. body: JSON.stringify(payload),
  2292. });
  2293. if (!upstream.ok) {
  2294. const errText = await upstream.text();
  2295. console.error('🤖 [Stream] 上游错误:', upstream.status, errText);
  2296. return res.status(upstream.status).json({ error: errText || `upstream ${upstream.status}` });
  2297. }
  2298. // 直接把上游 SSE chunk 透传给客户端
  2299. res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
  2300. res.setHeader('Cache-Control', 'no-cache, no-transform');
  2301. res.setHeader('Connection', 'keep-alive');
  2302. res.setHeader('X-Accel-Buffering', 'no'); // nginx 不缓冲
  2303. res.flushHeaders?.();
  2304. // node-fetch 18+ 的 body 是 web ReadableStream;用 reader 读
  2305. const reader = upstream.body.getReader();
  2306. const onAbort = () => { try { reader.cancel(); } catch {} };
  2307. req.on('close', onAbort);
  2308. try {
  2309. while (true) {
  2310. const { value, done } = await reader.read();
  2311. if (done) break;
  2312. if (value) res.write(Buffer.from(value));
  2313. }
  2314. } finally {
  2315. req.off('close', onAbort);
  2316. res.end();
  2317. }
  2318. } catch (err) {
  2319. console.error('🤖 [Stream] 异常:', err.message);
  2320. if (!res.headersSent) {
  2321. res.status(500).json({ error: `LLM 流式请求失败: ${err.message}` });
  2322. } else {
  2323. try { res.end(); } catch {}
  2324. }
  2325. }
  2326. });
  2327. /* MIGRATED-TO-CLOUD: proxyHub — START
  2328. // POST /api/llm/chat — OpenAI ChatCompletions 代理
  2329. app.post('/api/llm/chat', async (req, res) => {
  2330. try {
  2331. const { model, messages, temperature, max_tokens, stream, ...rest } = req.body;
  2332. if (!messages || !Array.isArray(messages)) {
  2333. return res.status(400).json({ error: '缺少 messages 参数' });
  2334. }
  2335. const payload = {
  2336. model: model || 'gpt-4o-mini',
  2337. messages,
  2338. temperature: temperature ?? 0.7,
  2339. max_tokens: max_tokens || 4096,
  2340. stream: stream || false,
  2341. ...rest
  2342. };
  2343. const url = `${LLM_BASE_URL}/v1/chat/completions`;
  2344. console.log(`🤖 LLM Chat 请求: model=${payload.model}, messages=${messages.length}条`);
  2345. if (payload.stream) {
  2346. // 流式响应
  2347. const response = await fetch(url, {
  2348. method: 'POST',
  2349. headers: {
  2350. 'Content-Type': 'application/json',
  2351. 'Authorization': `Bearer ${LLM_API_KEY}`
  2352. },
  2353. body: JSON.stringify(payload)
  2354. });
  2355. if (!response.ok) {
  2356. const errText = await response.text();
  2357. console.error('🤖 LLM Stream 错误:', response.status, errText);
  2358. return res.status(response.status).json({ error: errText });
  2359. }
  2360. res.setHeader('Content-Type', 'text/event-stream');
  2361. res.setHeader('Cache-Control', 'no-cache');
  2362. res.setHeader('Connection', 'keep-alive');
  2363. const reader = response.body;
  2364. reader.on('data', (chunk) => res.write(chunk));
  2365. reader.on('end', () => res.end());
  2366. reader.on('error', (err) => {
  2367. console.error('🤖 LLM Stream 读取错误:', err.message);
  2368. res.end();
  2369. });
  2370. } else {
  2371. // 非流式响应
  2372. const response = await fetch(url, {
  2373. method: 'POST',
  2374. headers: {
  2375. 'Content-Type': 'application/json',
  2376. 'Authorization': `Bearer ${LLM_API_KEY}`
  2377. },
  2378. body: JSON.stringify(payload)
  2379. });
  2380. const data = await response.json();
  2381. if (!response.ok) {
  2382. console.error('🤖 LLM Chat 错误:', response.status, data);
  2383. return res.status(response.status).json(data);
  2384. }
  2385. console.log(`🤖 LLM Chat 完成: tokens=${data?.usage?.total_tokens || '?'}`);
  2386. res.json(data);
  2387. }
  2388. } catch (err) {
  2389. console.error('🤖 LLM Chat 异常:', err.message);
  2390. res.status(500).json({ error: `LLM 请求失败: ${err.message}` });
  2391. }
  2392. });
  2393. // POST /api/llm/gemini — Gemini 原生格式代理(支持媒体识别)
  2394. app.post('/api/llm/gemini', async (req, res) => {
  2395. try {
  2396. const { model, contents, generationConfig, safetySettings, systemInstruction } = req.body;
  2397. if (!contents) {
  2398. return res.status(400).json({ error: '缺少 contents 参数' });
  2399. }
  2400. const geminiModel = model || 'gemini-2.5-flash';
  2401. const url = `${LLM_BASE_URL}/v1beta/models/${geminiModel}:generateContent`;
  2402. const payload = { contents };
  2403. if (generationConfig) payload.generationConfig = generationConfig;
  2404. if (safetySettings) payload.safetySettings = safetySettings;
  2405. if (systemInstruction) payload.systemInstruction = systemInstruction;
  2406. console.log(`🤖 Gemini 请求: model=${geminiModel}, parts=${contents?.[0]?.parts?.length || 0}`);
  2407. const response = await fetch(url, {
  2408. method: 'POST',
  2409. headers: {
  2410. 'Content-Type': 'application/json',
  2411. 'Authorization': `Bearer ${LLM_API_KEY}`
  2412. },
  2413. body: JSON.stringify(payload)
  2414. });
  2415. const data = await response.json();
  2416. if (!response.ok) {
  2417. console.error('🤖 Gemini 错误:', response.status, data);
  2418. return res.status(response.status).json(data);
  2419. }
  2420. console.log(`🤖 Gemini 完成: tokens=${data?.usageMetadata?.totalTokenCount || '?'}`);
  2421. res.json(data);
  2422. } catch (err) {
  2423. console.error('🤖 Gemini 异常:', err.message);
  2424. res.status(500).json({ error: `Gemini 请求失败: ${err.message}` });
  2425. }
  2426. });
  2427. MIGRATED-TO-CLOUD: proxyHub — END */
  2428. // ==================== Douyin transcript worker ====================
  2429. function cleanText(value) {
  2430. return String(value || '').trim();
  2431. }
  2432. function getVocToken() {
  2433. return cleanText(
  2434. process.env.DOUYIN_API_TOKEN
  2435. || process.env.VOC_TOKEN
  2436. || process.env.TRANSCRIPTION_VOC_TOKEN
  2437. || process.env.VOICE_TOKEN
  2438. || process.env.OPENCLAW_VOC_TOKEN
  2439. || process.env.VOC_SOCIAL_TOKEN
  2440. );
  2441. }
  2442. function bearerAuth(token) {
  2443. const value = cleanText(token);
  2444. return /^Bearer\s+/i.test(value) ? value : `Bearer ${value}`;
  2445. }
  2446. function stripEmpty(value) {
  2447. if (!value || typeof value !== 'object') return value;
  2448. const out = Array.isArray(value) ? [] : {};
  2449. for (const [key, val] of Object.entries(value)) {
  2450. if (val === undefined || val === null || val === '') continue;
  2451. if (val && typeof val === 'object' && !Array.isArray(val)) {
  2452. const nested = stripEmpty(val);
  2453. if (Object.keys(nested).length) out[key] = nested;
  2454. } else {
  2455. out[key] = val;
  2456. }
  2457. }
  2458. return out;
  2459. }
  2460. function readGatewayError(data, fallback) {
  2461. return data?.error?.message || data?.error || data?.mess || data?.message || data?.msg || data?.detail || fallback || '';
  2462. }
  2463. function sleep(ms) {
  2464. return new Promise(resolve => setTimeout(resolve, ms));
  2465. }
  2466. function formatNetworkError(error) {
  2467. const cause = error?.cause?.code || error?.cause?.message || '';
  2468. return `${error?.message || 'fetch failed'}${cause ? `;cause=${cause}` : ''}`;
  2469. }
  2470. async function fetchDouyinGatewayWithRetry(url, init, routePath) {
  2471. let lastError = null;
  2472. for (let attempt = 1; attempt <= DOUYIN_GATEWAY_MAX_ATTEMPTS; attempt += 1) {
  2473. try {
  2474. return await fetch(url, init);
  2475. } catch (error) {
  2476. lastError = error;
  2477. const reason = formatNetworkError(error);
  2478. console.warn(`[douyin-gateway] ${routePath} attempt ${attempt}/${DOUYIN_GATEWAY_MAX_ATTEMPTS} failed: ${reason}`);
  2479. if (attempt < DOUYIN_GATEWAY_MAX_ATTEMPTS) {
  2480. await sleep(Math.min(300 * attempt, 1200));
  2481. }
  2482. }
  2483. }
  2484. throw lastError || new Error('fetch failed');
  2485. }
  2486. async function requestDouyinGateway(routeName, params = {}, payload = {}) {
  2487. const route = DOUYIN_API_ROUTES[routeName];
  2488. if (!route) {
  2489. throw Object.assign(new Error('不支持的抖音接口'), { statusCode: 400 });
  2490. }
  2491. const token = getVocToken();
  2492. if (!token) {
  2493. throw Object.assign(new Error('本地抖音数据网关未配置 DOUYIN_API_TOKEN、VOC_TOKEN 或 VOC_SOCIAL_TOKEN。'), { statusCode: 400 });
  2494. }
  2495. const url = new URL(`${DOUYIN_API_BASE_URL}${route.path}`);
  2496. const init = {
  2497. method: route.method,
  2498. headers: {
  2499. 'Content-Type': 'application/json',
  2500. Accept: 'application/json',
  2501. Authorization: bearerAuth(token),
  2502. },
  2503. };
  2504. if (route.method === 'GET') {
  2505. const query = { ...(payload || {}), ...(params || {}) };
  2506. for (const [key, value] of Object.entries(query)) {
  2507. if (value !== undefined && value !== null && value !== '') {
  2508. url.searchParams.set(key, String(value));
  2509. }
  2510. }
  2511. } else {
  2512. init.body = JSON.stringify(stripEmpty(payload || {}));
  2513. }
  2514. let response;
  2515. try {
  2516. response = await fetchDouyinGatewayWithRetry(url.toString(), init, route.path);
  2517. } catch (error) {
  2518. throw Object.assign(new Error(`本地抖音数据网关网络请求失败:${formatNetworkError(error)};attempts=${DOUYIN_GATEWAY_MAX_ATTEMPTS};base=${DOUYIN_API_BASE_URL} route=${route.path}`), {
  2519. statusCode: 502,
  2520. });
  2521. }
  2522. const text = await response.text();
  2523. let data = null;
  2524. try { data = text ? JSON.parse(text) : null; } catch {}
  2525. if (!response.ok || data?.success === false) {
  2526. throw Object.assign(new Error(readGatewayError(data, text) || `抖音数据接口请求失败 HTTP ${response.status}`), {
  2527. statusCode: response.status || 500,
  2528. });
  2529. }
  2530. return data || { code: 500, success: false, error: '服务返回异常' };
  2531. }
  2532. app.post('/api/douyin/call', async (req, res) => {
  2533. try {
  2534. const { route, params = {}, payload = {}, optional = false } = req.body || {};
  2535. const data = await requestDouyinGateway(route, params, payload);
  2536. res.json({ success: true, data });
  2537. } catch (error) {
  2538. if (req.body?.optional) {
  2539. return res.json({
  2540. success: false,
  2541. optional: true,
  2542. error: error.message || '本地抖音数据网关调用失败',
  2543. });
  2544. }
  2545. res.status(error.statusCode || 500).json({
  2546. success: false,
  2547. error: error.message || '本地抖音数据网关调用失败',
  2548. });
  2549. }
  2550. });
  2551. app.get('/api/douyin/diagnose', async (req, res) => {
  2552. const routeName = cleanText(req.query.route || 'videoDetail');
  2553. const route = DOUYIN_API_ROUTES[routeName] || DOUYIN_API_ROUTES.videoDetail;
  2554. const result = {
  2555. success: true,
  2556. data: {
  2557. baseUrl: DOUYIN_API_BASE_URL,
  2558. routeName,
  2559. routePath: route.path,
  2560. tokenConfigured: !!getVocToken(),
  2561. probe: null,
  2562. },
  2563. };
  2564. if (String(req.query.probe || '') === '1') {
  2565. try {
  2566. result.data.probe = await requestDouyinGateway(routeName, { aweme_id: cleanText(req.query.awemeId) || '7592116912205630761' });
  2567. } catch (error) {
  2568. result.data.probe = { success: false, error: error.message || '探测失败' };
  2569. }
  2570. }
  2571. res.json(result);
  2572. });
  2573. function createTranscriptJob(input) {
  2574. const now = new Date().toISOString();
  2575. return {
  2576. id: `transcript_${Date.now()}_${crypto.randomBytes(4).toString('hex')}`,
  2577. awemeId: cleanText(input.awemeId),
  2578. analysisId: cleanText(input.analysisId),
  2579. provider: cleanText(input.provider) || 'iflytek-gateway',
  2580. status: 'queued',
  2581. stageLabel: '已创建转写任务',
  2582. progress: 0,
  2583. warnings: [],
  2584. createdAt: now,
  2585. updatedAt: now
  2586. };
  2587. }
  2588. function updateTranscriptJob(job, patch) {
  2589. Object.assign(job, patch, { updatedAt: new Date().toISOString() });
  2590. transcriptTasks.set(job.id, job);
  2591. return job;
  2592. }
  2593. function findAwemeDetail(node, depth = 0) {
  2594. if (!node || depth > 8) return null;
  2595. if (Array.isArray(node)) {
  2596. for (const item of node) {
  2597. const found = findAwemeDetail(item, depth + 1);
  2598. if (found) return found;
  2599. }
  2600. return null;
  2601. }
  2602. if (typeof node !== 'object') return null;
  2603. if (node.aweme_id && node.video) return node;
  2604. if (node.aweme_detail) return findAwemeDetail(node.aweme_detail, depth + 1) || node.aweme_detail;
  2605. if (node.aweme_info) return findAwemeDetail(node.aweme_info, depth + 1) || node.aweme_info;
  2606. for (const value of Object.values(node)) {
  2607. const found = findAwemeDetail(value, depth + 1);
  2608. if (found) return found;
  2609. }
  2610. return null;
  2611. }
  2612. function extractAwemeId(value) {
  2613. const text = cleanText(value);
  2614. const patterns = [/aweme_id=(\d+)/, /modal_id=(\d+)/, /douyin\.com\/video\/(\d+)/, /douyin\.com\/share\/video\/(\d+)/, /\b(\d{15,25})\b/];
  2615. for (const pattern of patterns) {
  2616. const match = text.match(pattern);
  2617. if (match?.[1]) return match[1];
  2618. }
  2619. return /^\d{15,25}$/.test(text) ? text : '';
  2620. }
  2621. function decodeMaybeBase64Url(value) {
  2622. const text = cleanText(value);
  2623. if (/^https?:\/\//i.test(text)) return text;
  2624. if (!/^[A-Za-z0-9+/=_-]{20,}$/.test(text)) return '';
  2625. try {
  2626. const decoded = Buffer.from(text, 'base64').toString('utf8');
  2627. return /^https?:\/\//i.test(decoded) ? decoded : '';
  2628. } catch {
  2629. return '';
  2630. }
  2631. }
  2632. function isLikelyMediaUrl(url) {
  2633. return /^https?:\/\//i.test(url) && !/\.(?:jpg|jpeg|png|webp|gif)(?:\?|$)/i.test(url);
  2634. }
  2635. function inferMediaKind(pathParts, url) {
  2636. const joined = pathParts.join('.').toLowerCase();
  2637. if (/audio|mp4a|music|sound/.test(joined) || /audio|mp4a/i.test(url)) return 'audio';
  2638. if (/play_addr|download_addr|bit_rate|video|media/.test(joined) || isLikelyMediaUrl(url)) return 'video';
  2639. return 'unknown';
  2640. }
  2641. function collectMediaCandidates(node, pathParts = [], out = []) {
  2642. if (!node) return out;
  2643. if (Array.isArray(node)) {
  2644. node.forEach((item, index) => collectMediaCandidates(item, [...pathParts, String(index)], out));
  2645. return out;
  2646. }
  2647. if (typeof node !== 'object') return out;
  2648. for (const [key, value] of Object.entries(node)) {
  2649. const nextPath = [...pathParts, key];
  2650. if (key === 'url_list' && Array.isArray(value)) {
  2651. value.forEach((item, index) => {
  2652. const url = decodeMaybeBase64Url(item);
  2653. const kind = inferMediaKind(nextPath, url);
  2654. if (url && ['audio', 'video'].includes(kind)) {
  2655. out.push({
  2656. url,
  2657. kind,
  2658. keyPath: nextPath.join('.'),
  2659. index,
  2660. dataSize: Number(node.data_size || node.size || 0),
  2661. bitRate: Number(node.bit_rate || node.bitrate || node.real_bitrate || node.avg_bitrate || 0)
  2662. });
  2663. }
  2664. });
  2665. } else if (['main_url', 'backup_url', 'backup_url_1', 'url'].includes(key) && typeof value === 'string') {
  2666. const url = decodeMaybeBase64Url(value);
  2667. const kind = inferMediaKind(nextPath, url);
  2668. if (url && ['audio', 'video'].includes(kind)) {
  2669. out.push({
  2670. url,
  2671. kind,
  2672. keyPath: nextPath.join('.'),
  2673. dataSize: Number(node.data_size || node.size || 0),
  2674. bitRate: Number(node.bit_rate || node.bitrate || node.real_bitrate || node.avg_bitrate || 0)
  2675. });
  2676. }
  2677. }
  2678. collectMediaCandidates(value, nextPath, out);
  2679. }
  2680. return out;
  2681. }
  2682. function selectMediaCandidate(detail) {
  2683. const seen = new Set();
  2684. const candidates = collectMediaCandidates(detail)
  2685. .filter(item => isLikelyMediaUrl(item.url))
  2686. .filter(item => {
  2687. if (seen.has(item.url)) return false;
  2688. seen.add(item.url);
  2689. return true;
  2690. });
  2691. candidates.sort((a, b) => {
  2692. const aPreferred = a.kind === 'audio' ? 0 : 1;
  2693. const bPreferred = b.kind === 'audio' ? 0 : 1;
  2694. if (aPreferred !== bPreferred) return aPreferred - bPreferred;
  2695. const rank = item => {
  2696. const keyPath = String(item.keyPath || '').toLowerCase();
  2697. if (item.kind === 'audio' && keyPath.includes('video.dynamic_audio_list')) return 0;
  2698. if (item.kind === 'audio' && keyPath.includes('video.bit_rate_audio')) return 1;
  2699. if (item.kind === 'audio' && keyPath.includes('music.')) return 3;
  2700. return 2;
  2701. };
  2702. const aRank = rank(a);
  2703. const bRank = rank(b);
  2704. if (aRank !== bRank) return aRank - bRank;
  2705. return (a.dataSize || Number.MAX_SAFE_INTEGER) - (b.dataSize || Number.MAX_SAFE_INTEGER);
  2706. });
  2707. return candidates[0] || null;
  2708. }
  2709. function durationFromDetail(detail) {
  2710. const raw = detail?.video?.duration || detail?.duration || detail?.video_duration || detail?.durationMs;
  2711. const numeric = Number(raw || 0);
  2712. if (!Number.isFinite(numeric) || numeric <= 0) return 0;
  2713. return numeric > 10000 ? Math.round(numeric) : Math.round(numeric * 1000);
  2714. }
  2715. function extensionFromUrl(url, fallback) {
  2716. try {
  2717. const parsed = new URL(url);
  2718. const ext = path.extname(parsed.pathname).toLowerCase();
  2719. if (/^\.(mp4|m4a|mp3|wav|aac|mov|webm)$/i.test(ext)) return ext;
  2720. } catch {}
  2721. return fallback;
  2722. }
  2723. async function fetchDouyinDetailForTranscript(input) {
  2724. const supplied = findAwemeDetail(input.detail);
  2725. if (supplied?.aweme_id && supplied.video) return supplied;
  2726. const token = getVocToken();
  2727. if (!token) {
  2728. throw Object.assign(new Error('未配置 DOUYIN_API_TOKEN、VOC_TOKEN 或 VOC_SOCIAL_TOKEN,无法获取抖音视频详情。'), {
  2729. status: 'needs_provider_config'
  2730. });
  2731. }
  2732. const awemeId = cleanText(input.awemeId) || extractAwemeId(input.sourceUrl || input.url);
  2733. if (!awemeId) {
  2734. throw Object.assign(new Error('缺少 awemeId,无法获取视频详情。'), { status: 'needs_media' });
  2735. }
  2736. const url = new URL(`${DOUYIN_API_BASE_URL}/douyin/app/v3/fetch_one_video_v3`);
  2737. url.searchParams.set('aweme_id', awemeId);
  2738. const response = await fetch(url.toString(), {
  2739. method: 'GET',
  2740. headers: {
  2741. Accept: 'application/json',
  2742. Authorization: bearerAuth(token)
  2743. }
  2744. });
  2745. const data = await response.json().catch(() => ({}));
  2746. if (!response.ok || data.success === false) {
  2747. throw new Error(data.error?.message || data.error || data.message || data.mess || `抖音详情获取失败 HTTP ${response.status}`);
  2748. }
  2749. const detail = findAwemeDetail(data);
  2750. if (!detail?.aweme_id) {
  2751. throw Object.assign(new Error('抖音详情响应中未找到 aweme_detail。'), { status: 'needs_media' });
  2752. }
  2753. return detail;
  2754. }
  2755. async function downloadTranscriptMedia(candidate, job) {
  2756. fs.mkdirSync(TRANSCRIPT_TEMP_DIR, { recursive: true });
  2757. const ext = extensionFromUrl(candidate.url, candidate.kind === 'audio' ? '.m4a' : '.mp4');
  2758. const filePath = path.join(TRANSCRIPT_TEMP_DIR, `${job.id}-source${ext}`);
  2759. const { response } = await fetchRemoteVideoResponse([candidate.url], {
  2760. Accept: '*/*',
  2761. 'User-Agent': 'Mozilla/5.0',
  2762. Referer: 'https://www.douyin.com/',
  2763. Origin: 'https://www.douyin.com'
  2764. });
  2765. await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(filePath));
  2766. return filePath;
  2767. }
  2768. function extractAudioForTranscript(inputPath, job) {
  2769. return new Promise((resolve, reject) => {
  2770. fs.mkdirSync(TRANSCRIPT_TEMP_DIR, { recursive: true });
  2771. const outputPath = path.join(TRANSCRIPT_TEMP_DIR, `${job.id}.m4a`);
  2772. const args = ['-y', '-i', inputPath, '-vn', '-c:a', 'aac', '-b:a', '64k', '-ar', '16000', '-ac', '1', outputPath];
  2773. const proc = spawn('ffmpeg', args, { cwd: PROJECT_ROOT });
  2774. let stderr = '';
  2775. proc.stderr.on('data', data => { stderr += data.toString(); });
  2776. proc.on('error', err => {
  2777. reject(Object.assign(new Error(/ENOENT/i.test(err.message) ? '未检测到 ffmpeg,请先安装 ffmpeg 并加入 PATH。' : err.message), {
  2778. status: 'needs_media_processing'
  2779. }));
  2780. });
  2781. proc.on('close', code => {
  2782. if (code !== 0 || !fs.existsSync(outputPath)) {
  2783. const lastLine = stderr.split('\n').filter(Boolean).slice(-1)[0] || '';
  2784. reject(Object.assign(new Error(`音频提取失败${lastLine ? `:${lastLine}` : ''}`), {
  2785. status: 'needs_media_processing'
  2786. }));
  2787. return;
  2788. }
  2789. resolve(outputPath);
  2790. });
  2791. });
  2792. }
  2793. async function uploadTranscriptGateway(filePath, durationMs) {
  2794. const token = getVocToken();
  2795. if (!token) {
  2796. throw Object.assign(new Error('未配置 VOC_TOKEN、TRANSCRIPTION_VOC_TOKEN 或 VOICE_TOKEN,无法调用转写网关。'), {
  2797. status: 'needs_provider_config'
  2798. });
  2799. }
  2800. if (!durationMs) {
  2801. throw Object.assign(new Error('缺少音频时长 durationMs,无法提交转写网关。'), { status: 'needs_media' });
  2802. }
  2803. const buffer = fs.readFileSync(filePath);
  2804. const form = new FormData();
  2805. form.append('audio', new Blob([buffer], { type: 'audio/mp4' }), path.basename(filePath));
  2806. form.append('durationMs', String(durationMs));
  2807. form.append('roleType', '1');
  2808. form.append('roleNum', '0');
  2809. const response = await fetch(`${TRANSCRIPTION_GATEWAY_BASE_URL}/upload`, {
  2810. method: 'POST',
  2811. headers: {
  2812. Authorization: bearerAuth(token),
  2813. Accept: 'application/json'
  2814. },
  2815. body: form
  2816. });
  2817. const data = await response.json().catch(() => ({}));
  2818. if (!response.ok || data.success === false) {
  2819. throw new Error(data.error?.message || data.error || data.message || `转写上传失败 HTTP ${response.status}`);
  2820. }
  2821. const orderId = data.orderId || data.content?.orderId || data.data?.orderId || data.result?.orderId;
  2822. if (!orderId) throw new Error('转写网关未返回 orderId。');
  2823. return {
  2824. orderId,
  2825. estimateTime: Number(data.estimateTime || data.content?.estimateTime || data.data?.estimateTime || 0)
  2826. };
  2827. }
  2828. async function queryTranscriptGateway(orderId) {
  2829. const token = getVocToken();
  2830. if (!token) {
  2831. throw Object.assign(new Error('未配置 VOC_TOKEN、TRANSCRIPTION_VOC_TOKEN 或 VOICE_TOKEN,无法查询转写网关。'), {
  2832. status: 'needs_provider_config'
  2833. });
  2834. }
  2835. const response = await fetch(`${TRANSCRIPTION_GATEWAY_BASE_URL}/result`, {
  2836. method: 'POST',
  2837. headers: {
  2838. Authorization: bearerAuth(token),
  2839. Accept: 'application/json',
  2840. 'Content-Type': 'application/json'
  2841. },
  2842. body: JSON.stringify({ orderId })
  2843. });
  2844. const data = await response.json().catch(() => ({}));
  2845. if (!response.ok) {
  2846. throw new Error(data.error?.message || data.error || data.message || `转写查询失败 HTTP ${response.status}`);
  2847. }
  2848. return data;
  2849. }
  2850. function gatewayValue(data, key) {
  2851. return data?.[key] ?? data?.data?.[key] ?? data?.result?.[key] ?? data?.content?.[key];
  2852. }
  2853. function normalizeSegmentTime(value) {
  2854. const number = Number(value);
  2855. if (!Number.isFinite(number)) return null;
  2856. return number > 1000 ? number / 1000 : number;
  2857. }
  2858. function normalizeTranscriptSegments(segments) {
  2859. return (Array.isArray(segments) ? segments : []).map(segment => ({
  2860. start: normalizeSegmentTime(segment.start ?? segment.begin ?? segment.bg),
  2861. end: normalizeSegmentTime(segment.end ?? segment.ed),
  2862. text: cleanText(segment.text || segment.onebest || segment.content)
  2863. })).filter(segment => segment.text);
  2864. }
  2865. async function pollTranscriptJob(job) {
  2866. if (!job.orderId || job.status === 'completed' || job.status === 'failed') return job;
  2867. const data = await queryTranscriptGateway(job.orderId);
  2868. const status = cleanText(gatewayValue(data, 'status')).toLowerCase();
  2869. const text = cleanText(gatewayValue(data, 'text'));
  2870. const segments = normalizeTranscriptSegments(gatewayValue(data, 'segments'));
  2871. if (status === 'completed' || text || segments.length) {
  2872. return updateTranscriptJob(job, {
  2873. status: 'completed',
  2874. stageLabel: '转写完成',
  2875. progress: 100,
  2876. text: text || segments.map(segment => segment.text).join('\n'),
  2877. segments
  2878. });
  2879. }
  2880. if (status === 'failed' || status === 'error') {
  2881. return updateTranscriptJob(job, {
  2882. status: 'failed',
  2883. stageLabel: '转写失败',
  2884. errorMessage: gatewayValue(data, 'error') || gatewayValue(data, 'message') || '转写网关返回失败'
  2885. });
  2886. }
  2887. return updateTranscriptJob(job, {
  2888. status: 'polling_provider',
  2889. stageLabel: '转写处理中',
  2890. progress: Math.max(Number(job.progress || 0), 85),
  2891. warnings: [...new Set([...(job.warnings || []), '转写任务仍在处理中。'])]
  2892. });
  2893. }
  2894. async function runTranscriptJob(job, input) {
  2895. try {
  2896. updateTranscriptJob(job, { status: 'resolving_detail', stageLabel: '正在获取视频详情', progress: 10 });
  2897. const detail = await fetchDouyinDetailForTranscript(input);
  2898. const awemeId = cleanText(detail.aweme_id || job.awemeId);
  2899. if (awemeId && awemeId !== job.awemeId) updateTranscriptJob(job, { awemeId });
  2900. updateTranscriptJob(job, { status: 'selecting_media', stageLabel: '正在选择可转写媒体', progress: 25 });
  2901. const suppliedMediaUrl = cleanText(input.mediaUrl);
  2902. const candidate = suppliedMediaUrl
  2903. ? { url: suppliedMediaUrl, kind: /audio|m4a|mp3|aac/i.test(suppliedMediaUrl) ? 'audio' : 'video', keyPath: 'input.mediaUrl' }
  2904. : selectMediaCandidate(detail);
  2905. if (!candidate?.url) {
  2906. updateTranscriptJob(job, {
  2907. status: 'needs_media',
  2908. stageLabel: '未找到可转写媒体',
  2909. progress: 25,
  2910. warnings: [...(job.warnings || []), '视频详情中未找到音频或视频下载地址。']
  2911. });
  2912. return;
  2913. }
  2914. updateTranscriptJob(job, {
  2915. status: 'downloading_media',
  2916. stageLabel: '正在下载媒体',
  2917. progress: 45,
  2918. mediaUrl: candidate.url,
  2919. sourceKind: candidate.kind === 'audio' ? 'douyin_audio' : 'douyin_video'
  2920. });
  2921. const sourcePath = await downloadTranscriptMedia(candidate, job);
  2922. updateTranscriptJob(job, { localVideoPath: sourcePath });
  2923. const durationMs = Number(input.durationMs || durationFromDetail(detail) || 0);
  2924. let audioPath = sourcePath;
  2925. if (candidate.kind !== 'audio') {
  2926. updateTranscriptJob(job, { status: 'extracting_audio', stageLabel: '正在提取音频', progress: 65 });
  2927. audioPath = await extractAudioForTranscript(sourcePath, job);
  2928. }
  2929. updateTranscriptJob(job, {
  2930. status: 'submitting_provider',
  2931. stageLabel: '正在提交转写网关',
  2932. progress: 80,
  2933. localAudioPath: audioPath,
  2934. durationMs
  2935. });
  2936. const uploaded = await uploadTranscriptGateway(audioPath, durationMs);
  2937. updateTranscriptJob(job, {
  2938. status: 'polling_provider',
  2939. stageLabel: '转写任务已提交',
  2940. progress: 85,
  2941. orderId: uploaded.orderId,
  2942. estimateTime: uploaded.estimateTime,
  2943. warnings: [...(job.warnings || []), `已提交转写任务:${uploaded.orderId}`]
  2944. });
  2945. } catch (error) {
  2946. updateTranscriptJob(job, {
  2947. status: error.status || 'failed',
  2948. stageLabel: '转写任务失败',
  2949. errorMessage: error.message || '转写任务失败',
  2950. warnings: [...(job.warnings || []), error.message || '转写任务失败']
  2951. });
  2952. }
  2953. }
  2954. app.post('/api/douyin/transcript/start', (req, res) => {
  2955. const input = req.body || {};
  2956. if (!cleanText(input.awemeId) && !cleanText(input.sourceUrl) && !cleanText(input.mediaUrl)) {
  2957. return res.status(400).json({ success: false, error: '缺少 awemeId、sourceUrl 或 mediaUrl,无法创建转写任务。' });
  2958. }
  2959. const job = createTranscriptJob(input);
  2960. transcriptTasks.set(job.id, job);
  2961. res.json({ success: true, job: { ...job } });
  2962. setImmediate(() => runTranscriptJob(job, input));
  2963. });
  2964. app.get('/api/douyin/transcript/:jobId', async (req, res) => {
  2965. const job = transcriptTasks.get(req.params.jobId);
  2966. if (!job) {
  2967. return res.status(404).json({ success: false, error: '未找到逐字稿任务。' });
  2968. }
  2969. try {
  2970. if (job.status === 'polling_provider' && job.orderId) {
  2971. await pollTranscriptJob(job);
  2972. }
  2973. res.json({ success: true, job: { ...job } });
  2974. } catch (error) {
  2975. updateTranscriptJob(job, {
  2976. status: error.status || 'failed',
  2977. stageLabel: '查询转写结果失败',
  2978. errorMessage: error.message || '查询转写结果失败',
  2979. warnings: [...(job.warnings || []), error.message || '查询转写结果失败']
  2980. });
  2981. res.json({ success: true, job: { ...job } });
  2982. }
  2983. });
  2984. app.post('/api/douyin/transcript/:jobId/retry', (req, res) => {
  2985. const previous = transcriptTasks.get(req.params.jobId);
  2986. if (!previous) {
  2987. return res.status(404).json({ success: false, error: '未找到逐字稿任务。' });
  2988. }
  2989. const body = req.body || {};
  2990. const input = {
  2991. ...body,
  2992. awemeId: body.awemeId || previous.awemeId,
  2993. analysisId: body.analysisId || previous.analysisId,
  2994. mediaUrl: body.mediaUrl || previous.mediaUrl
  2995. };
  2996. const job = createTranscriptJob(input);
  2997. transcriptTasks.set(job.id, job);
  2998. res.json({ success: true, job: { ...job } });
  2999. setImmediate(() => runTranscriptJob(job, input));
  3000. });
  3001. // ==================== 健康检查 ====================
  3002. app.get('/api/health', (req, res) => {
  3003. res.json({
  3004. status: 'ok',
  3005. timestamp: new Date().toISOString(),
  3006. project: PROJECT_ROOT,
  3007. services: {
  3008. manifest: fs.existsSync(MANIFEST_PATH),
  3009. whisperDir: fs.existsSync(WHISPER_DIR),
  3010. videoDir: fs.existsSync(DATA_VIDEO_DIR) || fs.existsSync(LEGACY_VIDEO_DIR),
  3011. douyinTranscriptWorker: {
  3012. enabled: true,
  3013. tempDir: TRANSCRIPT_TEMP_DIR,
  3014. taskCount: transcriptTasks.size,
  3015. douyinGateway: DOUYIN_API_BASE_URL,
  3016. transcriptionGateway: TRANSCRIPTION_GATEWAY_BASE_URL,
  3017. douyinGatewayMaxAttempts: DOUYIN_GATEWAY_MAX_ATTEMPTS,
  3018. hasVocToken: !!getVocToken()
  3019. }
  3020. }
  3021. });
  3022. });
  3023. // ==================== 启动 ====================
  3024. app.listen(PORT, () => {
  3025. console.log('');
  3026. console.log('========================================');
  3027. console.log(` 🚀 后端服务已启动: http://localhost:${PORT}`);
  3028. console.log(` 📁 项目根目录: ${PROJECT_ROOT}`);
  3029. console.log(` 📋 接口列表:`);
  3030. console.log(` GET /api/health — 健康检查`);
  3031. console.log(` GET /api/whisper/status — Whisper 可用性`);
  3032. console.log(` POST /api/whisper/transcribe — 语音转文字`);
  3033. console.log(` GET /api/manifest — 获取视频清单`);
  3034. console.log(` PUT /api/manifest/:videoId — 更新视频信息`);
  3035. console.log(` POST /api/manifest — 添加视频条目`);
  3036. console.log(` GET /api/files/whisper/:id — Whisper 输出文件`);
  3037. console.log(` GET /api/files/read?path= — 读取项目文件`);
  3038. console.log(` POST /api/upload/video — 上传视频文件`);
  3039. console.log(` POST /api/remix/extract-audio — 提取视频音频`);
  3040. console.log(` POST /api/remix/upload-asset — 上传重塑素材到 Parse`);
  3041. console.log(` POST /api/download/video — 下载远程视频到本地库`);
  3042. console.log(` GET /api/download/video/:id — 查询下载任务进度`);
  3043. console.log(` ---`);
  3044. console.log(` GET /api/tasks — 获取任务列表`);
  3045. console.log(` POST /api/tasks — 创建任务`);
  3046. console.log(` PUT /api/tasks/:id — 更新任务`);
  3047. console.log(` DEL /api/tasks/:id — 删除任务`);
  3048. console.log(` ---`);
  3049. console.log(` GET /api/history — 获取历史记录`);
  3050. console.log(` POST /api/history — 添加历史`);
  3051. console.log(` DEL /api/history/:id — 删除历史`);
  3052. console.log(` DEL /api/history — 清空历史`);
  3053. console.log(` ---`);
  3054. console.log(` GET /api/results — 获取结果`);
  3055. console.log(` POST /api/results — 添加结果`);
  3056. console.log(` PUT /api/results/:id — 更新结果`);
  3057. console.log(` DEL /api/results/:id — 删除结果`);
  3058. console.log(` ---`);
  3059. console.log(` POST /api/llm/chat — LLM 对话(OpenAI格式)`);
  3060. console.log(` POST /api/llm/gemini — Gemini 媒体识别`);
  3061. console.log('========================================');
  3062. console.log('');
  3063. });