修复生成mp4相关bug

This commit is contained in:
xiongziliang 2019-08-01 21:59:26 +08:00
parent 6d8db7d232
commit 6c0d89f417
3 changed files with 64 additions and 15 deletions

View File

@ -74,17 +74,44 @@ void MP4Muxer::onTrackFrame(const Frame::Ptr &frame) {
} }
auto it = _codec_to_trackid.find(frame->getCodecId()); auto it = _codec_to_trackid.find(frame->getCodecId());
if(it == _codec_to_trackid.end()){ if(it == _codec_to_trackid.end()){
//该Track不存在或初始化失败
return; return;
} }
int with_nalu_size ;
switch (frame->getCodecId()){
case CodecH264:
case CodecH265:
//我们输入264、265是没有头四个字节表明数据长度的
with_nalu_size = 0;
break;
default:
//aac或其他类型frame不用添加4个nalu_size的字节
with_nalu_size = 1;
break;
}
//mp4文件时间戳需要从0开始
auto &track_info = it->second;
if(!track_info.start_dts){
track_info.start_dts = frame->dts();
}
if(!track_info.start_pts){
track_info.start_pts = frame->pts();
}
//相对时间戳
int dts_inc = frame->dts() - track_info.start_dts;
int pts_inc = frame->pts() - track_info.start_pts;
mov_writer_write_l(_mov_writter.get(), mov_writer_write_l(_mov_writter.get(),
it->second, track_info.track_id,
frame->data() + frame->prefixSize(), frame->data() + frame->prefixSize(),
frame->size() - frame->prefixSize(), frame->size() - frame->prefixSize(),
frame->pts(), pts_inc >= 0 ? pts_inc : 0,
frame->dts(), dts_inc >= 0 ? dts_inc : 0,
frame->keyFrame() ? MOV_AV_FLAG_KEYFREAME : 0, frame->keyFrame() ? MOV_AV_FLAG_KEYFREAME : 0,
0); with_nalu_size);
} }
void MP4Muxer::onTrackReady(const Track::Ptr &track) { void MP4Muxer::onTrackReady(const Track::Ptr &track) {
@ -98,10 +125,16 @@ void MP4Muxer::onTrackReady(const Track::Ptr &track) {
auto track_id = mov_writer_add_audio(_mov_writter.get(), auto track_id = mov_writer_add_audio(_mov_writter.get(),
MOV_OBJECT_AAC, MOV_OBJECT_AAC,
aac_track->getAudioChannel(), aac_track->getAudioChannel(),
aac_track->getAudioSampleBit(), aac_track->getAudioSampleBit() * aac_track->getAudioChannel(),
aac_track->getAudioSampleRate(), aac_track->getAudioSampleRate(),
aac_track->getAacCfg().data(), 2); aac_track->getAacCfg().data(), 2);
_codec_to_trackid[track->getCodecId()] = track_id; if(track_id < 0){
WarnL << "添加AAC Track失败:" << track_id;
return;
}
track_info info;
info.track_id = track_id;
_codec_to_trackid[track->getCodecId()] = info;
} }
break; break;
case CodecH264: { case CodecH264: {
@ -129,7 +162,14 @@ void MP4Muxer::onTrackReady(const Track::Ptr &track) {
h264_track->getVideoHeight(), h264_track->getVideoHeight(),
extra_data, extra_data,
extra_data_size); extra_data_size);
_codec_to_trackid[track->getCodecId()] = track_id;
if(track_id < 0){
WarnL << "添加H264 Track失败:" << track_id;
return;
}
track_info info;
info.track_id = track_id;
_codec_to_trackid[track->getCodecId()] = info;
} }
break; break;
case CodecH265: { case CodecH265: {
@ -158,7 +198,13 @@ void MP4Muxer::onTrackReady(const Track::Ptr &track) {
h265_track->getVideoHeight(), h265_track->getVideoHeight(),
extra_data, extra_data,
extra_data_size); extra_data_size);
_codec_to_trackid[track->getCodecId()] = track_id; if(track_id < 0){
WarnL << "添加H265 Track失败:" << track_id;
return;
}
track_info info;
info.track_id = track_id;
_codec_to_trackid[track->getCodecId()] = info;
} }
break; break;
default: default:
@ -171,7 +217,7 @@ void MP4Muxer::onTrackReady(const Track::Ptr &track) {
MP4MuxerFile::MP4MuxerFile(const char *file) { MP4MuxerFile::MP4MuxerFile(const char *file) {
//创建文件 //创建文件
auto fp = File::createfile_file(file,"wb"); auto fp = File::createfile_file(file,"wb+");
if(!fp){ if(!fp){
throw std::runtime_error(string("打开文件失败:") + file); throw std::runtime_error(string("打开文件失败:") + file);
} }

View File

@ -74,7 +74,12 @@ protected:
*/ */
void onTrackFrame(const Frame::Ptr &frame) override; void onTrackFrame(const Frame::Ptr &frame) override;
private: private:
map<CodecId,int> _codec_to_trackid; struct track_info{
int track_id = -1;
uint32_t start_dts = 0;
uint32_t start_pts = 0;
};
map<CodecId,track_info> _codec_to_trackid;
}; };

View File

@ -57,9 +57,7 @@ Mp4Maker::Mp4Maker(const string& strPath,
const string &strVhost, const string &strVhost,
const string &strApp, const string &strApp,
const string &strStreamId) { const string &strStreamId) {
DebugL << strPath;
_strPath = strPath; _strPath = strPath;
/////record 业务逻辑////// /////record 业务逻辑//////
_info.strAppName = strApp; _info.strAppName = strApp;
_info.strStreamId = strStreamId; _info.strStreamId = strStreamId;