小程序
小游戏
企业微信
微信支付
扫描小程序码分享
如题,纯血鸿蒙系统(HarmonyOS 5以上),在调用wx.compressVideo之后走fail,无法使用
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
验证是正常的,麻烦提供下能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
请问解决了吗,我也碰到同样问题。 nova14 活力版 HarmonyOS 5.1.0
就这么简单的代码,其他机型都能压缩。 nova14 活力版 就报错,走fail .
wx.compressVideo({
src: para.filePath,
quality: "low",
success: (res) => {
para["filePath"] = res.tempFilePath;
para["size"] = res.size;
resolve(para)
},
fail: (res => {
console.log("压缩失败," ,res ); ---> 这里提示 压缩失败, {"errMsg": "compressVideo:fail:compress failed."}
})
});
本回答由AI生成,可能已过期、失效或不适用于当前情形,请谨慎参考
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
验证是正常的,麻烦提供下能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
请问解决了吗,我也碰到同样问题。 nova14 活力版 HarmonyOS 5.1.0
就这么简单的代码,其他机型都能压缩。 nova14 活力版 就报错,走fail .
wx.compressVideo({
src: para.filePath,
quality: "low",
success: (res) => {
para["filePath"] = res.tempFilePath;
para["size"] = res.size;
resolve(para)
},
fail: (res => {
console.log("压缩失败," ,res ); ---> 这里提示 压缩失败, {"errMsg": "compressVideo:fail:compress failed."}
resolve(para)
})
});
function compressVideo(para) {
console.log("call util.compressVideo....")
console.log("原始参数:", para)
return new Promise(async (resolve, reject) => {
const filePath = para.filePath;
let originalSize = para.size || 0;
let videoInfo;
let isOriginSuccess = false;
let progressTimer = null; // 进度定时器
let currentProgress = 0; // 当前进度
// --------------------------
// 第一步:初始化进度提示(动态更新百分比)
// --------------------------
const updateProgress = () => {
// 进度递增:根据视频大小调整递增速度(大视频慢,小视频快)
const step = originalSize > 100*1024*1024 ? 2 : 5; // 100MB以上每次+2%,否则+5%
currentProgress = Math.min(currentProgress + step, 95); // 最高到95%,留最后5%给完成
wx.showLoading({
title: `视频压缩中(${currentProgress}%)`,
mask: true
});
};
// --------------------------
// 第二步:优先尝试原逻辑(带进度)
// --------------------------
// 启动进度定时器
currentProgress = 0;
progressTimer = setInterval(updateProgress, 300); // 每300ms更新一次进度
updateProgress(); // 立即执行一次
await new Promise((originResolve) => {
wx.compressVideo({
src: filePath,
quality: "medium",
success: (res) => {
clearInterval(progressTimer); // 停止进度更新
console.log("原逻辑压缩成功:", res);
if (res.size > 1024 && res.tempFilePath) {
para["filePath"] = res.tempFilePath;
para["size"] = res.size;
isOriginSuccess = true;
// 显示100%完成
wx.showToast({
title: '视频压缩完成(100%)',
icon: 'success',
duration: 2000,
mask: true
});
} else {
console.log("原逻辑压缩文件损坏,准备降级到码率压缩");
isOriginSuccess = false;
// 重启进度定时器,开始降级压缩进度
currentProgress = 0;
progressTimer = setInterval(updateProgress, 300);
updateProgress();
}
originResolve();
},
fail: (err) => {
clearInterval(progressTimer); // 停止进度更新
console.error("原逻辑压缩失败,准备降级到码率压缩:", err);
isOriginSuccess = false;
// 重启进度定时器,开始降级压缩进度
currentProgress = 0;
progressTimer = setInterval(updateProgress, 300);
updateProgress();
originResolve();
}
});
});
// 原逻辑成功则关闭提示并返回
if (isOriginSuccess) {
wx.hideLoading();
resolve(para);
return;
}
// --------------------------
// 第三步:降级压缩(继续动态进度)
// --------------------------
const compressRatio = 0.6;
const minBitrate = 600;
// 获取原视频信息(保持进度更新)
try {
videoInfo = await new Promise((infoResolve, infoReject) => {
wx.getVideoInfo({
src: filePath,
success: (res) => {
originalSize = res.size || originalSize;
infoResolve(res);
},
fail: (err) => {
clearInterval(progressTimer); // 停止进度
wx.hideLoading();
console.error("获取视频信息失败:", err);
// wx.showToast({
// title: '压缩失败,使用原视频',
// icon: 'none',
// duration: 2000,
// mask: true
// });
infoReject(err);
}
});
});
} catch (err) {
resolve(para);
return;
}
// 执行降级压缩
wx.compressVideo({
src: filePath,
bitrate: Math.max(minBitrate, Math.floor(videoInfo.bitrate * compressRatio)),
fps: Math.floor(videoInfo.fps),
resolution: 1.0,
success: (res) => {
clearInterval(progressTimer); // 停止进度
wx.hideLoading();
if (res.size > 1024 && res.size < originalSize * 1.1 && res.tempFilePath) {
para["filePath"] = res.tempFilePath;
para["size"] = res.size;
wx.showToast({
title: `压缩完成`,
icon: 'success',
duration: 2500,
mask: true
});
} else {
// wx.showToast({
// title: '压缩失败,使用原视频',
// icon: 'none',
// duration: 2000,
// mask: true
// });
}
resolve(para);
},
fail: (err) => {
clearInterval(progressTimer); // 停止进度
wx.hideLoading();
console.error("降级压缩也失败:", err);
// wx.showToast({
// title: '压缩失败,使用原视频',
// icon: 'none',
// duration: 2000,
// mask: true
// });
resolve(para);
}
});
});
}