使用AWS Elastic Transcoder转码MOV视频遇报错,求解决方案
问题描述
使用AWS Elastic Transcoder将.MOV视频压缩至360p和720p时,创建任务遇到以下错误:
4000 b17c3141-20d2-4168-bf7f-8fb8a5a5ca35: Amazon Elastic Transcoder could not interpret the media file.
尝试的代码如下:
import { S3, ElasticTranscoder } from "aws-sdk"; import { config } from "dotenv"; const awsConversionPresets = { 360: "1351620000001-000040", 720: "1351620000001-000010", }; const region = "ap-southeast-2"; config(); const getVideos = async () => { const s3 = new S3({ region }); const items = await s3 .listObjectsV2({ Bucket: "my-bucket-id", }) .promise(); if (items.Contents) { return items.Contents.map((content) => content.Key) as string[]; } throw new Error("No contents"); }; const transcodeVideos = async (videosArr: string[]) => { const transcoder = new ElasticTranscoder({ region }); for (const video of videosArr) { const job = await transcoder .createJob({ PipelineId: "1684136955957-d86lw1", Input: { Key: video, Container: "auto", AspectRatio: "auto", Resolution: "auto", FrameRate: "auto", Interlaced: "auto", }, Output: { Key: `test-output/${video}`, PresetId: "1351620000001-100180", }, }) .promise(); } }; const main = async () => { const videos = await getVideos(); transcodeVideos(videos); }; main();
需要排查错误并修正代码,实现MOV视频的转码压缩需求。
错误排查与代码修正
错误原因分析
出现该错误的常见原因:
- 容器识别失败:
Container: "auto"无法识别部分特殊编码的MOV文件,需显式指定容器类型。 - 预设ID不匹配:当前代码使用的预设ID
1351620000001-100180并非目标分辨率的标准预设,且可能与输入编码不兼容。 - 文件范围未过滤:代码处理S3桶中所有文件,包含非视频文件,导致无效任务。
- 权限缺失:Elastic Transcoder管道角色无S3源文件读取或输出文件写入权限。
修正后的代码
以下代码修复了上述问题,同时实现批量生成360p和720p转码文件的需求:
import { S3, ElasticTranscoder } from "aws-sdk"; import { config } from "dotenv"; // AWS官方标准预设ID:360p和720p H.264/AAC编码 const awsConversionPresets = { 360: "1351620000001-000040", 720: "1351620000001-000010", }; const region = "ap-southeast-2"; config(); // 仅获取桶中的MOV格式文件 const getVideos = async () => { const s3 = new S3({ region }); const items = await s3 .listObjectsV2({ Bucket: "my-bucket-id" }) .promise(); if (items.Contents) { return items.Contents .filter(content => content.Key.toLowerCase().endsWith('.mov')) .map(content => content.Key) as string[]; } throw new Error("桶中未找到MOV文件"); }; // 批量转码,同时生成360p和720p版本 const transcodeVideos = async (videosArr: string[]) => { const transcoder = new ElasticTranscoder({ region }); for (const video of videosArr) { const baseFileName = video.replace(/\.[^/.]+$/, ""); try { const job = await transcoder .createJob({ PipelineId: "1684136955957-d86lw1", Input: { Key: video, Container: "mov", // 显式指定容器类型 AspectRatio: "auto", Resolution: "auto", FrameRate: "auto", Interlaced: "auto", }, // 同时输出两个分辨率的文件 Outputs: [ { Key: `test-output/${baseFileName}-360p.mp4`, PresetId: awsConversionPresets[360], }, { Key: `test-output/${baseFileName}-720p.mp4`, PresetId: awsConversionPresets[720], } ] }) .promise(); console.log(`任务创建成功:${job.Job.Id},处理文件:${video}`); } catch (error) { console.error(`转码失败:${video},错误信息:`, error); } } }; const main = async () => { try { const videos = await getVideos(); if (videos.length === 0) { console.log("无需要转码的MOV文件"); return; } await transcodeVideos(videos); } catch (error) { console.error("主流程执行错误:", error); } }; main();
额外注意事项
- 权限验证:确保Elastic Transcoder的管道角色拥有
S3:GetObject(源桶)和S3:PutObject(目标桶)权限。 - 编码兼容性:若转码仍失败,用
ffprobe工具检查MOV文件编码,确保是Elastic Transcoder支持的格式(如H.264视频+AAC音频),专业编码(如ProRes)需先转成兼容格式再处理。 - 错误监控:代码添加了错误捕获,可单独排查每个文件的转码问题。
内容的提问来源于stack exchange,提问作者Manav Oza
相关产品推荐
相关产品推荐

