HarmonyOS/OpenHarmony视频压缩库

HarmonyOS/OpenHarmony视频压缩库

视频压缩库的选择

现市面上针对视频压缩的库大多都是Android的库,HarmonyOS想要对视频压缩。其他公司的策略就是自己去移植Android的库给到HarmonyOS使用。
我在Github上找到一个使用率较高的Android视频压缩库videocompressor准备调研移植看看。
后发现在Gitee上有同名库ohos_videocompressor,查看得知当前库隶属于华为维护的生态库。

OpenHarmony-SIG/ohos_videocompressor 视频压缩库简介

存在问题:

当前ohos_videocompressor还不支持HEVC格式(HarmonyOS手机拍摄的视频格式),当然此问题已经修复,需要等他们发布. 目前最新版本^1.0.4-rc.1

"dependencies": {
"@ohos/videocompressor": "^1.0.4-rc.1"
}

压缩成果展示

手机录屏5分钟得到160.7M视频文件
通过高质量压缩得到104.9M
通过中质量压缩得到73.6M
通过低质量压缩得到41.8M

三方压缩库打包生成hap包后的大小

项目打包后所占用的资源大小为2903403字节 约2.8M

查看发布的最新版本

当前地址作用,可以在后续更新修复HEVC格式发布后,根据查看标签名更新版本号
TAG地址 : https://gitee.com/openharmony-sig/ohos_videocompressor/tags

引用方法:

通过DevEco studio 当前项目终端(Terminal)运行命令,直接集成到我们自己项目

ohpm install @ohos/videocompressor

安装执行完后版本号为 "@ohos/videocompressor": "^1.0.3"
想要使用最新版本需要到项目根目录下
oh-package.json5 文件夹下找到dependencies修改videocompressor版本号

"dependencies": {
"@ohos/videocompressor": "^1.0.3"  //改完当前最新"^1.0.4-rc.1"即可
}

使用方法

 let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_HIGH) // 分别对应3个压缩质量 COMPRESS_QUALITY_HIGH,COMPRESS_QUALITY_MEDIUM, COMPRESS_QUALITY_LOW 
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                //outputPath: 压缩后的文件地址
                  console.log("videoCompressor HIGH message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor HIGH code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
              console.log("videoCompressor HIGH get error message" + err.message);
            })

Demo运行的全部代码示例

import { CompressorResponseCode, CompressQuality, VideoCompressor } from '@ohos/videoCompressor';
import picker from '@ohos.file.picker';

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private selectVideoText: string = "选择视频";
  private highTextTest: string = "高质量压缩测试"
  private mediaTextTest: string = "中质量压缩测试"
  private lowTextTest: string = "低质量压缩测试"
  startColor: Color = Color.Blue
  @State selectFilePath: string = ""

  selectVideo() {
    let that = this;
    try {
      let photoSelectOptions = new picker.PhotoSelectOptions();
      photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE;
      photoSelectOptions.maxSelectNumber = 1;
      let photoPicker = new picker.PhotoViewPicker();
      photoPicker.select(photoSelectOptions).then((PhotoSelectResult) => {
        that.selectFilePath = PhotoSelectResult.photoUris[0];
        console.info('videoCompressor select selectFilePath:' + that.selectFilePath)
      }).catch((err: object) => {
        console.error("videoCompressor select failed with err:" + err);
      })
    } catch (err) {
      console.error("videoCompressor select failed with err2:" + err);
    }
  }

  build() {
    Row() {
      Column() {
        Text(this.selectFilePath).fontSize(18).margin({ top: 30 }).fontWeight(FontWeight.Bold);
        Button(this.selectVideoText)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width("80%")
          .onClick(() => {
            console.log("selectVideo");
            this.selectVideo();
          })
        TextInput({ placeholder: '请输入应用内存文件名,例如www.mp4' })
          .fontSize(20)
          .margin({ top: 30 })
          .width('80%')
          .onChange((value) => {
            this.selectFilePath = getContext().filesDir + "/" + value
          })
        Button(this.highTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width("80%")
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_HIGH)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor HIGH message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor HIGH code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
              console.log("videoCompressor HIGH get error message" + err.message);
            })
          })

        Button(this.mediaTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width("80%")
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_MEDIUM)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor MEDIUM message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor MEDIUM code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
              console.log("videoCompressor HIGH get error message" + err.message);
            })
          })
        Button(this.lowTextTest)
          .fontSize(20)
          .margin({ top: 30 })
          .height(50)
          .backgroundColor(this.startColor)
          .width("80%")
          .onClick(() => {
            let videoCompressor = new VideoCompressor();
            videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_LOW)
              .then((data) => {
                if (data.code == CompressorResponseCode.SUCCESS) {
                  console.log("videoCompressor LOW message:" + data.message + "--outputPath:" + data.outputPath);
                } else {
                  console.log("videoCompressor LOW code:" + data.code + "--error message:" + data.message);
                }
              }).catch((err: Error) => {
              console.log("videoCompressor LOW get error message" + err.message);
            })
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

执行压缩后,压缩视频文件查看地址

在手机目录下查看
DevEco studio–> Device File Browser > date --> el2 --> 100 --> base --> 项目地址 --> haps --> entry --> files

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值