【鸿蒙HarmonyOS Next实战开辟】mp4parser库-音视频裁剪、合成、取帧等利用

[复制链接]
发表于 2025-11-2 09:21:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
mp4parser

简介

   一个读取、写入利用音视频文件编辑的工具。
  下载安装

  1. ohpm install @ohos/mp4parser
复制代码
OpenHarmony ohpm 环境设置等更多内容,请参考怎样安装 OpenHarmony ohpm 包
利用分析

视频合成

  1.   import {MP4Parser} from "@ohos/mp4parser";
  2.   import {ICallBack} from "@ohos/mp4parser";
  3.     /**
  4.      * 视频合成
  5.      */
  6.   private videoMerge() {
  7.     let getLocalDirPath = getContext(this).cacheDir+"/";
  8.     let that = this;
  9.     let filePathOne = getLocalDirPath + "qqq.mp4";
  10.     let filePathTwo = getLocalDirPath + "www.mp4";
  11.     let outMP4 = getLocalDirPath + "mergeout.mp4";
  12.     var callBack: ICallBack = {
  13.       callBackResult(code: number) {
  14.         that.btnText = "视频合成点击执行"
  15.         that.imageWidth = 0
  16.         that.imageHeight = 0
  17.         if (code == 0) {
  18.           AlertDialog.show({ message: $r('app.string.ok') })
  19.         }
  20.         else {
  21.           AlertDialog.show({ message: $r('app.string.failed') })
  22.         }
  23.       }
  24.     }
  25.     MP4Parser.videoMerge(filePathOne, filePathTwo, outMP4, callBack);
  26.   }
复制代码
视频裁剪

  1. import {MP4Parser} from "@ohos/mp4parser";
  2. import {ICallBack} from "@ohos/mp4parser";
  3.   /**
  4.    * 视频裁剪
  5.    */
  6.    private videoClip() {
  7.     let getLocalDirPath = getContext(this).cacheDir+"/";
  8.     let that=this;
  9.     let sTime = "00:00:10";
  10.     let eTime = "00:00:20";
  11.     let sourceMP4 = getLocalDirPath+"qqq.mp4";
  12.     let outMP4 = getLocalDirPath+"clipout.mp4";
  13.     var callBack: ICallBack = {
  14.       callBackResult(code: number) {
  15.         that.btnText="视频裁剪点击执行"
  16.         that.imageWidth=0
  17.         that.imageHeight=0
  18.         if (code == 0) {
  19.           AlertDialog.show({ message: $r('app.string.ok') })
  20.         }
  21.         else {
  22.           AlertDialog.show({ message: $r('app.string.failed') })
  23.         }
  24.       }
  25.     }
  26.     MP4Parser.videoClip(sTime, eTime, sourceMP4, outMP4, callBack);
  27.   }
复制代码
音频合成

  1. import {MP4Parser} from "@ohos/mp4parser";
  2. import {ICallBack} from "@ohos/mp4parser";
  3.     /**
  4.    * 音频合成
  5.    */
  6.    private audioMerge() {
  7.     let getLocalDirPath = getContext(this).cacheDir+"/";
  8.     let that = this;
  9.     let filePathOne = getLocalDirPath + "a.mp3";
  10.     let filePathTwo = getLocalDirPath + "b.mp3";
  11.     let outPath = getLocalDirPath + "mergeout.mp3";
  12.     var callBack: ICallBack = {
  13.       callBackResult(code: number) {
  14.         console.log("mp4parser-->audioMerge--->end");
  15.         that.btnText = "音频合成点击执行"
  16.         that.imageWidth = 0
  17.         that.imageHeight = 0
  18.         if (code == 0) {
  19.           AlertDialog.show({ message: $r('app.string.ok') })
  20.         }
  21.         else {
  22.           AlertDialog.show({ message: $r('app.string.failed') })
  23.         }
  24.       }
  25.     }
  26.     MP4Parser.audioMerge(filePathOne, filePathTwo, outPath, callBack);
  27.   }
复制代码
音频裁剪

  1. import {MP4Parser} from "@ohos/mp4parser";
  2. import {ICallBack} from "@ohos/mp4parser";
  3.    /**
  4.      * 音频裁剪
  5.      */
  6.    private audioClip() {
  7.     let getLocalDirPath = getContext(this).cacheDir+"/";
  8.     let that = this;
  9.     let sTime = "00:00:00";
  10.     let eTime = "00:00:10";
  11.     let sourcePath = getLocalDirPath + "a.mp3";
  12.     let outPath = getLocalDirPath + "clipout.mp3";
  13.     var callBack: ICallBack = {
  14.       callBackResult(code: number) {
  15.         that.btnText = "音频裁剪点击执行"
  16.         that.imageWidth = 0
  17.         that.imageHeight = 0
  18.         if (code == 0) {
  19.           AlertDialog.show({ message: $r('app.string.ok') })
  20.         }
  21.         else {
  22.           AlertDialog.show({ message: $r('app.string.failed') })
  23.         }
  24.       }
  25.     }
  26.     MP4Parser.audioClip(sTime, eTime, sourcePath, outPath, callBack);
  27.   }
