FFMPEG直播推流
所用到的变量
//输入流
AVForamtContext *ictx = NULL: //封装格式上下文
char *inUrl = " test.mp4 "; //输入的视频文件//输出流
AVForamtContext *octx = NULL: //创建输出流上下文
char *outUrl = " test.flv ";
av_register_all();
初始化所有的封装和解封装,flv,mp4,mov,,,,
avforamt_network_init();
初始化网络库。
avformat_open_input();
打开文件,解封文件头。一般h264是没有头的,他的文件信息一般都放在后面。
参数:
AVFormatContext **ps,封装格式上下文
const char *url, 文件名或者文件的绝对路径
AVInputFormat *fmt, 指定封装或者解封格式
AVDictionary **option,
用法:
int ret = avformat_open_input (&ictx, url, NULL, NULL)if (re != 0){char buf[1024 ] = {0};av_strerror(re, buf, sizeof(buf));cout<< buf<< endl;}
avformat_find_stream_info();
获取音视频流信息,h264,flv
使用:
int ret = avformat_find_stream_info(ictx,0);
if (re != 0){char buf[1024 ] = {0};av_strerror(re, buf, sizeof(buf));cout<< buf<< endl;}
av_dump_format();
打印关于输入或输出格式的详细信息,例如
*持续时间,比特率,流,容器,程序,元数据,侧数据,
*编解码器和时基。
av_dump_format(ictx, 0, inUrl, 0);
输出流:
avformat_alloc_output_context2();
为输出格式分配一个AVFormatContext。
avformat_free_context()可以用来释放上下文和所有由框架分配的内容。
int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat, const char *format_name, const char *filename);
3:封装格式名称
4:输出文件名称
int ret = avformat_alloc_output_context2(&octx, 0, "flv", "outUrl");
if (ret != 0){char buf[1024 ] = {0};av_strerror(re, buf, sizeof(buf));cout<< buf<< endl;}
配置输出流
遍历输入的AVstream
for (int i = 0; i < octx->nb_streams; i++)
{//创建输出流//向媒体文件中添加一个新的流AVStrema &out = avformat_new_stream(octx, ictx->stream[i] -> codec->codec);//将源AVCodecContext的设置复制到目标中AVCodecContext。//适用于mp4//accodec_copy_context(out->codec; ictx->stream[i] ->codec);avcodec_parameters_copy(out->codecpar; ictx->stream[i] ->codecpar)out->codec->codec_tag = 0;
}
rtmp推流
打开IO
avio_open(&octx->pb, outUrl, AVIO_FLAG_WRITE);
if(!octx->pb)
{return -1;
}
写入头信息
int re = avformat_write_hander(octx, 0,);
if(re<0)
{return -1;
}
推流每一帧数据
AVPacket pkt;
//获取当前时间戳
long long strarttime = av_gettime();
for{;;}
{int re = av_read_frame(ictx, &pkt);if(re != 0)break;cout<< pkt.pts <<" "<<flush;//计算转换pts, dtsAVRational itime = ictx->stream[pkt.stream_index]->time_base;AVRational otime = octx->stream[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){AVRational tb = ictx->streams[pkt.steeams_index]->time_base;//已经过去的时间long long now = av_gettime() - startTime;long long dts = 0;dts = pkt.dts * (1000 * 1000 * tb.num / tb.den);if(dts > now){av_usleep(dts - now);}re = av_interleaves_write_frame(octx,&pkt);}if(re<0){return -1;}//空间释放av_packet_unrcf(&pkt);
}
main.c
extern "C"
{#include "libavformat/avformat.h"
#include "libavutil/time.h"
}
#include <iostream>
using namespace std;
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")int XError(int errNum)
{char buf[1024] = { 0 };av_strerror(errNum, buf, sizeof(buf));cout << buf << endl;getchar();return -1;
}
static double r2d(AVRational r)
{return r.num == 0 || r.den == 0 ? 0. : (double)r.num / (double)r.den;
}int main(int argc, char *argv[])
{char *inUrl = "test.flv";char *outUrl = "rtmp://192.168.1.44/live";//初始化所有封装和解封装 flv mp4 mov mp3av_register_all();//初始化网络库avformat_network_init();////输入流 1 打开文件,解封装//输入封装上下文AVFormatContext *ictx = NULL;//打开文件,解封文件头int re = avformat_open_input(&ictx, inUrl, 0, 0);if (re != 0){return XError(re);}cout << "open file " << inUrl << " Success." << endl;//获取音频视频流信息 ,h264 flvre = avformat_find_stream_info(ictx, 0);if (re != 0){return XError(re);}av_dump_format(ictx, 0, inUrl, 0);//////输出流 //创建输出流上下文AVFormatContext *octx = NULL;re = avformat_alloc_output_context2(&octx, 0, "flv", outUrl);if (!octx){return XError(re);}cout << "octx create success!" << endl;//配置输出流//遍历输入的AVStreamfor (int i = 0; i < ictx->nb_streams; i++){//创建输出流AVStream *out = avformat_new_stream(octx, ictx->streams[i]->codec->codec);if (!out){return XError(0);}//复制配置信息,同于MP4//re = avcodec_copy_context(out->codec, ictx->streams[i]->codec);re = avcodec_parameters_copy(out->codecpar, ictx->streams[i]->codecpar);out->codec->codec_tag = 0;}av_dump_format(octx, 0, outUrl, 1);////rtmp推流//打开iore = avio_open(&octx->pb, outUrl, AVIO_FLAG_WRITE);if (!octx->pb){return XError(re);}//写入头信息re = avformat_write_header(octx, 0);if (re < 0){return XError(re);}cout << "avformat_write_header " << re << endl;AVPacket pkt;long long startTime = av_gettime();for (;;){re = av_read_frame(ictx, &pkt);if (re != 0){break;}cout << pkt.pts << " " << flush;//计算转换pts dtsAVRational 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_NEAR_INF));pkt.dts = av_rescale_q_rnd(pkt.pts, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));pkt.duration = av_rescale_q_rnd(pkt.duration, itime, otime, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_NEAR_INF));pkt.pos = -1;//视频帧推送速度if (ictx->streams[pkt.stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO){AVRational tb = ictx->streams[pkt.stream_index]->time_base;//已经过去的时间long long now = av_gettime() - startTime;long long dts = 0;dts = pkt.dts * (1000 * 1000 * r2d(tb));if (dts > now)av_usleep(dts - now);}re = av_interleaved_write_frame(octx, &pkt);if (re<0){return XError(re);}}cout << "file to rtmp test" << endl;getchar();return 0;
}
FFMPEG直播推流相关推荐
- 使用ffmpeg直播推流总结
需求描述 最近在做一个项目,需要拿到PC摄像头.麦克风和录屏进行推流,使用的是win10系统 步骤 配置环境 ffmpeg环境: 官网下载,并配置好环境变量 录屏环境:下载screen-capture ...
- Linux----FFmpeg直播推流
FFmpeg直播推流 1.本地视频ffmpeg推流rtmp(nginx+rtmp) 1.在Linux Centos7上安装docker(安装过程可以参考CentOS Docker 安装:https:/ ...
- ffmpeg推流_明白了以下5点思路,你也能用Python实现直播推流效果(技术活)
今天为大家带来的内容是:明白了以下5点思路,你也能用Python实现直播推流效果(技术活) 本文内容主要介绍了Python实现直播推流效果,主要是通过opencv读取视频对视频分割为帧,本文通过实例代 ...
- C++编程FFMpeg实时美颜直播推流实战-基于ffmpeg,qt5,opencv视频课程-夏曹俊-专题视频课程...
C++编程FFMpeg实时美颜直播推流实战-基于ffmpeg,qt5,opencv视频课程-11788人已学习 课程介绍 C++编程FFMpeg实时美颜直播推流实战视频培训教程,本课程 ...
- 视频教程-C++编程FFMpeg(QT5+OpenCV)实战--实时美颜直播推流-C/C++
C++编程FFMpeg(QT5+OpenCV)实战--实时美颜直播推流 夏曹俊:南京捷帝科技有限公司创始人,南京大学计算机硕士毕业,有15年c++跨平台项目研发的经验,领导开发过大量的c++虚拟仿真, ...
- windows下ffmpeg+nginx-rtmp环境搭建及opencv+ffmpeg+python实现拉流和rtmp直播推流
由于工作需要最近在研究rtsp拉流与直播推流的问题,目前先在我本机上进行了实验,本博客记录学习的内容,包含windows下ffmpeg.nginx-rtmp环境搭建及opencv+ffmpeg+pyt ...
- 用ffmpeg录制小程序直播开发高清视频并实现直播推流
导读:用ffmpeg录制小程序直播开发高清视频并实现直播推流,本文用ffmpeg和 screen capture recorder工具实现用命令行方式录制小程序直播开发的高清视频,并将实时录制的高清视 ...
- 直播推流 ffmpeg 拉流二次转发 记录
因 前两天老大给我发了个任务,说让我做一个flask 服务器接口,接收请求,在请求参数中获取 直播拉流地址和 推流地址,然后调用 ffmpeg 到拉流地址上进行拉流 将拉到的流媒体数据推到另一个直播服 ...
- 使用ffmpeg做直播推流例子
1.视频加音频直播 ffmpeg -f dshow -i audio="麦克风阵列 (Realtek(R) Audio)" -acodec aac -f dshow -i vide ...
最新文章
- 配置伪静态(URL重写)
- python使用matplotlib可视化、移除可视化图像坐标轴的刻度线和标签( remove the default axis ticks and labels)
- Python中的包以及跨目录访问变量和函数(同级目录)
- 多线程读取同一个文件_前端进阶:多线程Web Workers的工作原理及使用场景
- .NET Core ORM 类库Petapoco中对分页Page添加Order By对查询的影响
- Spark 101:它是什么,它做什么以及为什么起作用
- 关于PHP SESSION
- 解决OpenCV4出现“/usr/bin/ld:cannot find -lcudart_static.../usr/bin/ld: cannot find -lx86_64-linux-gnu“问题
- 【转载】怎样成为优秀的电子工程师?另外求本文出处
- tableau破解方法_使用Tableau浏览Netflix内容的简单方法
- 顶岗实习阶段工作总结
- MP3免费音频文件下载
- ubuntu MeshLab安装
- nacos 适配人大金仓数据
- HTML CSS 布局
- js将HTML导出生成word文档
- android水果购物车,实现购物车功能
- 使用GO操作MongoDB
- 一部电影,一种人生 (会议分享表述用)
- android动画如何更新UI(ValueAnimator源码解析)