简介
一个读取、写入操作音视频文件编辑的工具。
编译运行
1、通过IDE工具下载依赖SDK,Tools->SDK Manager->Openharmony SDK 把native选项勾上下载,API版本>=10
2、开发板选择RK3568,ROM下载地址. 选择开发板类型是rk3568,请使用最新的版本
3、下载源码
git clone https://gitee.com/openharmony-tpc/mp4parser.git --recurse-submodules
4、项目依赖FFmpeg库,关于FFmpeg的编译:FFmpeg源码基于版本号:n4.2.5. 请参考:OpenHarmony编译构建指导. 编译脚本参考详见目录:doc/
下载安装
ohpm install @ohos/mp4parser
OpenHarmony ohpm
环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包
使用说明
视频合成
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 视频合成
*/
private videoMerge() {
let getLocalDirPath = getContext(this).cacheDir+"/";
let that = this;
let filePathOne = getLocalDirPath + "qqq.mp4";
let filePathTwo = getLocalDirPath + "www.mp4";
let outMP4 = getLocalDirPath + "mergeout.mp4";
var callBack: ICallBack = {
callBackResult(code: number) {
that.btnText = "视频合成点击执行"
that.imageWidth = 0
that.imageHeight = 0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.videoMerge(filePathOne, filePathTwo, outMP4, callBack);
}
视频裁剪
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 视频裁剪
*/
private videoClip() {
let getLocalDirPath = getContext(this).cacheDir+"/";
let that=this;
let sTime = "00:00:10";
let eTime = "00:00:20";
let sourceMP4 = getLocalDirPath+"qqq.mp4";
let outMP4 = getLocalDirPath+"clipout.mp4";
var callBack: ICallBack = {
callBackResult(code: number) {
that.btnText="视频裁剪点击执行"
that.imageWidth=0
that.imageHeight=0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.videoClip(sTime, eTime, sourceMP4, outMP4, callBack);
}
音频合成
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 音频合成
*/
private audioMerge() {
let getLocalDirPath = getContext(this).cacheDir+"/";
let that = this;
let filePathOne = getLocalDirPath + "a.mp3";
let filePathTwo = getLocalDirPath + "b.mp3";
let outPath = getLocalDirPath + "mergeout.mp3";
var callBack: ICallBack = {
callBackResult(code: number) {
console.log("mp4parser-->audioMerge--->end");
that.btnText = "音频合成点击执行"
that.imageWidth = 0
that.imageHeight = 0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.audioMerge(filePathOne, filePathTwo, outPath, callBack);
}
音频裁剪
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 音频裁剪
*/
private audioClip() {
let getLocalDirPath = getContext(this).cacheDir+"/";
let that = this;
let sTime = "00:00:00";
let eTime = "00:00:10";
let sourcePath = getLocalDirPath + "a.mp3";
let outPath = getLocalDirPath + "clipout.mp3";
var callBack: ICallBack = {
callBackResult(code: number) {
that.btnText = "音频裁剪点击执行"
that.imageWidth = 0
that.imageHeight = 0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.audioClip(sTime, eTime, sourcePath, outPath, callBack);
}
视频批量合成
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 视频批量合成
*/
private videoMultMerge() {
let that = this;
let getLocalDirPath = getContext(this).cacheDir+"/";
let filePath = getLocalDirPath + "mergeList.txt";
let outMP4 = getLocalDirPath + "mergeout3.mp4";
var callBack: ICallBack = {
callBackResult(code: number) {
that.btnText2 = "视频合成点击执行"
that.imageWidth = 0
that.imageHeight = 0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.videoMultMerge(filePath, outMP4, callBack);
}
音频批量合成
import {MP4Parser} from "@ohos/mp4parser";
import {ICallBack} from "@ohos/mp4parser";
/**
* 音频批量合成
*/
private audioMultMerge() {
let getLocalDirPath = getContext(this).cacheDir+"/";
let that = this;
let filePath = getLocalDirPath + "mergewavList.txt";
let outPath = getLocalDirPath + "mergeout3.wav";
var callBack: ICallBack = {
callBackResult(code: number) {
that.btnText2 = "音频合成点击执行"
that.imageWidth = 0
that.imageHeight = 0
if (code == 0) {
AlertDialog.show({ message: '执行成功' })
}
else {
AlertDialog.show({ message: '执行失败' })
}
}
}
MP4Parser.audioMultMerge(filePath, outPath, callBack);
}
视频取帧
import {ICallBack, IFrameCallBack, MP4Parser} from "@ohos/mp4parser";
private getFrameAtTimeRang() {
let getLocalDirPath = getContext(this).cacheDir + "/";
let sourceMP4 = getLocalDirPath + "www.mp4";
let that = this;
var callBack: ICallBack = {
callBackResult(code: number) {
if (code == 0) {
var frameCallBack: IFrameCallBack = {
async callBackResult(data: ArrayBuffer, timeUs: number) {
const imageSource = image.createImageSource(data)
that.imagePixelMap = await imageSource.createPixelMap()
}
}
MP4Parser.getFrameAtTimeRang("1000000", "9000000", MP4Parser.OPTION_CLOSEST, frameCallBack);
}
}
}
MP4Parser.setDataSource(sourceMP4, callBack);
}
调用FFmpeg指令
let context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext()
let getLocalDirPath = context.cacheDir + "/";
let sTime = "00:00:01";
let eTime = "00:00:02";
let sourceMP4 = getLocalDirPath + "testvideo.mp4";
let outMP4 = getLocalDirPath + "out.mp4";
let callBack: ICallBack = {
callBackResult(code: number) {
expect(0).assertEqual(code)
done()
}
}
MP4Parser.ffmpegCmd("ffmpeg -y -i " + sourceMP4 + " -ss " + sTime + " -c copy -to " + eTime + " " + outMP4, callBack)
接口说明
import {MP4Parser} from "@ohos/mp4parser";
- 视频合成
MP4Parser.videoMerge()
- 视频裁剪
MP4Parser.videoClip()
- 批量视频合成
MP4Parser.videoMultMerge()
- 音频合成
MP4Parser.audioMerge()
- 音频裁剪
MP4Parser.audioClip()
- 音频批量合成
MP4Parser.audioMultMerge()
- 设置视频源
MP4Parser.setDataSource()
- 视频取帧
MP4Parser.getFrameAtTimeRang()
- 停止取帧
MP4Parser.stopGetFrame()
- 调用FFmpeg指令
MP4Parser.ffmpegCmd()
约束与限制
在下述版本验证通过:
DevEco Studio版本: 4.0 Release(4.0.3.413), SDK: (4.0.10.3)
DevEco Studio 版本: 4.1 Canary(4.1.3.317),OpenHarmony SDK: API11 (4.1.0.36)
目录结构
|---- mp4parser
| |---- entry # 示例代码文件夹
| |---- library # mp4parser库文件夹
| |---- MP4Parser.ets # 对外接口
| |---- README.MD # 安装使用方法
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
《鸿蒙开发学习手册》:
如何快速入门:https://qr21.cn/FV7h05
- 基本概念
- 构建第一个ArkTS应用
- ……
开发基础知识:https://qr21.cn/FV7h05
- 应用基础知识
- 配置文件
- 应用数据管理
- 应用安全管理
- 应用隐私保护
- 三方应用调用管控机制
- 资源分类与访问
- 学习ArkTS语言
- ……
基于ArkTS 开发:https://qr21.cn/FV7h05
- Ability开发
- UI开发
- 公共事件与通知
- 窗口管理
- 媒体
- 安全
- 网络与链接
- 电话服务
- 数据管理
- 后台任务(Background Task)管理
- 设备管理
- 设备使用信息统计
- DFX
- 国际化开发
- 折叠屏系列
- ……
鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH
鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向