// 视频转码API - 完整版本(支持自动恢复) import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; import { transcodeQueue, cleanupTranscodeResidues, VIDEO_MATERIAL_TYPES } from '@/lib/video-transcode-queue'; import { executeTranscodeTask, enqueueVideoTranscodeTask } from '@/lib/video-transcode-task'; export const dynamic = 'force-dynamic'; export const maxDuration = 3600; // 1小时 // 恢复标志:确保只执行一次 let recoveryChecked = false; async function countInterruptedVideoTasks(): Promise { try { return await prisma.material.count({ where: { materialType: { in: VIDEO_MATERIAL_TYPES }, videoProcessingStatus: 'processing', videoSourceUrl: { not: null }, deletedAt: null, }, }); } catch (error) { console.error('[Transcode] Failed to count interrupted video tasks:', error); // 返回 -1 代表统计失败,后续逻辑会谨慎地触发兜底恢复 return -1; } } /** * R1修复:懒加载恢复中断的转码任务 * - 优先从新的 VideoTranscodeProgress 表恢复(持久化队列状态) * - 降级到旧的 Material.videoProcessingStatus 恢复(向后兼容) */ async function ensureRecoveryCompleted(): Promise { if (recoveryChecked) { return; } recoveryChecked = true; try { console.log('[Transcode] Starting recovery check...'); // R1修复:优先使用新的持久化队列恢复 let recovered = await transcodeQueue.recoverOnStartup(executeTranscodeTask); // 如果新表没有数据,并且存在真正被中断的任务,再降级到旧方法 if (recovered === 0) { const interruptedCount = await countInterruptedVideoTasks(); if (interruptedCount > 0 || interruptedCount === -1) { if (interruptedCount > 0) { console.log(`[Transcode] Found ${interruptedCount} interrupted processing tasks, running legacy recovery...`); } else { console.warn('[Transcode] Unable to verify interrupted tasks, running legacy recovery as a safeguard...'); } const { recovered: legacyRecovered, requeued } = await transcodeQueue.recoverInterruptedTasks( executeTranscodeTask, cleanupTranscodeResidues, { videoTypes: VIDEO_MATERIAL_TYPES } ); recovered += legacyRecovered + requeued; } else { console.log('[Transcode] No interrupted processing tasks detected, skipping legacy fallback.'); } } console.log(`[Transcode] Recovery completed: ${recovered} tasks recovered`); } catch (error) { console.error('[Transcode] Recovery failed:', error); // 恢复失败不影响正常流程,重置标志以便下次重试 recoveryChecked = false; } } // GET - 查询转码状态 export async function GET(request: NextRequest) { // 懒加载恢复:第一次调用时自动恢复中断的任务(包括 GET 请求) await ensureRecoveryCompleted(); try { const { searchParams } = new URL(request.url); const materialId = searchParams.get('materialId'); if (!materialId) { return NextResponse.json( { error: '缺少素材ID' }, { status: 400 } ); } const material = await prisma.material.findUnique({ where: { id: materialId }, select: { id: true, videoProcessingStatus: true, videoPreview540Url: true, videoPreviewClipUrl: true, videoCoverUrl: true, videoDuration: true, transcodeLastError: true, transcodeAttempts: true, } as any, }); if (!material) { return NextResponse.json( { error: '素材不存在' }, { status: 404 } ); } // 获取转码队列状态 const queueStatus = transcodeQueue.getStatus(); const isProcessing = queueStatus.processingIds.includes(materialId); const waitingIndex = queueStatus.waiting.findIndex(id => id === materialId); const waitingTotal = queueStatus.waiting.length; return NextResponse.json({ materialId: material.id, status: material.videoProcessingStatus || 'pending', hasPreview540: !!material.videoPreview540Url, hasClip: !!material.videoPreviewClipUrl, hasCover: !!material.videoCoverUrl, duration: material.videoDuration, attempts: material.transcodeAttempts || 0, lastError: material.transcodeLastError, progress: isProcessing ? queueStatus.progress.get(materialId) || null : null, stage: isProcessing ? queueStatus.stages.get(materialId) || null : null, queue: { waitingIndex: waitingIndex >= 0 ? waitingIndex + 1 : null, waitingTotal: waitingTotal, processingIds: queueStatus.processingIds, maxConcurrent: queueStatus.maxConcurrent, }, }); } catch (error) { console.error('[Transcode] GET error:', error); return NextResponse.json( { error: '查询转码状态失败' }, { status: 500 } ); } } // POST - 启动转码任务 export async function POST(request: NextRequest) { // 懒加载恢复:第一次调用时自动恢复中断的任务 await ensureRecoveryCompleted(); try { const { materialId, forceRetry } = await request.json(); if (!materialId) { return NextResponse.json( { error: '缺少素材ID' }, { status: 400 } ); } const material = await prisma.material.findFirst({ where: { id: materialId, deletedAt: null }, select: { id: true, videoSourceUrl: true, videoCoverUrl: true, videoProcessingStatus: true, materialType: true, } as any, }); if (!material) { return NextResponse.json( { error: '素材不存在或已被删除' }, { status: 404 } ); } // 检查是否为视频类型 if (!VIDEO_MATERIAL_TYPES.includes((material as any).materialType)) { return NextResponse.json( { error: '此素材不是视频类型' }, { status: 400 } ); } // 检查是否有源视频 if (!(material as any).videoSourceUrl) { return NextResponse.json( { error: '未找到源视频文件' }, { status: 400 } ); } // 检查转码状态 if ((material as any).videoProcessingStatus === 'processing' && !forceRetry) { return NextResponse.json( { error: '转码任务正在进行中', materialId }, { status: 409 } ); } if ((material as any).videoProcessingStatus === 'done' && !forceRetry) { return NextResponse.json( { message: '转码已完成', materialId }, { status: 200 } ); } // 判断是否需要生成封面图(用户未上传时才生成) const needsCoverImage = !(material as any).videoCoverUrl; // 加入转码队列 await transcodeQueue.add( materialId, (material as any).videoSourceUrl, needsCoverImage, executeTranscodeTask ); console.log(`[Transcode] Task queued for material ${materialId}`); return NextResponse.json({ message: '转码任务已启动', materialId, status: 'processing', queueStatus: transcodeQueue.getStatus(), }); } catch (error) { console.error('[Transcode] POST error:', error); return NextResponse.json( { error: '转码任务启动失败' }, { status: 500 } ); } }