diff --git a/src/MediaFile/MP4Muxer.cpp b/src/MediaFile/MP4Muxer.cpp index afa78762..cedc7c4f 100644 --- a/src/MediaFile/MP4Muxer.cpp +++ b/src/MediaFile/MP4Muxer.cpp @@ -79,6 +79,14 @@ void MP4Muxer::onTrackFrame(const Frame::Ptr &frame) { return; } + if(frame->getTrackType() == TrackVideo){ + if(!_started && !frame->keyFrame()){ + //第一帧必须是I帧,防止花屏 + return; + } + _started = true; + } + int with_nalu_size ; switch (frame->getCodecId()){ case CodecH264: diff --git a/src/MediaFile/MP4Muxer.h b/src/MediaFile/MP4Muxer.h index a458b4ca..5b8d2f27 100644 --- a/src/MediaFile/MP4Muxer.h +++ b/src/MediaFile/MP4Muxer.h @@ -80,6 +80,7 @@ private: uint32_t start_pts = 0; }; map _codec_to_trackid; + bool _started = false; }; diff --git a/src/MediaFile/Mp4Maker.cpp b/src/MediaFile/Mp4Maker.cpp index a4ec0d89..3cf2a7ec 100644 --- a/src/MediaFile/Mp4Maker.cpp +++ b/src/MediaFile/Mp4Maker.cpp @@ -89,10 +89,8 @@ void Mp4Maker::createFile() { try { _muxer = std::make_shared(strFileTmp.data()); for(auto &track :_tracks){ - if(track){ - //添加track - _muxer->addTrack(track); - } + //添加track + _muxer->addTrack(track); } _strFileTmp = strFileTmp; _strFile = strFile;