quickly.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const crypto = require('crypto');
  2. function registerQuicklyRoutes(app, options) {
  3. const appKey = options.appKey;
  4. const appSecret = options.appSecret;
  5. const accountId = options.accountId;
  6. const callbackUrl = options.callbackUrl;
  7. const relayUrl = options.relayUrl;
  8. const queryUrl = options.queryUrl || 'https://server.fmode.cn/api/functions';
  9. app.post('/api/quickly/create', async (req, res) => {
  10. try {
  11. const { videoUrls, options: createOptions = {} } = req.body;
  12. if (!videoUrls || !Array.isArray(videoUrls) || videoUrls.length === 0) {
  13. return res.status(400).json({ error: '缺少 videoUrls 参数' });
  14. }
  15. const timestamp = Date.now().toString();
  16. const signStr = timestamp + '#' + appSecret;
  17. const sign = crypto.createHash('md5').update(signStr).digest('hex');
  18. const materialList = videoUrls.map(url => ({ type: 'video', value: url }));
  19. const preId = timestamp;
  20. const apiBody = {
  21. account_id: accountId,
  22. callback_url: callbackUrl,
  23. material_list: materialList,
  24. tags: createOptions.tags || '视频,AI生成',
  25. proportion: createOptions.proportion || '9:16',
  26. video_duration: createOptions.videoDuration || { min: 10, max: 30 },
  27. pre_id: preId,
  28. compose_number: 1,
  29. ai_voice: createOptions.aiVoice ?? 1,
  30. ai_bgm: createOptions.aiBgm ?? 1,
  31. ai_flower: 1,
  32. ai_subtitle: createOptions.aiSubtitle ?? 0,
  33. original_voice: 0,
  34. };
  35. const relayData = JSON.stringify({
  36. apiPath: '/v2/video/vlog/create',
  37. apiBody,
  38. appKey,
  39. timestamp,
  40. sign,
  41. });
  42. const body = JSON.stringify({ action: 'relay', relayData });
  43. console.log(`📦 一键成片 - timestamp=${timestamp}, sign=${sign}`);
  44. console.log(` 材料: ${videoUrls.length} 个视频`);
  45. const response = await fetch(relayUrl, {
  46. method: 'POST',
  47. headers: { 'Content-Type': 'application/json' },
  48. body,
  49. });
  50. const result = await response.json();
  51. console.log('📦 一键成片 - 响应:', JSON.stringify(result).substring(0, 300));
  52. res.json(result);
  53. } catch (err) {
  54. console.error('❌ 一键成片代理失败:', err.message);
  55. res.status(500).json({ error: err.message });
  56. }
  57. });
  58. app.post('/api/quickly/query', async (req, res) => {
  59. try {
  60. const { taskId } = req.body;
  61. if (!taskId) return res.status(400).json({ error: '缺少 taskId 参数' });
  62. const body = JSON.stringify({
  63. id: 'sWvRr8RvPT',
  64. _ApplicationId: 'ncloudmaster',
  65. action: 'query',
  66. taskId,
  67. });
  68. const response = await fetch(queryUrl, {
  69. method: 'POST',
  70. headers: { 'Content-Type': 'application/json' },
  71. body,
  72. });
  73. const result = await response.json();
  74. res.json(result);
  75. } catch (err) {
  76. console.error('❌ 一键成片查询失败:', err.message);
  77. res.status(500).json({ error: err.message });
  78. }
  79. });
  80. }
  81. module.exports = { registerQuicklyRoutes };