test-new-audio-generation.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function testNewAudioGeneration() {
  4. console.log('\n🎵 测试新生成的音频路径\n');
  5. console.log('=' .repeat(60));
  6. const bookId = 5;
  7. // 找到一个小节
  8. const subsection = await prisma.bookChapter.findFirst({
  9. where: {
  10. bookId,
  11. level: 3
  12. },
  13. select: {
  14. id: true,
  15. title: true,
  16. content: true,
  17. audioUrl: true,
  18. }
  19. });
  20. if (!subsection) {
  21. console.log('❌ 找不到小节\n');
  22. await prisma.$disconnect();
  23. return;
  24. }
  25. console.log(`测试小节: ${subsection.title}`);
  26. console.log(`ID: ${subsection.id}`);
  27. console.log(`当前audioUrl: ${subsection.audioUrl || '无'}\n`);
  28. // 调用API重新生成音频(使用批量生成)
  29. console.log('📤 调用批量音频生成API...\n');
  30. try {
  31. const response = await fetch(`http://localhost:3000/api/book-generator/langgraph/books/${bookId}/audio`, {
  32. method: 'POST',
  33. headers: { 'Content-Type': 'application/json' },
  34. body: JSON.stringify({ voiceId: 'cherry' }),
  35. });
  36. const data = await response.json();
  37. console.log('API响应:');
  38. console.log(` 状态码: ${response.status}`);
  39. console.log(` code: ${data.code}`);
  40. console.log(` message: ${data.message}\n`);
  41. if (data.code !== 0) {
  42. console.log('❌ API调用失败\n');
  43. await prisma.$disconnect();
  44. return;
  45. }
  46. console.log('✅ API调用成功,等待15秒让音频生成完成...\n');
  47. await new Promise(resolve => setTimeout(resolve, 15000));
  48. // 检查新生成的音频URL
  49. console.log('📊 检查新生成的音频URL:\n');
  50. const updatedChapter = await prisma.bookChapter.findUnique({
  51. where: { id: subsection.id },
  52. select: {
  53. id: true,
  54. title: true,
  55. audioUrl: true,
  56. audioDuration: true,
  57. }
  58. });
  59. console.log(`章节: ${updatedChapter.title}`);
  60. console.log(`audioUrl: ${updatedChapter.audioUrl || '无'}`);
  61. console.log(`audioDuration: ${updatedChapter.audioDuration || 0}秒\n`);
  62. // 检查文件
  63. const fs = require('fs');
  64. const path = require('path');
  65. const uuid = updatedChapter.audioUrl.match(/\/uploads\/([^\/]+)\//)?.[1];
  66. if (uuid) {
  67. const filePath = path.join(__dirname, 'uploads', uuid, 'output.mp3');
  68. const segmentPath = path.join(__dirname, 'uploads', uuid, 'segment_0.mp3');
  69. console.log('📁 检查文件:\n');
  70. console.log(`output.mp3: ${fs.existsSync(filePath) ? '✅ 存在' : '❌ 不存在'}`);
  71. console.log(`segment_0.mp3: ${fs.existsSync(segmentPath) ? '✅ 存在' : '❌ 不存在'}\n`);
  72. if (fs.existsSync(filePath)) {
  73. const stats = fs.statSync(filePath);
  74. console.log(`output.mp3 大小: ${(stats.size / 1024).toFixed(2)} KB\n`);
  75. }
  76. console.log('=' .repeat(60));
  77. console.log('\n📋 测试结论:\n');
  78. if (updatedChapter.audioUrl?.includes('output.mp3')) {
  79. console.log('✅ 音频URL使用output.mp3,路径正确!\n');
  80. } else if (updatedChapter.audioUrl?.includes('segment_0.mp3')) {
  81. console.log('❌ 音频URL仍使用segment_0.mp3,需要修复\n');
  82. } else {
  83. console.log('⚠️ 未知情况\n');
  84. }
  85. }
  86. } catch (error) {
  87. console.log('❌ 测试失败:', error.message);
  88. console.log(error.stack);
  89. }
  90. await prisma.$disconnect();
  91. }
  92. testNewAudioGeneration().catch(console.error);