Ionic视频编辑器在Android Nougat/Oreo转码失败,遇InvalidOutputFormatException
这个问题我之前在处理Cordova/Ionic视频转码需求时也踩过坑,核心原因是Android 7.0(Nougat)和8.0(Oreo)的系统原生转码引擎仅支持AVC视频的Baseline Profile,而你的输入视频使用了High Profile(报错里的profile_idc: 100对应的就是High Profile),导致触发InvalidOutputFormatException。下面是几个经过验证的解决方案:
方案1:强制指定转码为Baseline Profile
如果使用的是cordova-plugin-video-editor,可以在调用转码方法时明确设置视频编码的Profile为Baseline,让插件适配系统限制。示例代码如下:
window.VideoEditor.transcodeVideo( (success) => console.log('转码完成:', success), (error) => console.error('转码失败:', error), { fileUri: '你的输入视频路径', outputFileName: 'transcoded_video', outputFileType: window.VideoEditor.OutputFileType.MPEG4, videoProfile: 'baseline' // 关键配置:指定Baseline Profile } );
如果插件没有暴露这个参数,你需要修改插件的Android原生代码:找到AndroidTranscoder的配置逻辑,将视频编码Profile设置为VideoEncoderProfile.BASELINE,确保转码输出符合系统要求。
方案2:替换为FFmpeg转码引擎
系统原生转码引擎的局限性较强,改用FFmpeg作为转码工具可以彻底解决兼容性问题。Ionic/Cordova有成熟的cordova-plugin-ffmpeg插件,支持几乎所有主流编码格式和Profile。示例代码:
// 构造FFmpeg转码命令,强制输出Baseline Profile的视频 const ffmpegCmd = `-i ${inputVideoUri} -c:v libx264 -profile:v baseline -level 3.0 -c:a aac -strict -2 ${outputVideoPath}`; cordova.plugins.ffmpeg.execute(ffmpegCmd, (output) => console.log('转码成功:', output), (error) => console.error('转码出错:', error) );
⚠️ 注意:FFmpeg库体积较大,建议使用精简版的FFmpeg包(只保留必要的编码模块)来控制APK大小。
方案3:预先检测视频Profile再处理
在转码前先检测输入视频的编码Profile,只对不兼容的视频(比如High Profile)执行转码操作,提升性能。可以用cordova-plugin-video-info插件获取视频元信息:
window.VideoInfo.getVideoInfo(inputVideoUri, (videoInfo) => { // profile_idc=66是Baseline,77是Main,100是High if (videoInfo.videoProfileIdc === 100) { // 执行转码为Baseline的操作 } else { // 视频格式兼容,直接使用原文件 } }, (error) => console.error('获取视频信息失败:', error) );
额外注意事项
- 部分Android厂商会定制系统转码引擎,建议在不同品牌的7.0/8.0设备上测试验证;
- 如果修改了插件原生代码,记得将修改后的插件同步到Ionic项目,或者fork插件仓库自行维护版本。
内容的提问来源于stack exchange,提问作者kamal soni

