# mp4parser **Repository Path**: loss-fun/mp4parser ## Basic Information - **Project Name**: mp4parser - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 35 - **Created**: 2025-03-25 - **Last Updated**: 2025-03-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mp4parser ## Introduction > mp4parser is a tool for reading and writing audio and video files. ## How to Build 1. Download the dependency SDK through DevEco Studio. Specifically, on DevEco Studio, choose **Tools > SDK Manager > OpenHarmony SDK**, select **native**, and set the API version to 10 or later to download the SDK. 2. Select the RK3568 development board. The ROM download address of the development board is **https://ci.openharmony.cn/workbench/cicd/dailybuild/dailylist**. Choose the latest ROM version. 3. Run the following command to download the source code: ``` git clone https://gitee.com/openharmony-tpc/mp4parser.git --recurse-submodules ``` 4. This project depends on the FFmpeg library. For details about FFmpeg compilation, see [Source Code of FFmpeg n4.3.8](https://github.com/FFmpeg/FFmpeg). ## Configuring FFmpeg Dependencies 1. Enable the support of x86_64 for cross compilation. For details, see [adpater_architecture.md](https://gitee.com/openharmony-sig/tpc_c_cplusplus/blob/master/docs/adpater_architecture.md) 2. Generate the library file and header file by cross-compiling the [Source Code of FFmpeg n4.3.8](https://github.com/bilibili/FFmpeg/tags). ​ Before compilation, change the [FFmpeg](https://gitee.com/openharmony-sig/tpc_c_cplusplus/tree/master/thirdparty/FFmpeg) version number in the HPKBUILD file to n4.3.8. ``` pkgver=n6.0 // Change the version number. pkgver=n4.3.8 ``` 3. Download [FFmpeg n4.3.8](https://codeload.github.com/FFmpeg/FFmpeg/zip/refs/tags/n4.3.8), run the following command to obtain the SHA-512 value, and replace the content of the SHA512SUM file. ``` sha512num FFmpeg-n4.3.8.zip ``` 4. Add the **third_party** directory to the **cpp** directory and add the **libs** directory under **library** and copy the FFmpeg library generated after compilation to the **third_party** directory. ![img.png](img/img.png) ## How to Install ```shell ohpm install @ohos/mp4parser ``` OpenHarmony ohpm For details about the environment configuration, see [Installing the OpenHarmony HAR](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_har_usage.en.md). ## Configuring the x86 Emulator Running Your App/Service on an Emulator ## How to Use ### Merging Two Video Files ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Merge two video files. */ 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 = "Click to merge videos." that.imageWidth = 0 that.imageHeight = 0 if (code == 0) { AlertDialog.show ({message:'Execution succeeded'}) } else { AlertDialog.show ({message:'Execution failed'}) } } } MP4Parser.videoMerge(filePathOne, filePathTwo, outMP4, callBack); } ``` ### Clipping Videos ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Clip a video. */ 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="Click to clip the video." that.imageWidth=0 that.imageHeight=0 if (code == 0) { AlertDialog.show ({message:'Execution succeeded'}) } else { AlertDialog.show ({message:'Execution failed'}) } } } MP4Parser.videoClip(sTime, eTime, sourceMP4, outMP4, callBack); } ``` ### Merging Two Audio Files ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Merge two audio files. */ 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 = "Click to merge audio clips." that.imageWidth = 0 that.imageHeight = 0 if (code == 0) { AlertDialog.show ({message:'Execution succeeded'}) } else { AlertDialog.show ({message:'Execution failed'}) } } } MP4Parser.audioMerge(filePathOne, filePathTwo, outPath, callBack); } ``` ### Clipping Audio ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Clip an audio clip. */ 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 = "Click to clip the audio." that.imageWidth = 0 that.imageHeight = 0 if (code == 0) { AlertDialog.show ({message:'Execution succeeded'}) } else { AlertDialog.show ({message:'Execution failed'}) } } } MP4Parser.audioClip(sTime, eTime, sourcePath, outPath, callBack); } ``` ### Merging Multiple Video Files ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Merge multiple video files. */ 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 = "Click to merge videos." that.imageWidth = 0 that.imageHeight = 0 if (code == 0) { AlertDialog.show ({message:'Execution succeeded'}) } else { AlertDialog.show ({message:'Execution failed'}) } } } MP4Parser.videoMultMerge(filePath, outMP4, callBack); } ``` ### Merging Multiple Audio Files ``` import {MP4Parser} from "@ohos/mp4parser"; import {ICallBack} from "@ohos/mp4parser"; /** * Merge multiple audio files. */ 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 = "Click to merge audio clips." that.imageWidth = 0 that.imageHeight = 0 if (code == 0) { AlertDialog.show({message:'Execution succeeded'}) } else { AlertDialog.show({message:'Execution failed'}) } } } MP4Parser.audioMultMerge(filePath, outPath, callBack); } ``` ### Obtaining Frames Within a Specified Time Range ``` 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); } ``` ### Calling FFmpeg Commands ``` 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) ``` ## Available APIs `import {MP4Parser} from "@ohos/mp4parser";` 1. Merges two video files into a single file. `MP4Parser.videoMerge()` 2. Clips a video. `MP4Parser.videoClip()` 3. Merges multiple video files into a single file. `MP4Parser.videoMultMerge()` 4. Merges two audio files into a single file. `MP4Parser.audioMerge()` 5. Clips an audio clip. `MP4Parser.audioClip()` 6. Merges multiple audio files into a single one. `MP4Parser.audioMultMerge()` 7. Sets the video sources. `MP4Parser.setDataSource()` 8. Obtains frames within a certain time range. `MP4Parser.getFrameAtTimeRang()` 9. Stops frame extraction. `MP4Parser.stopGetFrame()` 10. Calls an FFmpeg command. `MP4Parser.ffmpegCmd()` ## Constraints This project has been verified in the following versions: 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: API 11 (4.1.0.36) DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release (5.0.0.66) ## Directory Structure ```` |---- mp4parser | |---- entry # Sample code | |---- library # mp4parser library | |---- MP4Parser.ets # External APIs | |---- README.md # Readme ```` ## How to Contribute If you find any problem when using mp4parser, submit an [issue](https://gitee.com/openharmony-tpc/mp4parser/issues) or a [PR](https://gitee.com/openharmony-tpc/mp4parser/pulls). ## License This project is licensed under [Apache License 2.0](https://gitee.com/openharmony-tpc/mp4parser/blob/master/LICENSE).