Github

https://github.com/gongluck/FFmpeg4.0-study.git

#include <iostream>
using namespace std; extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/time.h"
}
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avcodec.lib") char av_error[AV_ERROR_MAX_STRING_SIZE] = { 0 };
#define av_err2str(errnum) \
av_make_error_string(av_error, AV_ERROR_MAX_STRING_SIZE, errnum) #define INFILE "in.flv"
#define RTMP "rtmp://192.168.140.128/live/test"
#define RTSP "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov" int file2rtmp()
{
int ret = 0;
//封装上下文
AVFormatContext* ictx = nullptr;
AVFormatContext* octx = nullptr;
const char* iurl = INFILE;
const char* ourl = RTMP;
int64_t starttime; ret = avformat_network_init();
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //打开文件,解封文件头
ret = avformat_open_input(&ictx, iurl, nullptr, nullptr);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
cerr << "open file " << iurl << " success." << endl; //获取音视频流信息,h264 flv
ret = avformat_find_stream_info(ictx, nullptr);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //打印媒体信息
av_dump_format(ictx, 0, iurl, 0); ////////////////////////////// //输出流
ret = avformat_alloc_output_context2(&octx, av_guess_format(nullptr, INFILE, nullptr), nullptr, ourl);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
cout << "octx create success." << endl; //配置输出流
for (int i = 0; i < ictx->nb_streams; ++i)
{
//创建流
AVStream* ostream = avformat_new_stream(octx, avcodec_find_encoder(ictx->streams[i]->codecpar->codec_id));
if (ostream == nullptr)
goto END;
//复制配置信息
ret = avcodec_parameters_copy(ostream->codecpar, ictx->streams[i]->codecpar);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
ostream->codecpar->codec_tag = 0;//标记不需要重新编解码
}
av_dump_format(octx, 0, ourl, 1); ////////////////////////////// //推流
ret = avio_open2(&octx->pb, ourl, AVIO_FLAG_WRITE, nullptr, nullptr);
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //写入头信息
ret = avformat_write_header(octx, nullptr);
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //推流每一帧数据
AVPacket pkt;
starttime = av_gettime();
while (av_read_frame(ictx, &pkt) == 0)
{
//计算转换pts dts
AVRational itime = ictx->streams[pkt.stream_index]->time_base;
AVRational otime = octx->streams[pkt.stream_index]->time_base;
pkt.pts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.pos = -1; if (ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
{
int64_t nowtime = av_gettime() - starttime;
int64_t dts = pkt.dts * av_q2d(octx->streams[pkt.stream_index]->time_base) * 1000 * 1000;
if(dts > nowtime)
/*av_usleep(dts- nowtime)*/;
} ret = av_interleaved_write_frame(octx, &pkt);
av_packet_unref(&pkt);
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
}
ret = av_write_trailer(octx);//写文件尾
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
} END:
if (ictx != nullptr)
avformat_close_input(&ictx);
if (octx != nullptr)
{
avio_close(octx->pb);
avformat_free_context(octx);
}
ret = avformat_network_deinit();
if (ret != 0)
cout << av_err2str(ret) << endl;
return 0;
} int rtsp2rtmp()
{
int ret = 0;
//封装上下文
AVFormatContext* ictx = nullptr;
AVFormatContext* octx = nullptr;
const char* iurl = RTSP;
const char* ourl = RTMP;
int64_t starttime; ret = avformat_network_init();
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //打开文件,解封文件头
ret = avformat_open_input(&ictx, iurl, nullptr, nullptr);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
cerr << "open file " << iurl << " success." << endl; //获取音视频流信息,h264 flv
ret = avformat_find_stream_info(ictx, nullptr);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //打印媒体信息
av_dump_format(ictx, 0, iurl, 0); ////////////////////////////// //输出流
ret = avformat_alloc_output_context2(&octx, av_guess_format(nullptr, INFILE, nullptr), nullptr, ourl);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
cerr << "octx create success." << endl; //配置输出流
for (int i = 0; i < ictx->nb_streams; ++i)
{
//创建流
AVStream* ostream = avformat_new_stream(octx, avcodec_find_encoder(ictx->streams[i]->codecpar->codec_id));
if (ostream == nullptr)
return -1;
//复制配置信息
ret = avcodec_parameters_copy(ostream->codecpar, ictx->streams[i]->codecpar);
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
}
ostream->codecpar->codec_tag = 0;//标记不需要重新编解码
}
av_dump_format(octx, 0, ourl, 1); ////////////////////////////// //推流
if (ret != 0)
{
cout << av_err2str(ret) << endl;
goto END;
} ret = avio_open(&octx->pb, ourl, AVIO_FLAG_WRITE);
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //写入头信息
ret = avformat_write_header(octx, nullptr);
if (ret < 0)
{
cout << av_err2str(ret) << endl;
goto END;
} //推流每一帧数据
AVPacket pkt;
starttime = av_gettime();
while (av_read_frame(ictx, &pkt) == 0)
{
//计算转换pts dts
AVRational itime = ictx->streams[pkt.stream_index]->time_base;
AVRational otime = octx->streams[pkt.stream_index]->time_base;
pkt.pts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.pos = -1; ret = av_interleaved_write_frame(octx, &pkt);
av_packet_unref(&pkt);
if (ret < 0)
cout << av_err2str(ret) << endl;//不用退出
} END:
if (ictx != nullptr)
avformat_close_input(&ictx);
if (octx != nullptr)
{
avio_close(octx->pb);
avformat_free_context(octx);
}
ret = avformat_network_deinit();
if (ret != 0)
cout << av_err2str(ret) << endl;
return 0;
} int main()
{
//file2rtmp();
rtsp2rtmp();
system("pause");
return 0;
}

