1.h264文件基本功能

NAL 头 0x00 0x00 0x00 0x01 
sps :nal+0x67开头 
pps :nal+0x68开头

I帧 0x65 开头

......

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 ;
}

01.mp4v2应用—mp4转h264的更多相关文章

  1. FFMPEG_avi转码到mp4(aac+h264)源码

    #include <ffmpeg/avcodec.h>#include <ffmpeg/avformat.h>#include <stdio.h>#include ...

  2. mp4格式的视频,编码方式mpeg4,转化为h264

    知识点:在使用vcastr3.swf播放器播放flv视频,(同时在html5页面,使用<video>标签时),发现某些MP4格式的代码不能播放 原因:vcastr3.swf和video,不 ...

  3. 树莓派环境下使用python将h264格式的视频转为mp4

    个人博客 地址:https://www.wenhaofan.com/a/20190430144809 下载安装MP4Box 命令行下执行以下指令安装MP4Box   sudo apt-get inst ...

  4. ffmpeg 编程常用 pcm 转 aac aac 转 pcm mp4 h264解码

    ffmpeg 是现在开源的全能编解码器,基本上全格式都支持,纯 c 语言作成,相对比其它的 VLC ,GStreamer glib2 写的,开发更简单些,文档很棒,就是 examples 比较少. 常 ...

  5. mp4v2 基本知识

    mp4v2 和mp4的一些基础知识 由于项目需要做mp4文件的合成(264+aac)和mp4文件的解析: MP4文件本身就是一个容器,对于视频来说就是把不同的内容放按照mp4的规则存放而已: 如果完全 ...

  6. MP4v2 基本使用(二)

    MP4转H264文件 本文最目标是基本的MP4v2接口的使用,并且实现一个简单的提取mp4文件里面264流的功能: 1.264文件基本知识 下看看H264文件的基本常识 NAL 头 0x00 0x00 ...

  7. 使用librtmp进行H264与AAC直播

    libx264 版本是 128libfaac 版本是 1.28 1.帧的划分 1.1 H.264 帧 对于 H.264 而言每帧的界定符为 00 00 00 01 或者 00 00 01. 比如下面的 ...

  8. rtmp 推送h264 + aac 的数据

    相关源码下载: http://download.csdn.net/detail/keepingstudying/8340431 需要libfaac,librtmp 的支持, 1.帧的划分 1.1 H. ...

  9. chrome mp4格式支持问题

    经过一些搜索得知,其实根本的问题是虽然大家都是.mp4后缀的文件,但是编码方式不同,而video标签的标准是用H.264方式编码视频的MP4文件(当然video标签还可以播放WebM和OGG格式的文件 ...

随机推荐

  1. 第八篇:ORM框架SQLAlchemy 了解知识

    一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取 ...

  2. JS大小转化B KB MB GB的转化方法

    function conver(limit){ var size = ""; ){ //如果小于0.1KB转化成B size = limit.toFixed() + "B ...

  3. Linux下启动、停止xampp命令

    启动xampp: /opt/lampp/./lampp start 停止xampp: /opt/lampp/./lampp stop 卸载xampp: rm -rf /opt/lampp

  4. php下关于Cannot use a scalar value as an array的解决办法

    今天在测试php程序的时候,出现了一个错误提示:Cannot use a scalar value as an array,这个错误提示前几天也出过,当时好像稍微调了一下就好了,也没深究,今天却又出现 ...

  5. Head First Python (一)

    建立一个数组: cast = ["Cleese","Palin","Jones","Idle"] 列出数组有多少数据项: ...

  6. 【java】实体类中 Set<对象> 按照对象的某个字段对set排序

    Java利用hibernate进行一对多查询时,把另一张表作为一个属性存进这张表的字段中,返回的类型是set类型,要对返回的set类型进行排序 user表 package onlyfun.caterp ...

  7. Maya建模命令

    Surface-Loft(放样)在两条曲线中间生成曲面Section Radius 改变圆环的圆环半径Edit Mesh- Merge 点连结挤压 keep face together(整体挤压),若 ...

  8. CQRS之旅——旅程3(订单和注册限界上下文)

    旅程3:订单和注册限界上下文 CQRS之旅的第一站 "寓言家和鳄鱼是一样的,只是名字不同" --约翰·劳森 描述: 订单和注册上下文有一部分职责在会议预订的过程中,在此上下文中,一 ...

  9. PAT1038(两个运行超时 未解决

    # include<iostream> # include<algorithm> using namespace std; int jishu(int a[],int N,in ...

  10. java BigDecimal工具类

    package com.core.calculate; import java.math.BigDecimal; import java.text.DecimalFormat; /** * Creat ...