移植mp4v2开源库,h264编码mp4
来源:互联网 发布:营销数据管理软件 编辑:程序博客网 时间:2024/06/03 09:08
1、mp4v2移植
step1、下载:https://launchpad.net/ubuntu/+source/mp4v2
step2、编译
简单配置参数:
./configure --host=arm-linux CXX=arm-hisiv100nptl-linux-g++ CC=arm-hisiv100nptl-linux-gcc --prefix=/home/mp4v2-2.0.0/Demo --enable-shared
减少库大小配置参数:
./configure --host=arm-linux CXX=arm-hisiv100nptl-linux-g++ CC=arm-hisiv100nptl-linux-gcc --prefix=/home/mp4v2-2.0.0/Demo --enable-shared --disable-option-checking --disable-debug --disable-fvisibility --disable-largefile --disable-util --disable-dependency-tracking --disable-libtool-lock
如果编译出来的库在调用 MP4AddH264VideoTrack 函数时发生崩溃现象,加上以下选项可以解决,具体原因我也不清楚。
--disable-optimize
make
make install
2、mp4v2 API简介
MP4FileHandle MP4Create (const char* fileName,uint32_t flags)
功能:创建MP4文件句柄。
返回:MP4文件句柄。
参数:fileName 要录制的MP4文件名;flags 创建文件类型,如果要创建普通文件用默认值0就可以,如要录制大于4G的MP4文件此处要设置MP4_CREATE_64BIT_DATA。
bool MP4SetTimeScale( MP4FileHandle hFile, uint32_t value )
功能:设置时间标度。
返回:成功返回true,失败返回false。
参数:hFile MP4文件句柄,value 要设置的值(每秒的时钟ticks数)。
MP4TrackId MP4AddH264VideoTrack(MP4FileHandle hFile,
uint32_t timeScale,
MP4Duration sampleDuration,
uint16_t width,
uint16_t height,
uint8_t AVCProfileIndication,
uint8_t profile_compat,
uint8_t AVCLevelIndication,
uint8_t sampleLenFieldSizeMinusOne)
功能:添加h264视频track。
返回:返回track id号。
参数:hFile MP4文件句柄,timeScale 视频每秒的ticks数(如90000),sampleDuration 设置为 MP4_INVALID_DURATION,width height 视频的宽高,AVCProfileIndication profile (baseline profile, main profile, etc. see),profile_compat compatible profile,AVCLevelIndication levels,sampleLenFieldSizeMinusOne 设置为3.
注意: AVCProfileIndication,profile_compat, AVCLevelIndication,这三个参数值是在h264流中得到的。
MP4TrackId MP4AddAudioTrack(
MP4FileHandle hFile,
uint32_t timeScale,
MP4Duration sampleDuration,
uint8_t audioType)
功能:添加音频(aac)track。
返回:返回track id号。
参数:hFile MP4句柄,timeScale音频每秒的ticks数(如16000),下面两参数设置为MP4_INVALID_DURATION和MP4_MPEG4_AUDIO_TYPE。
bool MP4SetTrackESConfiguration(
MP4FileHandle hFile,
MP4TrackId trackId,
const uint8_t* pConfig,
uint32_t configSize );
功能:设置音频解码信息(如果设置错误会导致没有声音)。
返回:成功返回true,失败返回false。
参数:hFile 文件句柄,trackId 音频的track id,pConfig 记录解码信息的二进制流,configSize 解码串的长度。
注意:mpeg4ip 使用faac进行aac音频编码的,在编码时可以调用相应的函数得到二进制串pConfig和长度configSize,但是如果aac不是用faac编码的,这是需要自己填充pConfig,可以参考faac的实现,下面是一个填充结构例子:
前五个字节为 AAC object types LOW 2
接着4个字节为 码率index 16000 8
接着4个字节为 channels 个数 1
应打印出的正确2进制形式为 00010 | 1000 | 0001 | 000
2 8 1
bool MP4WriteSample(
MP4FileHandle hFile,
MP4TrackId trackId,
const uint8_t* pBytes,
uint32_t numBytes,
MP4Duration duration DEFAULT(MP4_INVALID_DURATION),
MP4Duration renderingOffset DEFAULT(0),
bool isSyncSample DEFAULT(true) );
功能:写一帧视频数据或写一段音频数据。
返回:成功返回true,失败返回false。
参数:hFile 文件句柄,trackId 音频或视频的track id,pBytes为要写的数据流指针,numBytes为数据字节长度,duration为前一视频帧与当前视频帧之间的ticks数,或这是前一段音频数据和当前音频数据之间的ticks。isSyncSample 对视频来说是否为关键帧。
注意:1,duration这个参数是用来实现音视频同步用的,如果设置错了会造成音视频不同步,甚至会出现crash现象(一般出现在调用MP4Close是crash)。 2,对于视频流MP4WriteSample函数每次调用是录制前一帧数据,用当前帧的时间戳和前一帧的时间戳计算duration值,然后把当前帧保存下来用做下次调用MP4WriteSample时用,写音频数据一样。
void MP4AddH264SequenceParameterSet(
MP4FileHandle hFile,
MP4TrackId trackId,
const uint8_t* pSequence,
uint16_t sequenceLen );
和
void MP4AddH264PictureParameterSet(
MP4FileHandle hFile,
MP4TrackId trackId,
const uint8_t* pPict,
uint16_t pictLen );
功能:添加序列参数集,添加图像参数集。
参数:hFile 文件句柄,trackId 视频track id,pSequence和pPict为要写入的序列图像参数集的数据指针,sequenceLen和pictLen为串长度。
注意:当检测到序列参数集或图像参数集更新时要调用MP4AddH264SequenceParameterSet或MP4AddH264PictureParameterSet进行更新。
void MP4Close(
MP4FileHandle hFile,
uint32_t flags DEFAULT(0) );
功能:关闭以打开的MP4文件。
参数:hFile 文件句柄,flags 是否允许在关闭MP4文件前做一些额外的优化处理。
注意:在录制较小的MP4文件时可以把flags设置为默认值,如果录制较大的文件最好把flags设置为MP4_CLOSE_DO_NOT_COMPUTE_BITRATE否则调用MP4Close函数会用掉很长的时间。
3、h264编码mp4文件伪代码
- {
- rtp_s* p_rtp = (rtp_s*) arg;
- if (p_rtp == NULL)
- {
- printf("ERROR!\n");
- return;
- }
- MP4FileHandle file = MP4Create("test.mp4", 0);
- if (file == MP4_INVALID_FILE_HANDLE)
- {
- printf("open file fialed.\n");
- return;
- }
- MP4SetTimeScale(file, 90000);
- //添加h264 track
- MP4TrackId video = MP4AddH264VideoTrack(file, 90000, 90000 / video rate(25), width, height,
- sps[1], //sps[1] AVCProfileIndication
- sps[1], //sps[2] profile_compat
- sps[3]f, //sps[3] AVCLevelIndication
- 3); // 4 bytes length before each NAL unit
- if (video == MP4_INVALID_TRACK_ID)
- {
- printf("add video track failed.\n");
- return;
- }
- MP4SetVideoProfileLevel(file, 0x7F);
- //添加aac音频
- MP4TrackId audio = MP4AddAudioTrack(file, 48000, 1024, MP4_MPEG4_AUDIO_TYPE);
- if (video == MP4_INVALID_TRACK_ID)
- {
- printf("add audio track failed.\n");
- return;
- }
- MP4SetAudioProfileLevel(file, 0x2);
- int ncount = 0;
- while (1)
- {
- frame_t* pf = NULL; //frame
- pthread_mutex_lock(&p_rtp->mutex);
- pf = p_rtp->p_frame_header;
- if (pf != NULL)
- {
- if (pf->i_type == 1)//video
- {
- MP4WriteSample(file, video, pf->p_frame, pf->i_frame_size, MP4_INVALID_DURATION, 0, 1);
- }
- else if (pf->i_type == 2)//audio
- {
- MP4WriteSample(file, audio, pf->p_frame, pf->i_frame_size , MP4_INVALID_DURATION, 0, 1);
- }
- ncount++;
- //clear frame.
- p_rtp->i_buf_num--;
- p_rtp->p_frame_header = pf->p_next;
- if (p_rtp->i_buf_num <= 0)
- {
- p_rtp->p_frame_buf = p_rtp->p_frame_header;
- }
- free_frame(&pf);
- pf = NULL;
- if (ncount >= 1000)
- {
- break;
- }
- }
- else
- {
- //printf("BUFF EMPTY, p_rtp->i_buf_num:%d\n", p_rtp->i_buf_num);
- }
- pthread_mutex_unlock(&p_rtp->mutex);
- usleep(10000);
- }
- MP4Close(file);
- }
- 移植mp4v2开源库,h264编码mp4
- h264->mp4(mp4v2-android)
- mp4v2再学习 -- H264视频编码成MP4文件
- MP4V2 录制mp4(h264+aac)视频
- MP4V2 录制mp4(h264+aac)视频
- MP4V2 录制mp4(h264+aac)视频
- MP4V2 录制mp4(h264+aac)视频
- 使用 MP4v2 分离 MP4 中的 H264 码流
- MP4V2 录制mp4(h264+aac)视频
- MP4V2 录制mp4(h264+aac)视频
- Mp4v2封装H264+AAC为MP4
- Mp4v2封装H264+AAC为MP4
- 转supermanwg的 mpeg4ip的Mp4v2库移植到android系统上,实现h264封装到mp4的容器内
- mpeg4ip的Mp4v2库移植到android系统上,实现h264封装到mp4的容器内
- mpeg4ip的Mp4v2库移植到android系统上,实现h264封装到mp4的容器内
- mpeg4ip的Mp4v2库移植到android系统上,实现h264封装到mp4的容器内
- 7.使用directshow采集视音频并进行H264和ACC实时编码再实时用MP4V2封装成MP4
- x264+mp4v2编码YUV420为mp4
- el表达式,取集合长度
- 【转载】stm32的GPIO八种工作模式
- 一点思考
- Jenkins文件服务器搭建
- Vive Raycaster & Raycast Method
- 移植mp4v2开源库,h264编码mp4
- PL/SQL不支持DESC命令
- 随谈
- JavaScript学习记录(2)
- c的旅程---字符
- NPOI——判断工作表是否隐藏, 单元格是否合并
- 【转载】google搜索从入门到精通
- java生成excel
- "数据类型"读书小记