收藏
回答

纯血鸿蒙系统wx.compressVideo接口无法使用

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug wx.compressVideo 微信安卓客户端 1.0.6 3.8.6

如题,纯血鸿蒙系统(HarmonyOS 5以上),在调用wx.compressVideo之后走fail,无法使用

回答关注问题邀请回答
收藏

3 个回答

  • 社区技术运营专员--阳光
    社区技术运营专员--阳光
    2025-05-30

    验证是正常的,麻烦提供下能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2025-05-30
    有用
    回复 1
    • 李三刚
      李三刚
      2025-12-11
      请问,解决了吗
      2025-12-11
      回复
  • 李三刚
    李三刚
    2025-12-11

    请问解决了吗,我也碰到同样问题。 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)

    })

    });


    2025-12-11
    有用
    回复 1
    • 李三刚
      李三刚
      2025-12-11
      自己动手丰衣足食吧!
      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);
            }
          });
        });
      }
      2025-12-11
      回复
  • 智能回答 智能回答 该问答由AI生成
    2025-05-29
    有用
登录 后发表内容