MP4转H264文件

本文最目标是基本的MP4v2接口的使用,并且实现一个简单的提取mp4文件里面264流的功能;

1.264文件基本知识

下看看H264文件的基本常识 
NAL 头 0x00 0x00 0x00 0x01 
sps :nal+0x67开头 
pps :nal+0x68开头

I帧 0x65 开头 
P帧… 
具体的参考别人的解析 
http://blog.csdn.net/jefry_xdz/article/details/8461343

2.mp4v2提取264文件的代码

 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <mp4v2/mp4v2.h> unsigned char sps[],pps[];
int spslen = ,ppslen = ; int get264stream(MP4FileHandle oMp4File,int VTrackId,int totalFrame)
{
if(!oMp4File) return -;
char NAL[] = {0x00,0x00,0x00,0x01};
unsigned char *pData = NULL;
unsigned int nSize = ;
MP4Timestamp pStartTime;
MP4Duration pDuration;
MP4Duration pRenderingOffset;
bool pIsSyncSample = ; int nReadIndex = ;
FILE *pFile = NULL;
pFile = fopen("out.h264","wb"); while(nReadIndex < totalFrame)
{
nReadIndex ++;
//printf("nReadIndex:%d\n",nReadIndex);
MP4ReadSample(oMp4File,VTrackId,nReadIndex,&pData,&nSize,&pStartTime,&pDuration,&pRenderingOffset,&pIsSyncSample); //IDR֡ 帧,写入sps pps先
if(pIsSyncSample)
{
fwrite(NAL,,,pFile);
fwrite(sps,spslen,,pFile); fwrite(NAL,,,pFile);
fwrite(pps,ppslen,,pFile); }
//264frame
if(pData && nSize > )
{
//标准的264帧,前面几个字节就是frame的长度.
//需要替换为标准的264 nal 头.
pData[] = 0x00;
pData[] = 0x00;
pData[] = 0x00;
pData[] = 0x01;
fwrite(pData,nSize,,pFile);
} //如果传入MP4ReadSample的视频pData是null
// 它内部就会new 一个内存
//如果传入的是已知的内存区域,
//则需要保证空间bigger then max frames size.
free(pData);
pData = NULL;
}
fflush(pFile);
fclose(pFile); return ;
}
int openmp4file(char *sMp4file)
{
MP4FileHandle oMp4File;
int i; //unsigned int oStreamDuration;
unsigned int oFrameCount; oMp4File = MP4Read(sMp4file);
int videoindex = -,audioindex = -;
uint32_t numSamples;
//uint32_t timescale;
//uint64_t duration; if (!oMp4File)
{
printf("Read error....%s\r\n",sMp4file);
return -;
} MP4TrackId trackId = MP4_INVALID_TRACK_ID;
uint32_t numTracks = MP4GetNumberOfTracks(oMp4File,NULL,);
printf("numTracks:%d\n",numTracks); for (i = ; i < numTracks; i++)
{
trackId = MP4FindTrackId(oMp4File, i,NULL,);
const char* trackType = MP4GetTrackType(oMp4File, trackId);
if (MP4_IS_VIDEO_TRACK_TYPE(trackType))
{
//printf("[%s %d] trackId:%d\r\n",__FUNCTION__,__LINE__,trackId);
videoindex= trackId; //duration = MP4GetTrackDuration(oMp4File, trackId );
numSamples = MP4GetTrackNumberOfSamples(oMp4File, trackId);
//timescale = MP4GetTrackTimeScale(oMp4File, trackId);
//oStreamDuration = duration/(timescale/1000);
oFrameCount = numSamples; // read sps/pps
uint8_t **seqheader;
uint8_t **pictheader;
uint32_t *pictheadersize;
uint32_t *seqheadersize;
uint32_t ix;
MP4GetTrackH264SeqPictHeaders(oMp4File, trackId, &seqheader, &seqheadersize, &pictheader, &pictheadersize); for (ix = ; seqheadersize[ix] != ; ix++)
{
memcpy(sps, seqheader[ix], seqheadersize[ix]);
spslen = seqheadersize[ix];
free(seqheader[ix]);
}
free(seqheader);
free(seqheadersize); for (ix = ; pictheadersize[ix] != ; ix++)
{
memcpy(pps, pictheader[ix], pictheadersize[ix]);
ppslen = pictheadersize[ix];
free(pictheader[ix]);
}
          free(pictheader);
free(pictheadersize);
}
         else if (MP4_IS_AUDIO_TRACK_TYPE(trackType))
{
audioindex = trackId;
printf("audioindex:%d\n",audioindex);
}
} //解析完了mp4,主要是为了获取sps pps 还有video的trackID
if(videoindex >= )
get264stream(oMp4File,videoindex,oFrameCount); //需要mp4close 否则在嵌入式设备打开mp4上多了会内存泄露挂掉.
MP4Close(oMp4File,);
return ;
} int main(void)
{
openmp4file("test.mp4");
return ;
}

