mp4v2 库是一个专用于处理mp4容器的开源项目,其使用c++编写,并提供c语言接口。
下载mp4v2库的最新代码之后,使用标准linux configure 进行配置,以设置cross-compile、目录等,而后进行编译。具体过程整理以下:css
./configure --help
能够查看全部配置选项。例如:./configure --prefix=/home/xxx/mp4v2_install_dir --host=arm-hisiv500-linux --disable-debug
make ; make install
├── bin
│ ├── mp4art
│ ├── mp4chaps
│ ├── mp4extract
│ ├── mp4file
│ ├── mp4info
│ ├── mp4subtitle
│ ├── mp4tags
│ ├── mp4track
│ └── mp4trackdump
├── include
│ └── mp4v2
│ ├── chapter.h
│ ├── file.h
│ ├── file_prop.h
│ ├── general.h
│ ├── isma.h
│ ├── itmf_generic.h
│ ├── itmf_tags.h
│ ├── mp4v2.h
│ ├── platform.h
│ ├── project.h
│ ├── sample.h
│ ├── streaming.h
│ ├── track.h
│ └── track_prop.h
├── lib
│ ├── libmp4v2.a
│ ├── libmp4v2.la
│ ├── libmp4v2.so -> libmp4v2.so.2.0.0
│ ├── libmp4v2.so.2 -> libmp4v2.so.2.0.0
│ └── libmp4v2.so.2.0.0
└── share
└── man
└── man1
├── mp4art.1
├── mp4file.15r6
├── mp4subtitle.1
└── mp4track.1
其中,使用mp4v2有两种方式:使用编译完成的bin工具或者使用其API(库文件)html
这里使用更为灵活的API方式完成MP4封装程序。
编译完成后在lib目录能够获得静态库目标libmp4v2.a和动态库目标libmp4v2.so.2.0.0,以及指向动态库的两个软连接文件。
在include目录下能够获得mp4v2库的全部头文件,其中mp4v2.h是顶层include文件。使用mp4v2库时,只需包含mp4v2.h便可。linux
该示例只插入了视频码流,音频码流插入方法相似c++
#include "mp4v2/mp4v2.h"
-static -lmp4v2
或-lmp4v2
-lstdc++ -lm
MP4FileHandle hMP4File = MP4CreateEx(strDstFileName, 0, 1, 1, 0, 0, 0, 0);
if (hMP4File == MP4_INVALID_FILE_HANDLE) {
printf("open file fialed.\n");
return NULL;
}
MP4SetTimeScale(hMP4File, 90000);
MP4TrackId video = MP4AddH264VideoTrack(hMP4File, 90000, 90000 / 25, 3840, 2160,
0x64, //sps[1] AVCProfileIndication
0x00, //sps[2] profile_compat
0x1f, //sps[3] AVCLevelIndication
3); // 4 bytes length before each NAL unit
if (video == MP4_INVALID_TRACK_ID) {
printf("add video track fialed.\n");
return;
}
while(!stream_end()){
MP4WriteSample(hMP4File, video, pBuf, nBuf , MP4_INVALID_DURATION, 0, 1);
}
MP4Close(hMP4File, 0);
【mp4文件格式解析】 http://blog.sina.com.cn/s/blog_48f93b530100jz4b.html
【RTP2MP4】 https://github.com/ZhengfengRao/rtp2mp4git