当前位置: 首页 > article >正文

【鸿蒙HarmonyOS Next实战开发】mp4parser库-音视频裁剪、合成、取帧等操作

mp4parser

简介

一个读取、写入操作音视频文件编辑的工具。

下载安装

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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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: $r('app.string.ok') })
        }
        else {
          AlertDialog.show({ message: $r('app.string.failed') })
        }
      }
    }
    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";

  1. 视频合成 MP4Parser.videoMerge()
  2. 视频裁剪 MP4Parser.videoClip()
  3. 批量视频合成 MP4Parser.videoMultMerge()
  4. 音频合成 MP4Parser.audioMerge()
  5. 音频裁剪 MP4Parser.audioClip()
  6. 音频批量合成 MP4Parser.audioMultMerge()
  7. 设置视频源 MP4Parser.setDataSource()
  8. 视频取帧 MP4Parser.getFrameAtTimeRang()
  9. 停止取帧 MP4Parser.stopGetFrame()
  10. 调用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)

目录结构

|---- mp4parser  
|     |---- entry  # 示例代码文件夹
|     |---- library  # mp4parser库文件夹
|           |---- MP4Parser.ets  # 对外接口
|     |---- README.MD  # 安装使用方法                    

http://www.kler.cn/a/541996.html

相关文章:

  • 分层耦合 - IOC详解
  • 【论文笔记】Are Self-Attentions Effective for Time Series Forecasting? (NeurIPS 2024)
  • Linux进阶——web服务器
  • 驱动开发系列36 - Linux Graphics 2D 绘制流程
  • PHP E-mail发送机制详解
  • macbook键盘进残渣,按键难回弹的简单处理方法
  • 【R语言】t检验
  • C# ASP.NET 介绍
  • Arduino 第十四章:led点阵
  • 渗透利器工具:Burp Suite 联动 XRAY 图形化工具.(主动扫描+被动扫描)
  • 1.【线性代数】——方程组的几何解释
  • 机器学习10-卷积和卷积核3
  • 12.项目结构
  • 亚远景-ASPICE标准在全球汽车行业的实践与展望
  • 除了重定向,还有其他方法处理未定义路由吗?
  • C# 上位机--变量
  • MongoDB进阶篇-索引
  • LabVIEW多电机CANopen同步
  • macbook2015升级最新MacOS 白苹果变黑苹果
  • 哈希表实现(C++实现)
  • 【橘子ES】Aggregations 聚合准备
  • 蓝桥杯备考:贪心算法简介
  • WPS计算机二级•文档的文本样式与编号
  • Unity-Mirror网络框架-从入门到精通之LagCompensation示例
  • C++ STL容器之vector的使用及复现
  • Word成功接入DeepSeek详细步骤