3.测试文件ffproe的信息如下.

测试的mp4文件和生成的264文件信息如下,用播放器播放可以正常显示图像.

MP4v2 基本使用(二)的更多相关文章

  1. 嵌入式 使用mp4v2将H264+AAC合成mp4文件

    录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...

  2. 使用mp4v2将H264+AAC合成mp4文件

    录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...

  3. 安装 yaml-cpp,MP4V2

    一.在ubuntu上安装yaml-cpp的过程: git clone https://github.com/jbeder/yaml-cpp.git进入yaml-cpp目录后,mkdir buildcd ...

  4. 嵌入式Linux下MP4视频录制库MP4V2移植和简单介绍

    **************************************************************************************************** ...

  5. 【小程序分享篇 二 】web在线踢人小程序,维持用户只能在一个台电脑持登录状态

    最近离职了, 突然记起来还一个小功能没做, 想想也挺简单,留下代码和思路给同事做个参考. 换工作心里挺忐忑, 对未来也充满了憧憬与担忧.(虽然已是老人, 换了N次工作了,但每次心里都和忐忑). 写写代 ...

  6. 前端开发中SEO的十二条总结

    一. 合理使用title, description, keywords二. 合理使用h1 - h6, h1标签的权重很高, 注意使用频率三. 列表代码使用ul, 重要文字使用strong标签四. 图片 ...

  7. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  8. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  9. 谈谈一些有趣的CSS题目(十二)-- 你该知道的字体 font-family

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

随机推荐

  1. vs附加调试 w3p进程没有名称

    解决: 把vs用管理员运行

  2. queue hardware os

    Computer Science An Overview 11th Edition Queues are often used as the underlying structure of a buf ...

  3. postgreSQL 自增需要使用序列

    postgreSQL 自增需要使用序列 1.使用SERIAL CREATE TABLE users ( id SERIAL4 primary key , name character varying, ...

  4. mysql ifnull判断为空设置默认值

    IFNULL(count,0) as count select IFNULL(count,0) as count from table_name 可以设置当某个字段为空的时候默认值.

  5. 【Pyton】【小甲鱼】魔法方法

    1.__init__ >>> class Rectangle: def __init__(self,x,y): self.x=x self.y=y def getPeri(self) ...

  6. 使用idea maven开发spring boot 分布式开发入门

    1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> & ...

  7. hiredis(Synchronous API)

    hiredis是一个小型的client端的c库.它只增加了最小对协议的支持,同时它用一个高级别的printf-alike API为了绑定各种redis命令.除了支持发送和接收命令,它还支持对流的解析. ...

  8. error.jsp错误页面跳转,统一异常处理

    常见web项目中会用倒计时然后跳转页面来处理异常 error.jsp关键代码: <script language="javascript" type="text/j ...

  9. Py之pandas:dataframe学习【转载】

    转自:https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm 1.数据框4特性 列是不同类型的数据元素. 每列 ...

  10. css定位浮动总结

    定位:定位在中间,放大缩小时也不会跑偏. position:absolute; top: 50%; left: 50%; margin: -270px 0 0 -455px; 解释:定位后,设百分比的 ...