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. js中如何把RGB颜色转换为16进制颜色

    将RGB颜色值转换为16进制颜色值,主要是将 R.G.B 值分别转换为对应的十六进制值,填入 #RRGGBB 中. 推荐在线颜色转换工具:http://www.ecjson.com/rgbhex/ 例 ...

  2. Thread 小总结

    目录 线程概述 线程的定义 线程的启动 线程的状态 线程的方法属性 1.线程概述 线程是一个程序的多个执行路径,执行调度的单元,依托于进程的存在.线不仅可以共享进程的内在,而且还拥有一个属于自己的内存 ...

  3. Girls and Boys-hdu 1068

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  5. 【luminate primordial】苏州之行

    测试了reader 07版 更主要的是第一次坐了高铁 还不错 路上看到下雨的时候都是水顺着玻璃平着流 好厉害的样子 6个人去的6个人回 今儿开会 老板不太满意 小随意 对我来说,收获感觉还是不小的,使 ...

  6. UVA:11297-Census(二维线段树)

    Census Time Limit: 8 sec Description This year, there have been many problems with population calcul ...

  7. Maven使用入门

    Maven使用POM文件管理项目资源,pom.xml文件位于项目根目录下,结构如下: <?xml version="1.0" encoding="UTF-8&quo ...

  8. va_list

    void Log( const TCHAR *pszFormat, ... ) { TCHAR buf[] ; va_list arglist ; try { _tcscpy_s ( buf, , _ ...

  9. loj2065 「SDOI2016」模式字符串

    ref不是太懂 #include <iostream> #include <cstring> #include <cstdio> using namespace s ...

  10. 如何将int转换为datetime?

    $timestamp = 1210003200; $datetime = date('Y-m-d H:i:s', $timestamp); echo "该时间戳代表的时间:", $ ...