复制代码
视频批量合成

  1. import {MP4Parser} from "@ohos/mp4parser";
  2. import {ICallBack} from "@ohos/mp4parser";
  3.   /**
  4.    * 视频批量合成
  5.    */
  6.    private videoMultMerge() {
  7.     let that = this;
  8.     let getLocalDirPath = getContext(this).cacheDir+"/";
  9.     let filePath = getLocalDirPath + "mergeList.txt";
  10.     let outMP4 = getLocalDirPath + "mergeout3.mp4";
  11.     var callBack: ICallBack = {
  12.       callBackResult(code: number) {
  13.         that.btnText2 = "视频合成点击执行"
  14.         that.imageWidth = 0
  15.         that.imageHeight = 0
  16.         if (code == 0) {
  17.           AlertDialog.show({ message: $r('app.string.ok') })
  18.         }
  19.         else {
  20.           AlertDialog.show({ message: $r('app.string.failed') })
  21.         }
  22.       }
  23.     }
  24.     MP4Parser.videoMultMerge(filePath, outMP4, callBack);
  25.   }
复制代码
音频批量合成

  1. import {MP4Parser} from "@ohos/mp4parser";
  2. import {ICallBack} from "@ohos/mp4parser";
  3.   /**
  4.    * 音频批量合成
  5.    */
  6.   private audioMultMerge() {
  7.     let getLocalDirPath = getContext(this).cacheDir+"/";
  8.     let that = this;
  9.     let filePath = getLocalDirPath + "mergewavList.txt";
  10.     let outPath = getLocalDirPath + "mergeout3.wav";
  11.     var callBack: ICallBack = {
  12.       callBackResult(code: number) {
  13.         that.btnText2 = "音频合成点击执行"
  14.         that.imageWidth = 0
  15.         that.imageHeight = 0
  16.         if (code == 0) {
  17.           AlertDialog.show({ message: $r('app.string.ok') })
  18.         }
  19.         else {
  20.           AlertDialog.show({ message: $r('app.string.failed') })
  21.         }
  22.       }
  23.     }
  24.     MP4Parser.audioMultMerge(filePath, outPath, callBack);
  25.   }
复制代码
视频取帧

  1. import {ICallBack, IFrameCallBack, MP4Parser} from "@ohos/mp4parser";
  2.   private getFrameAtTimeRang() {
  3.     let getLocalDirPath = getContext(this).cacheDir + "/";
  4.     let sourceMP4 = getLocalDirPath + "www.mp4";
  5.     let that = this;
  6.     var callBack: ICallBack = {
  7.       callBackResult(code: number) {
  8.         if (code == 0) {
  9.           var frameCallBack: IFrameCallBack = {
  10.             async callBackResult(data: ArrayBuffer, timeUs: number) {
  11.               const imageSource = image.createImageSource(data)
  12.               that.imagePixelMap = await imageSource.createPixelMap()
  13.             }
  14.           }
  15.           MP4Parser.getFrameAtTimeRang("1000000", "9000000", MP4Parser.OPTION_CLOSEST, frameCallBack);
  16.         }
  17.       }
  18.     }
  19.     MP4Parser.setDataSource(sourceMP4, callBack);
  20.   }
复制代码
调用FFmpeg指令

  1. let context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext()
  2.     let getLocalDirPath = context.cacheDir + "/";
  3.     let sTime = "00:00:01";
  4.     let eTime = "00:00:02";
  5.     let sourceMP4 = getLocalDirPath + "testvideo.mp4";
  6.     let outMP4 = getLocalDirPath + "out.mp4";
  7.     let callBack: ICallBack = {
  8.         callBackResult(code: number) {
  9.           expect(0).assertEqual(code)
  10.           done()
  11.         }
  12.     }
  13.     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) DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release (5.0.0.66)
目次结构

  1. |---- mp4parser  
  2. |     |---- entry  # 示例代码文件夹
  3. |     |---- library  # mp4parser库文件夹
  4. |           |---- MP4Parser.ets  # 对外接口
  5. |     |---- README.MD  # 安装使用方法                    
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表