Google Cloud转码任务偶发Code 13内部错误排查求助
排查Google Cloud Video Transcoder偶发Code 13内部错误
代码层面的关键问题
输入/输出URI配置错误:
你的代码中inputUri和outputUri仅设置为Bucket根路径(gs://${bucketName}/),但Transcoder API要求inputUri必须指向具体的GCS文件路径,outputUri需要是唯一的目录或文件名(避免文件覆盖或冲突)。当前配置会导致API无法准确定位输入文件,这是引发偶发错误的核心原因。修复方式:
// 上传文件到GCS后,生成具体的输入URI const inputFileName = file.filename; const inputUri = `gs://${bucketName}/${inputFileName}`; // 生成唯一的输出路径,避免覆盖或冲突 const outputFileName = `transcoded-${Date.now()}-${path.parse(file.originalname).name}.mp4`; const outputUri = `gs://${bucketName}/${outputFileName}`;帧率处理可能引发兼容性问题:
代码中用Math.round(frameRate)将原视频帧率四舍五入,但部分非标准帧率(如29.97fps)被强制转为30fps后,可能与视频的时间戳不兼容,导致偶发转码失败。建议保留原帧率的精确值:frameRate: frameRate[0] / frameRate[1], // 保留原始精确帧率,而非四舍五入ffprobe命令存在兼容性与安全风险:
直接使用exec执行ffprobe命令时,若文件名包含空格、特殊字符会导致命令执行失败,还存在命令注入风险。建议改用spawn并传递参数数组:const { spawn } = require('child_process'); const hasAudio = (filePath) => { return new Promise((resolve, reject) => { const proc = spawn('ffprobe', [ '-v', 'error', '-select_streams', 'a:0', '-show_entries', 'stream=codec_type', '-of', 'default=noprint_wrappers=1:nokey=1', filePath ]); let stdout = ''; proc.stdout.on('data', (data) => stdout += data); proc.on('close', (code) => { if (code === 0) { resolve(stdout.trim() === 'audio'); } else { reject(new Error(`ffprobe exited with code ${code}`)); } }); proc.on('error', reject); }); };
服务端层面的排查方向
若修复代码后仍有偶发错误,可排查以下点:
- 区域资源限制:
asia-south1区域的Transcoder资源可能临时不足,可尝试切换到其他区域(如us-central1)测试。 - 视频源编码异常:部分视频存在损坏、编码格式不兼容的情况,可使用ffmpeg重新封装后再测试,排除源文件问题。
- API配额与权限:检查项目的Transcoder API配额是否耗尽,以及服务账号是否拥有
roles/transcoder.editor和GCS读写权限。
内容的提问来源于stack exchange,提问作者Waleed Daud
相关产品推荐
相关产品推荐

