fix-audio-urls.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function fixAudioUrls() {
  4. console.log('\n🔧 修复音频URL不一致问题\n');
  5. console.log('=' .repeat(60));
  6. const bookId = 5;
  7. // 查找所有audioUrl包含output.mp3但实际文件是segment_0.mp3的小节
  8. const subsections = await prisma.bookChapter.findMany({
  9. where: {
  10. bookId,
  11. level: 3,
  12. audioUrl: { contains: 'output.mp3' }
  13. },
  14. select: {
  15. id: true,
  16. title: true,
  17. audioUrl: true,
  18. }
  19. });
  20. console.log(`找到${subsections.length}个小节的audioUrl需要修复\n`);
  21. let fixedCount = 0;
  22. let notFoundCount = 0;
  23. for (const subsection of subsections) {
  24. // 从URL中提取UUID
  25. const uuid = subsection.audioUrl.match(/\/uploads\/([^\/]+)\//)?.[1];
  26. if (!uuid) continue;
  27. const fs = require('fs');
  28. const path = require('path');
  29. // 检查segment_0.mp3是否存在
  30. const segmentPath = path.join(__dirname, 'uploads', uuid, 'segment_0.mp3');
  31. const outputPath = path.join(__dirname, 'uploads', uuid, 'output.mp3');
  32. if (fs.existsSync(segmentPath) && !fs.existsSync(outputPath)) {
  33. // 更新数据库中的URL
  34. await prisma.bookChapter.update({
  35. where: { id: subsection.id },
  36. data: {
  37. audioUrl: `/uploads/${uuid}/segment_0.mp3`
  38. }
  39. });
  40. console.log(`✅ ${subsection.title}: output.mp3 → segment_0.mp3`);
  41. fixedCount++;
  42. } else if (!fs.existsSync(segmentPath) && !fs.existsSync(outputPath)) {
  43. console.log(`❌ ${subsection.title}: 文件不存在,需要重新生成`);
  44. notFoundCount++;
  45. }
  46. }
  47. console.log('\n' + '=' .repeat(60));
  48. console.log('\n📊 修复结果:\n');
  49. console.log(` 已修复: ${fixedCount}个`);
  50. console.log(` 文件不存在: ${notFoundCount}个`);
  51. console.log(` 总计: ${subsections.length}个\n`);
  52. await prisma.$disconnect();
  53. }
  54. fixAudioUrls().catch(console.error);