#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "gh_rtsp2mp4.h"
#define RTSP "rtsp://admin:quantum7@192.168.1.144"
//经过实验,这个值最好
#define PTS_VALUE 4500
#define MAX_FRAMES 300
static bool g_RunningFlag = true;
static int rtsp2mp4(const char* pInputFileName, const char* pOutputFileName)
{
AVOutputFormat *ofmt = NULL;
//Input AVFormatContext and Output AVFormatContext
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
AVPacket pkt;
int ret=0, i=0;
int video_index = -1;
int frame_index = 0;
int dts = 0;
int pts = 0;
av_register_all();
//Network
avformat_network_init();
//Input
if ((ret = avformat_open_input(&ifmt_ctx, pInputFileName, 0, 0)) < 0)
{
printf("Could not open input file.");
goto end;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0)
{
printf("Failed to retrieve input stream information");
goto end;
}
for (i = 0; i<ifmt_ctx->nb_streams; i++)
{
if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
video_index = i;
break;
}
}
av_dump_format(ifmt_ctx, 0, pInputFileName, 0);
//Output
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, pOutputFileName); //RTMP
if (!ofmt_ctx)
{
printf("Could not create output context\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ofmt = ofmt_ctx->oformat;
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
//Create output AVStream according to input AVStream
AVStream *in_stream = ifmt_ctx->streams[i];
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
if (!out_stream)
{
printf("Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
goto end;
}
//Copy the settings of AVCodecContext
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0)
{
printf("Failed to copy context from input to output stream codec context\n");
goto end;
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
{
out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
//Dump Format------------------
av_dump_format(ofmt_ctx, 0, pOutputFileName, 1);
//Open output URL
if (!(ofmt->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx->pb, pOutputFileName, AVIO_FLAG_WRITE);
if (ret < 0)
{
printf("Could not open output URL '%s'", pOutputFileName);
goto end;
}
}
//Write file header
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0)
{
printf("Error occurred when opening output URL\n");
goto end;
}
#if USE_H264BSF
AVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
#endif
while (g_RunningFlag)
{
AVStream *in_stream, *out_stream;
//Get an AVPacket
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
{
break;
}
in_stream = ifmt_ctx->streams[pkt.stream_index];
out_stream = ofmt_ctx->streams[pkt.stream_index];
//Convert PTS/DTS
#if 1
if (pts == 0)
{
pts = pkt.pts;
dts = pkt.dts;
}
else
{
pkt.pts += pts;
pkt.dts += dts;
}
if (pkt.dts < pkt.pts)
{
pkt.dts = pkt.pts;
}
#else
pkt.pts += 4500*frame_index;
pkt.dts += 4500*frame_index;
#endif
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
if (pkt.stream_index == video_index)
{
//printf("Receive %8d video frames from RTSP\n", frame_index);
frame_index++;
#if USE_H264BSF
av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
#endif
}
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0 && frame_index > 3)
{
printf("Error muxing packet\n");
break;
}
av_free_packet(&pkt);
if (frame_index > MAX_FRAMES)
{
break;
}
}
#if USE_H264BSF
av_bitstream_filter_close(h264bsfc);
#endif
//Write file trailer
av_write_trailer(ofmt_ctx);
end:
avformat_close_input(&ifmt_ctx);
/* close output */
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
{
avio_close(ofmt_ctx->pb);
}
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF)
{
printf("Error occurred.\n");
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
rtsp2mp4(RTSP, "rtsp.mp4");
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
rtsp2h264.7z (5个子文件)
rtsp2h264
gh_rtsp2mp4.cpp 5KB
gh_rtsp2mp4.o 5KB
Makefile 606B
gh_rtsp2mp4.h 470B
gh_rtsp2mp4 14KB
共 5 条
- 1
资源评论
- 765873098348902022-02-10c++好后悔
- codeforge_wu2019-09-20是时间戳设置不对吗,本身录制的视频可以正常播放,1秒25帧。修改了摄像头一些配置后,不知道什么原因,录制下来的mp4文件播放非常缓慢,大概10倍吧,请问是什么原因柳鲲鹏2020-04-15目前已转换方向,这个问题无法回答。记得当时测试是正常的。
柳鲲鹏
- 粉丝: 5949
- 资源: 108
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功