Bento4库使用(1):读取MP4文件视频和音频裸数据
input_filename为读取的MP4文件名。
AP4_ByteStream* input_stream = NULL;
result = AP4_FileByteStream::Create(input_filename,
AP4_FileByteStream::STREAM_MODE_READ,
input_stream);
AP4_File input_file(*input_stream, true);
AP4_Movie *movie = input_file->GetMovie();
AP4_List<AP4_Track>& tracks = movie ->GetTracks();
int index = 0;
AP4_SampleDescription *sample_des;
for (AP4_List<AP4_Track>::Item* track_item = tracks.FirstItem();
index<tracks.ItemCount();track_item = track_item->GetNext(),++index)
{
AP4_Track *track = track_item->GetData();
if (track->GetType() == AP4_Track::TYPE_VIDEO) //视频轨道处理
{
Width = (float)track->GetWidth()/65536.0;
Height = (float)track->GetHeight()/65536.0;
VideoSampleNum = track->GetSampleCount();
VideoTrackId = track->GetId();
sample_des = track->GetSampleDescription(0);
stream_type = sample_des->GetFormat();
const char *track_format = AP4_GetFormatName(stream_type); //获得视频编码格式
VideoType = track_format;
//此处可根据视频编码类型获取SPS、PPS、VPS等数据
VideoFrameRate = track->GetSampleCount()/((float)track->GetMediaDuration()/(float)track->GetMediaTimeScale());
VideoTimeScale = track->GetMovieTimeScale();
}
else if(track->GetType() == AP4_Track::TYPE_AUDIO ) //音频轨道
{
LocalAudioTrackId = track->GetId();
sample_des = track->GetSampleDescription(0);
LocalAudioSampleNum = track->GetSampleCount();
LocalAudioMaxTime = track->GetDurationMs();
LocalAudioTimeScale = track->GetMediaTimeScale();
}
}
根据上面获得的track id区分读取的是视频数据还是音频数据,sample存放的即帧数据,sampleId为序号:
AP4_Track *track = movie.GetTrack(TrackId);
AP4_Sample sample;
AP4_DataBuffer sample_data;
AP4_Result result = track->ReadSample(SampleId++, sample, sample_data);
上面读取的帧数据是直接从MP4中获取的,因此读取的音频数据若是aac格式,是不包含ADTS头的,若需要读取的数据可正常播放,需要自己为每一帧添加ADTS头。