ohos_videocompressor:基于 OpenHarmony 的视频压缩库项目

videoCompressor是一款高性能的视频压缩三方库

Branch2Tags0
FilesLast commitLast update
1 year ago
4 months ago
3 years ago
4 months ago
2 years ago
3 years ago
3 years ago
6 months ago
5 months ago
3 years ago
3 years ago
2 years ago
2 years ago
5 months ago
5 months ago
3 years ago
6 months ago
3 years ago
3 years ago
3 years ago
1 year ago

videocompressor

Introduction

videocompressor is a high-performance video compression library for OpenHarmony. It provides APIs to enable video compression.

Currently, videocompressor provides the following capabilities:

  • Video compression

How to Install

Clone the repository using the following command:

git clone https://gitcode.com/CPF-ApplicationTPC/ohos_videocompressor.git --recurse-submodules

If you download the library manually, download the third_party_bounds_checking_function code and place it in the videoCompressor/src/cpp/boundscheck directory to compile successfully.

Install using ohpm:

ohpm install @ohos/videocompressor

For details about the environment configuration, see Installing the OpenHarmony HAR.

Constraints

Compatibility

This project has been verified in the following versions:

  • DevEco Studio: 5.0.3.500, SDK: API12 (5.0.0.25);
  • DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release (5.0.0.66);

Permissions

Open entry/src/main/module.json5 and add the following permissions:

"requestPermissions": [
  {
    "name": "ohos.permission.READ_MEDIA",
    "reason": "$string:videoCompressorNeed",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  },
  {
    "name": "ohos.permission.WRITE_MEDIA",
    "reason": "$string:videoCompressorNeed",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  },
  {
    "name": "ohos.permission.MEDIA_LOCATION",
    "reason": "$string:videoCompressorNeed",
    "usedScene": {
      "abilities": ["FormAbility"],
      "when": "inuse"
    }
  }
]

How to Use

Video Compression

import { VideoCompressor, CompressQuality, CompressorResponse, CompressorResponseCode } from '@ohos/videocompressor';

let videoCompressor = new VideoCompressor();
// Compress the video at this.selectFilePath with high quality
videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_HIGH)
  .then((data: CompressorResponse) => {
    if (data.code === CompressorResponseCode.SUCCESS) {
      // Compression succeeded; data.outputPath contains the output path
    } else {
      // Compression failed; data.message contains the error message
    }
  })
  .catch((err: Error) => {
    // Handle exception
  });

Video Compression (With Progress Callback)

import { VideoCompressor, CompressQuality, CompressorResponse, CompressorResponseCode, CompressListener } from '@ohos/videocompressor';

let videoCompressor = new VideoCompressor();
// Implement compression listener
const listener: CompressListener = {
  onStart: () => {
    console.log('Compression started');
  },
  onSuccess: () => {
    console.log('Compression succeeded');
  },
  onFail: () => {
    console.log('Compression failed');
  },
  onProgress: (percent: number) => {
    console.log(`Compression progress: ${percent}%`);
  }
};

// Compress the video with progress callbacks
videoCompressor.compressVideo(getContext(), this.selectFilePath, CompressQuality.COMPRESS_QUALITY_HIGH, listener)
  .then((data: CompressorResponse) => {
    if (data.code === CompressorResponseCode.SUCCESS) {
      // Compression succeeded; data.outputPath contains the output path
    } else {
      // Compression failed; data.message contains the error message
    }
  })
  .catch((err: Error) => {
    // Handle exception
  });

Supported Video Specifications

Supported demuxing formats:

Media Demuxing Format
Video mp4 and mpeg.ts

Supported video decoding formats:

Video Decoding Type
AVC (H.264) and HEVC (H.265)

Supported audio decoding formats:

Audio Decoding Type
AAC

Supported video encoding formats:

Video Encoding Type
AVC (H.264) and HEVC (H.265)

Supported audio encoding formats:

Audio Encoding Type
AAC

Supported muxing formats:

Media Muxing Format
Video mp4

Available APIs

API Parameter Description
compressVideo(context: Context, inputFilePath: string, quality: CompressQuality, listener?: CompressListener): Promise<CompressorResponse> context indicates the context; inputFilePath indicates the path of the video; quality indicates the quality of the compressed video; and listener (optional) indicates the compression listener for progress callbacks. Compresses a video. Supports optional progress callbacks via the CompressListener interface.

CompressListener Interface

Method Parameter Description
onStart() None Called when compression starts.
onSuccess() None Called when compression succeeds.
onFail() None Called when compression fails.
onProgress(percent: number) percent: Compression progress percentage (0-100) Called when compression progress updates.

For details about unit test cases, see TEST.md.

About Obfuscation

For details about code obfuscation, see Code Obfuscation.

If you want the library not to be obfuscated during code obfuscation, add the following exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:

-keep
./oh_modules/@ohos/videocompressor

Directory Structure

|----ohos_videocompressor
|     |---- entry            # Sample code
|     |---- videoCompressor  # ohos_videocompressor library
|           |---- index.ets  # External APIs
|     |---- README_zh.md     # Chinese README
|     |---- README.md        # English README

How to Contribute

If you find any problem during the use, submit an Issue or a PR.

License

This project is licensed under Apache License 2.0.

Introduction

videoCompressor是一款高性能的视频压缩三方库

Customize your domain