可用于在 OpenHarmony 平台压缩视频、移除音频、生成缩略图,实现跨平台兼容。项目鸿蒙端基于 FFmpeg 原生实现,高效轻量,支持进度监听,生成 MP4 容器与 AAC 音频确保多平台适配。【此简介由AI生成】
模板版本: v0.0.1
video_compress
本项目基于 video_compress 开发。
1. 安装与使用
1.1 安装方式
进入到工程目录并在 pubspec.yaml 中添加以下依赖:
pubspec.yaml
...
dependencies:
video_compress:
git:
url: https://gitcode.com/xiaoyangming/flutter_video_compress_ohos.git
ref: v3.1.4-ohos-3.27
...
仓库名
flutter_video_compress_ohos,Dart 包名仍为video_compress。
执行命令
flutter pub get
1.3 构建 example(可选)
cd example
flutter pub get
flutter build hap --debug
在 DevEco Studio 中打开 example/ohos 配置签名(File → Project Structure → Signing Configs)。
勿将 example/ohos/build-profile.json5 中的本地签名路径与密码提交到 Git。
1.2 使用案例
使用案例详见 example 功能演示页(压缩 / 缩略图 / 媒体信息)
2. 约束与限制
2.1 兼容性
在以下版本中已测试通过
- Flutter: 3.7.12-ohos-1.0.6; SDK: 5.0.0(12); IDE: DevEco Studio: 5.0.13.200; ROM: 5.1.0.120 SP3;
- Flutter: 3.35.8-ohos-0.0.1; SDK: 6.0.1(21); IDE: DevEco Studio: 6.0.1.260; ROM: 6.0.0.120 SP6;
- Flutter: 3.27.5-ohos-1.0.3(本仓库主要适配目标);
flutter build hap --debug与真机 Demo(压缩 / 缩略图 / 媒体信息)已验证;
3. API
"ohos Support"列为 yes 表示 ohos 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Input | Output | ohos Support |
|---|---|---|---|---|---|
| getByteThumbnail | 获取视频文件缩略图,返回缩略图byte数据 | function | String path, {int quality = 100,int position = -1} | Future<Uint8List?> | yes |
| getFileThumbnail | 获取视频文件缩略图,返回缩略图的路径 | function | String path, {int quality = 100,int position = -1} | Future | yes |
| getMediaInfo | 获取视频文件的媒体信息 | function | String path | Future | yes |
| deleteAllCache | 删除所有缓存文件 | function | / | Future<bool?> | yes |
| cancelCompression | 停止当前正在执行的压缩任务 | function | / | Future | no |
| compressVideo | 压缩视频文件 | function | String path, {VideoQuality quality = VideoQuality.DefaultQuality, bool deleteOrigin = false, int? startTime, int? duration, bool? includeAudio, int frameRate = 30} |
Future<MediaInfo?> | yes |
| setLogLevel | 动态设置日志等级 | function | int logLevel | Future | no |
4. 属性
"ohos Support"列为 yes 表示 ohos 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
Parameters
| Name | Description | Type | ohos Support |
|---|---|---|---|
| path | 视频文件路径 | String | yes |
| quality | 设置输出缩略图的图片质量 | int | yes |
| position | 缩略图截取时间点,单位为 毫秒(ms)(与 Dart API 一致) | int | yes |
| frameRate | 压缩视频帧率 | int | yes |
| includeAudio | 压缩是否保留音频 | bool? | yes |
| duration | 压缩时长,鸿蒙侧单位为 秒(s);不传或 -1 表示到结尾 | int? | yes |
| startTime | 压缩起始时间,鸿蒙侧单位为 秒(s) | int? | yes |
| deleteOrigin | 压缩成功后是否删除原文件 | bool | yes |
| quality (压缩) | 压缩画质 VideoQuality 枚举 |
VideoQuality | yes |
| logLevel | 日志等级(鸿蒙端未实现,调用无效果) | int | no |
5. 遗留问题
- ohos 端视频裁剪失败,返回缩略图byte文件类型错误: issue#25
6. 其他
- FFmpeg
- ffmpeg-kit
- pr20提交更换了底层实现视频裁剪的功能,采用FFmpeg和ffmpeg-kit,相比之前的mp4parser拥有更完成的功能,如果遇到裁剪后的视频无法播放,可以在issue中提出,也可以自行根据tpc_c_cplusplus进行功能的拓展
7. 开源协议
本项目基于 MIT License ,请自由地享受和参与开源。