HarmonyOS鸿蒙Next中视频压缩插件报错@ohos/videocompressor
HarmonyOS鸿蒙Next中视频压缩插件报错@ohos/videocompressor 在模拟器测试的,换了好几个视频,都不行,是什么原因[CompressUtil], videoCompressor code:-1 --error message:CreateVideoEncode error,视频格式不对吗?
10-29 17:12:21.204 6382-6382 A03d00/JSAPP com.examp…et_daily I VideoCompressor constructor
10-29 17:12:21.205 6382-6382 A03d00/JSAPP com.examp…et_daily I quality:2–videoCompressor outPutFilePath:/data/storage/el2/base/haps/entry/files/1761729141205.mp4
10-29 17:12:21.253 6382-6382 A03d00/JSAPP com.examp…et_daily I videoCompressor inputFile fd:96
10-29 17:12:21.254 6382-6382 A03200/videoCompressor com.examp…et_daily E inputSize:3049065
10-29 17:12:21.254 6382-6382 A03200/videoCompressor com.examp…et_daily E outputPath:/data/storage/el2/base/haps/entry/files/1761729141205.mp4
10-29 17:12:21.318 6382-6411 A03200/videoCompressor com.examp…et_daily E demuxer:audioMime:audio/mp4a-latm–audioBitrate:130530–aacIsADTS:1–audioSampleFormat:9–audioSampleRate:44100–audioChannelCount2–audioTrackId:0
10-29 17:12:21.318 6382-6411 A03200/videoCompressor com.examp…et_daily E demuxer:videoMime:video/avc–width:1280–height:720–frameRate:30.000000–bitrate2592644–videoTrackId:1
10-29 17:12:21.318 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateDemuxer end
10-29 17:12:21.318 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateMutex start
10-29 17:12:21.319 6382-6411 A03200/videoCompressor com.examp…et_daily E muxer:audioMime:audio/mp4a-latm–audioSampleRate:44100–audioChannelCount:2–audioBitrate:130530
10-29 17:12:21.319 6382-6411 A03200/videoCompressor com.examp…et_daily E muxer:videoMime:video/avc–videoWidth:1280–videoHeight:720
10-29 17:12:21.321 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateMutex end
10-29 17:12:21.321 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateVideoEncode start CreateVideoEncoder
10-29 17:12:21.327 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateVideoEncoder error
10-29 17:12:21.327 6382-6411 A03200/videoCompressor com.examp…et_daily E CreateVideoEncode error
10-29 17:12:21.330 6382-6382 A03200/videoCompressor com.examp…et_daily E delete outputFile
10-29 17:12:21.330 6382-6382 A03200/videoCompressor com.examp…et_daily E callback outputPath:/data/storage/el2/base/haps/entry/files/1761729141205.mp4
10-29 17:12:21.333 6382-6382 A0ff00/[PetDaily] com.examp…et_daily I [CompressUtil], videoCompressor code:-1 --error message:CreateVideoEncode error
更多关于HarmonyOS鸿蒙Next中视频压缩插件报错@ohos/videocompressor的实战教程也可以访问 https://www.itying.com/category-93-b0.html
日志显示CreateVideoEncoder error表明视频编码器创建失败,可能原因包括:
- 输入视频的编码格式与设备支持的编码器不兼容(如H.265格式设备不支持)
- 模拟器缺少硬件编码加速支持
- 分辨率/帧率超出设备能力(日志中视频分辨率1280x720、帧率30)
可以尝试使用AVCodec Kit视频编码功能,将未经压缩的视频数据传输至视频编码器进行编码处理,通过设定所需的编码格式、比特率、帧率等参数,并对编码输出进行控制,从而实现视频文件的压缩目的
int32_t VideoEncoder::Configure(const SampleInfo &sampleInfo) {
OH_AVFormat *format = OH_AVFormat_Create();
CHECK_AND_RETURN_RET_LOG(format != nullptr, AVCODEC_SAMPLE_ERR_ERROR, "AVFormat create failed");
OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, sampleInfo.videoWidth);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, sampleInfo.videoHeight);
OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, sampleInfo.frameRate);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, sampleInfo.pixelFormat);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, sampleInfo.bitrateMode);
OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, sampleInfo.bitrate);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, sampleInfo.hevcProfile);
// Setting HDRVivid-related parameters
if (sampleInfo.isHDRVivid) {
OH_AVFormat_SetIntValue(format, OH_MD_KEY_I_FRAME_INTERVAL, sampleInfo.iFrameInterval);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_RANGE_FLAG, sampleInfo.rangFlag);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_COLOR_PRIMARIES, sampleInfo.primary);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_TRANSFER_CHARACTERISTICS, sampleInfo.transfer);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_MATRIX_COEFFICIENTS, sampleInfo.matrix);
}
// ...
// Setting the Encoder
int ret = OH_VideoEncoder_Configure(encoder_, format);
OH_AVFormat_Destroy(format);
format = nullptr;
CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, AVCODEC_SAMPLE_ERR_ERROR, "Config failed, ret: %{public}d", ret);
return AVCODEC_SAMPLE_ERR_OK;
}
官方提供了多种方案,可以参考地址
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs/faqs-avsession-13
更多关于HarmonyOS鸿蒙Next中视频压缩插件报错@ohos/videocompressor的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,视频压缩插件@ohos/videocompressor报错通常由以下原因导致:插件版本与鸿蒙Next SDK不兼容;未在module.json5中正确声明权限或依赖;插件API调用方式不符合鸿蒙Next规范。排查步骤包括:检查插件版本是否支持鸿蒙Next;确认权限配置(如ohos.permission.READ_MEDIA和ohos.permission.WRITE_MEDIA);验证压缩参数是否符合要求。
根据日志分析,问题出现在视频编码器创建阶段(CreateVideoEncoder error)。虽然视频格式(H.264/AVC,1280x720,30fps)和音频格式(AAC)都符合常见标准,但错误代码-1通常表示底层编码器初始化失败。
可能的原因包括:
- 模拟器硬件编码支持不足,建议在真机测试
- 视频编码参数不兼容,尝试降低分辨率或码率
- 文件路径权限问题,检查输出目录写入权限
- 视频编码器资源占用冲突,确保没有其他进程占用编码资源
建议先调整视频压缩参数,使用较低的分辨率(如640x480)和码率重新测试,同时验证文件存储权限设置。

