const { PrismaClient } = require('@prisma/client'); const prisma = new PrismaClient(); async function fixAudioUrls() { console.log('\n🔧 修复音频URL不一致问题\n'); console.log('=' .repeat(60)); const bookId = 5; // 查找所有audioUrl包含output.mp3但实际文件是segment_0.mp3的小节 const subsections = await prisma.bookChapter.findMany({ where: { bookId, level: 3, audioUrl: { contains: 'output.mp3' } }, select: { id: true, title: true, audioUrl: true, } }); console.log(`找到${subsections.length}个小节的audioUrl需要修复\n`); let fixedCount = 0; let notFoundCount = 0; for (const subsection of subsections) { // 从URL中提取UUID const uuid = subsection.audioUrl.match(/\/uploads\/([^\/]+)\//)?.[1]; if (!uuid) continue; const fs = require('fs'); const path = require('path'); // 检查segment_0.mp3是否存在 const segmentPath = path.join(__dirname, 'uploads', uuid, 'segment_0.mp3'); const outputPath = path.join(__dirname, 'uploads', uuid, 'output.mp3'); if (fs.existsSync(segmentPath) && !fs.existsSync(outputPath)) { // 更新数据库中的URL await prisma.bookChapter.update({ where: { id: subsection.id }, data: { audioUrl: `/uploads/${uuid}/segment_0.mp3` } }); console.log(`✅ ${subsection.title}: output.mp3 → segment_0.mp3`); fixedCount++; } else if (!fs.existsSync(segmentPath) && !fs.existsSync(outputPath)) { console.log(`❌ ${subsection.title}: 文件不存在,需要重新生成`); notFoundCount++; } } console.log('\n' + '=' .repeat(60)); console.log('\n📊 修复结果:\n'); console.log(` 已修复: ${fixedCount}个`); console.log(` 文件不存在: ${notFoundCount}个`); console.log(` 总计: ${subsections.length}个\n`); await prisma.$disconnect(); } fixAudioUrls().catch(console.error);