C++ MP4v2获取rtsp流并保存为MP4文件


C++ MP4v2获取rtsp流并保存为MP4文件,ffmpeg版本为4.0.1,其中的dll,lib,include在程序中已经提供,编译环境为vs2012
资源截图
代码片段和文件信息
#pragma once
#include “MP4Encoder.h“


#define DEFAULT_VIDEO_TRACK_NUM 3
#define DEFAULT_VIDEO_PROFILE_LEVEL 1
#define DEFAULT_AUDIO_PROFILE_LEVEL 2


MP4Encoder::MP4Encoder(void)
: m_hFile(MP4_INVALID_FILE_HANDLE)
 m_videoTrack(MP4_INVALID_TRACK_ID)
 m_audioTrack(MP4_INVALID_TRACK_ID)
{
}

MP4Encoder::~MP4Encoder(void)
{
}

int MP4Encoder::MP4CreateFile(const char *sFileNameint Movie_Time_Scale)
{
m_hFile = MP4Create(sFileName);
if (m_hFile == MP4_INVALID_FILE_HANDLE)
{
printf(“create mp4 file error!
“);
return -1;
}
if (!MP4SetTimeScale(m_hFile Movie_Time_Scale))
{
printf(“set time Scale error!
“);
return -1;
}
return 0;
}

int  MP4Encoder::MP4AddH264Track(const uint8_t *sData int nSize
int nWidth int nHeight int nframeRate int Video_Time_Scale)
{
int sps pps;
for (sps = 0; sps < nSize;)
if (sData[sps++] == 0x00 && sData[sps++] == 0x00 && sData[sps++] == 0x00
&& sData[sps++] == 0x01)
break;
for (pps = sps; pps < nSize;)
if (sData[pps++] == 0x00 && sData[pps++] == 0x00 && sData[pps++] == 0x00
&& sData[pps++] == 0x01)
break;
if (sps >= nSize || pps >= nSize)
return -1;
m_videoTrack = MP4AddH264VideoTrack(m_hFile Video_Time_Scale
Video_Time_Scale / nframeRate nWidth nHeight
sData[sps + 1] sData[sps + 2] sData[sps + 3] DEFAULT_VIDEO_TRACK_NUM);//sps 后面的三个字节
if (MP4_INVALID_TRACK_ID == m_videoTrack)
{
printf(“add video track error!
“);
return -1;
}
MP4SetVideoProfileLevel(m_hFile DEFAULT_VIDEO_PROFILE_LEVEL);
MP4AddH264SequenceParameterSet(m_hFile m_videoTrack sData + spspps - sps - 4);//sps  内容  起始位置+长度  
MP4AddH264PictureParameterSet(m_hFile m_videoTrack sData + ppsnSize - pps);// pps 内容  起始位置+长度
return 0;
}

int   MP4Encoder::MP4AddAACTrack(const uint8_t *sData int nSizeint Audio_Time_Scale)
{
m_audioTrack = MP4AddAudioTrack(m_hFile Audio_Time_Scale1024 MP4_MPEG4_AUDIO_TYPE);
if (MP4_INVALID_TRACK_ID == m_audioTrack)
{
printf(“add audio track error!
“);
return -1;
}
MP4SetAudioProfileLevel(m_hFile DEFAULT_AUDIO_PROFILE_LEVEL);
if (!MP4SetTrackESConfiguration(m_hFile m_audioTrack sData nSize))
{
printf(“set track ESConfiguration error!
“);
return -1;
}

return 0;
}

int MP4Encoder::MP4WriteH264Data(uint8_t *sData int nSize)
{

bool result = false;
sData[0] = (nSize - 4) >> 24;
sData[1] = (nSize - 4) >> 16;
sData[2] = (nSize - 4) >> 8;
sData[3] = nSize - 4;

if(nSize<0||sData==NULL)
return -1;

if ((sData[4] & 0x1F) == 5)//判断I帧
result = MP4WriteSample(m_hFile m_videoTrack sData nSizeMP4_INVALID_DURATION0true);
else
result = MP4WriteSample(m_hFile m_videoTrack sData nSizeMP4_INVALID_DURATION0false);
if (!result)
{
printf(“write h264 frame error!
“);
return -1;
}
return 0;
}

int  MP4Encoder::MP4WriteAACData(const uint8_t *sData int nSize)
{
bool result = false;
if(nSize<0||sData==NULL)
ret

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-08-06 17:29  MP4Recorder
     目录           0  2019-08-06 16:45  MP4RecorderDebug
     文件    31476736  2018-07-25 16:07  MP4RecorderDebugavcodec-58.dll
     文件     5897216  2018-07-25 16:07  MP4RecorderDebugavformat-58.dll
     文件      671232  2018-07-25 16:07  MP4RecorderDebugavutil-56.dll
     目录           0  2019-08-06 16:45  MP4RecorderDebuginclude
     文件        7976  2015-02-13 01:38  MP4RecorderDebuginclude\_mingw.h
     文件        5721  2015-02-13 01:38  MP4RecorderDebugincludeinttypes.h
     目录           0  2019-08-06 16:45  MP4RecorderDebugincludelibavcodec
     文件        1207  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecac3_parser.h
     文件        1354  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecadts_parser.h
     文件      211684  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecavcodec.h
     文件        2570  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecavdct.h
     文件        3111  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecavfft.h
     文件        2853  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecd3d11va.h
     文件        4044  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecdirac.h
     文件        3715  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecdv_profile.h
     文件        2361  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecdxva2.h
     文件        1650  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecjni.h
     文件        2825  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecmediacodec.h
     文件        3763  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecqsv.h
     文件        2297  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecvaapi.h
     文件        5796  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecvdpau.h
     文件        4785  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecversion.h
     文件        4029  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecvideotoolbox.h
     文件        2285  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecvorbis_parser.h
     文件        6062  2018-07-25 16:07  MP4RecorderDebugincludelibavcodecxvmc.h
     目录           0  2019-08-06 16:45  MP4RecorderDebugincludelibavformat
     文件      117558  2018-07-25 16:07  MP4RecorderDebugincludelibavformatavformat.h
     文件       31557  2018-07-25 16:07  MP4RecorderDebugincludelibavformatavio.h
     文件        3781  2018-07-25 16:07  MP4RecorderDebugincludelibavformatversion.h
............此处省略283个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)