简介

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

编译运行

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
  • 1.

4、项目依赖FFmpeg库,关于FFmpeg的编译: FFmpeg源码基于版本号:n4.2.5. 请参考: OpenHarmony编译构建指导. 编译脚本参考详见目录:doc/

下载安装

ohpm install @ohos/mp4parser
  • 1.

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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
视频裁剪
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
音频合成
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
音频裁剪
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
视频批量合成
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
音频批量合成
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
视频取帧
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);
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
调用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)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

接口说明

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)

目录结构

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

经常有很多小伙伴抱怨说:不知道学习鸿蒙开发哪些技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?

为了能够帮助到大家能够有规划的学习,这里特别整理了一套纯血版鸿蒙(HarmonyOS Next)全栈开发技术的学习路线,包含了鸿蒙开发必掌握的核心知识要点,内容有(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、WebGL、元服务、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony驱动开发、系统定制移植等等)鸿蒙(HarmonyOS NEXT)技术知识点。

OpenHarmony多媒体-mp4parser_mp4parser

《鸿蒙 (Harmony OS)开发学习手册》(共计892页)
如何快速入门?

1.基本概念
2.构建第一个ArkTS应用
3.……

OpenHarmony多媒体-mp4parser_移动开发_02

1.应用基础知识

2.配置文件

3.应用数据管理

4.应用安全管理

5.应用隐私保护

6.三方应用调用管控机制

7.资源分类与访问

8.学习ArkTS语言

9.……

OpenHarmony多媒体-mp4parser_移动开发_03

基于ArkTS 开发

1.Ability开发

2.UI开发

3.公共事件与通知

4.窗口管理

5.媒体

6.安全

7.网络与链接

8.电话服务

9.数据管理

10.后台任务(Background Task)管理

11.设备管理

12.设备使用信息统计

13.DFX

14.国际化开发

15.折叠屏系列

16.……

OpenHarmony多媒体-mp4parser_mp4parser_04

OpenHarmony多媒体-mp4parser_鸿蒙开发_05

OpenHarmony 开发环境搭建

OpenHarmony多媒体-mp4parser_openharmony_06

《OpenHarmony源码解析》

  • 搭建开发环境
  • Windows 开发环境的搭建
  • Ubuntu 开发环境搭建
  • Linux 与 Windows 之间的文件共享
  • ……
  • 系统架构分析
  • 构建子系统
  • 启动流程
  • 子系统
  • 分布式任务调度子系统
  • 分布式通信子系统
  • 驱动子系统
  • ……

OpenHarmony多媒体-mp4parser_多媒体_07

OpenHarmony 设备开发学习手册

OpenHarmony多媒体-mp4parser_多媒体_08

写在最后
  • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
  • 想要获取更多完整鸿蒙最新学习资源,请移步前往小编: gitee.com/MNxiaona/733GH

OpenHarmony多媒体-mp4parser_鸿蒙开发_09