FFmpeg4.0笔记:rtsp2rtmp的更多相关文章

  1. FFmpeg4.0笔记:file2rtmp

    Github: https://github.com/gongluck/FFmpeg4.0-study.git #include <iostream> using namespace st ...

  2. FFmpeg4.0笔记:封装ffmpeg的解封装功能类CDemux

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CDemux.h /*********************** ...

  3. FFmpeg4.0笔记:封装ffmpeg的视频帧转换功能类CSws

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSws.h /************************* ...

  4. FFmpeg4.0笔记:封装ffmpeg的音频重采样功能类CSwr

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSwr.h /************************* ...

  5. FFmpeg4.0笔记:封装ffmpeg的解码功能类CDecode

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CDecode.h /********************** ...

  6. FFmpeg4.0笔记:本地媒体文件解码、帧格式转换、重采样、编码、封装、转封装、avio、硬解码等例子

    Github https://github.com/gongluck/FFmpeg4.0-study/blob/master/official%20example/my_example.cpp #in ...

  7. FFmpeg4.0笔记:采集系统声音

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集系统声音 void test_systemsound() ...

  8. FFmpeg4.0笔记:采集桌面

    Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff // 采集桌面 void test_desktop() { boo ...

  9. FFmpeg4.0笔记:VS2019编译FFmpeg4.0源码

    0.下载TDM.msys和yasm 1.安装TDM-GCC-64 2.安装msys到TDM-GCC的安装目录中 3.将call "C:\Program Files (x86)\Microso ...

随机推荐

  1. DNSlog盲注

    前言 在渗透测试中,经常遇到如下情景: 1,sql盲注 2,blind型ssrf以及xxe 3,无回显命令执行漏洞 ... dnslog盲注原理 开放的DNSlog平台: http://ceye.io ...

  2. ES开发的一些坑(一)

    一.ES-Hadoop导数据的时候报"Could not write all entries"异常  ES-Hadoop是一个开源的数据导入项目,支持数据从hdfs,hive,sp ...

  3. 条件随机场_CRF

    无向图 举例:“Bob drank coffee at Starbucks” 标记方式1:(名词,动词,名词,介词,名词) 称为l 标记方式2:(名词,动词,动词,介词,名词) 挑选出一个最靠谱的: ...

  4. IOS初级:AFNetworking

    狗 日的,第三方框架真j8难搞 1.为什么NS_ASSUME_NONNULL_BEGIN在6.2报错,你他么的还挑IDE,你这是什么态度? 2.还有,你他么的自动给老子转json了,有问过我么? #i ...

  5. 企业IT资产管理功能大全

  6. kbmmw 中JSON 操作入门

    现在各种系统中JSON 用的越来越多.delphi 也自身支持JSON 处理. 今天简要说一下kbmmw 内部如何使用和操作JSON. kbmmw 中json的操作是以TkbmMWJSONStream ...

  7. SpringMVC作用域传值几种方式

    一.SpringMVC 作用域传值的几种方式 1  使用原生Servlet 1.1 在 HandlerMethod 参数中添加作用域对象 1.1.1 ServletContext不能在方法参数中获取, ...

  8. 2019.01.13 loj#6515. 贪玩蓝月(线段树分治+01背包)

    传送门 题意简述:有一个初始为空的双端队列,每次可以在队首和队尾插入或弹出一个二元组(wi,vi)(w_i,v_i)(wi​,vi​),支持询问从当前队列中选取若干个元素是的他们的和对 MODMODM ...

  9. matlab 设定坐标比例

    figure() u=-0.1:0.005:0.1; v=-0.1:0.005:0.1; [x,y]=meshgrid(u,v); z=sin(x-y)./abs(x)+abs(y); surf(x, ...

  10. mysql的myBatis,主键自增设置

    方法一: insert id="insert" parameterType="Person" useGeneratedKeys="true" ...