01.mp4v2应用—mp4转h264
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的更多相关文章
- FFMPEG_avi转码到mp4(aac+h264)源码
#include <ffmpeg/avcodec.h>#include <ffmpeg/avformat.h>#include <stdio.h>#include ...
- mp4格式的视频,编码方式mpeg4,转化为h264
知识点:在使用vcastr3.swf播放器播放flv视频,(同时在html5页面,使用<video>标签时),发现某些MP4格式的代码不能播放 原因:vcastr3.swf和video,不 ...
- 树莓派环境下使用python将h264格式的视频转为mp4
个人博客 地址:https://www.wenhaofan.com/a/20190430144809 下载安装MP4Box 命令行下执行以下指令安装MP4Box sudo apt-get inst ...
- ffmpeg 编程常用 pcm 转 aac aac 转 pcm mp4 h264解码
ffmpeg 是现在开源的全能编解码器,基本上全格式都支持,纯 c 语言作成,相对比其它的 VLC ,GStreamer glib2 写的,开发更简单些,文档很棒,就是 examples 比较少. 常 ...
- mp4v2 基本知识
mp4v2 和mp4的一些基础知识 由于项目需要做mp4文件的合成(264+aac)和mp4文件的解析: MP4文件本身就是一个容器,对于视频来说就是把不同的内容放按照mp4的规则存放而已: 如果完全 ...
- MP4v2 基本使用(二)
MP4转H264文件 本文最目标是基本的MP4v2接口的使用,并且实现一个简单的提取mp4文件里面264流的功能: 1.264文件基本知识 下看看H264文件的基本常识 NAL 头 0x00 0x00 ...
- 使用librtmp进行H264与AAC直播
libx264 版本是 128libfaac 版本是 1.28 1.帧的划分 1.1 H.264 帧 对于 H.264 而言每帧的界定符为 00 00 00 01 或者 00 00 01. 比如下面的 ...
- rtmp 推送h264 + aac 的数据
相关源码下载: http://download.csdn.net/detail/keepingstudying/8340431 需要libfaac,librtmp 的支持, 1.帧的划分 1.1 H. ...
- chrome mp4格式支持问题
经过一些搜索得知,其实根本的问题是虽然大家都是.mp4后缀的文件,但是编码方式不同,而video标签的标准是用H.264方式编码视频的MP4文件(当然video标签还可以播放WebM和OGG格式的文件 ...
随机推荐
- 10.VUE学习之使用lodash库减少watch对后台请求的压力
问题描述 使用watch监听库里word的值的变化,获取新值后,用oxios发送的ajax异步请求, 此时会多次发送请求,浪费服务器资料. 解决办法 使用lodash库里的_.debounce函数延缓 ...
- CMSIS-DAP仿真器_学习(转载)
先给大家普及一下,哈哈.CMSIS-DAP仿真器,是ARM官方做的开源仿真器,没有版权,自由制作.官方给的源代码,使用的是NXP的单片机LPC4320做的.这个源代码,只要你安装了KEIL5,就可以找 ...
- Codeforces Round #461 (Div. 2) B. Magic Forest
B. Magic Forest time limit per test 1 second memory limit per test 256 megabytes Problem Description ...
- Network of Schools POJ - 1236 (强联通)
一些学校连接到了一个计算机网络.网络中的学校间有如下约定:每个学校维护一个列表,当该学校收到软件或信息后将会转发给列表中的所有学校(也就是接收方列表).需要注意的是如果B学校在A学校的接收方列表中,A ...
- python基础——18(面向对象2+异常处理)
一.组合 自定义类的对象作为另一个类的属性. class Teacher: def __init__(self,name,age): self.name = name self.age = age t ...
- Spring---环境搭建与包介绍
jar包下载路径 首先需要下载Spring框架 spring-framework-5.0.0.RELEASE-dist,官方地址为https://repo.spring.io/release/org/ ...
- Java中为什么字段不能被重写
官方说法: 在一个类中,一个具有相同名称的字段隐藏了父类的父类的领域,即使他们的类型是不同的.在子类中,父类中的字段是不能用简单的名称引用.相反,该字段必须通过超级访问.一般来说,我们不建议隐藏字段, ...
- OpenStack之各组件介绍
OpenStack简介 OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于:帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云. ...
- 使用 Pinup,PinupManager 在 XNA 中创建贴图(十七)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- linux/mac下的配置自定义命令alias
linux/mac下的自定义命令alias,并保存别名使其永久生效(重启不会失效) 在做开发每次提交代码的命令都是一长串参数,不想去记,于是可以使用alias命令来解决这个问题:alias aComm ...