RTSP视频推流方法汇总
1 基于rtsp-simple-server的视频推流
rtsp-simple-server是一个rtsp推流服务器,可以与gstreamer或ffmpeg结合进行推流。下载对应平台版本,直接运行即可。
1.1 使用gstreamer命令推流
1.1.1 RTSP视频推流:Camera
/usr/bin/gst-launch-1.0 v4l2src device=/dev/video0 ! videoconvert ! video/x-raw,width=640,height=480,framerate=30/1 ! timeoverlay ! videoconvert ! x264enc ! rtspclientsink location=rtsp://localhost:8554/mystream
1.1.2 RTSP视频推流:Camera简化
/usr/bin/gst-launch-1.0 v4l2src device=/dev/video0 ! timeoverlay ! videoconvert ! x264enc ! rtspclientsink location=rtsp://localhost:8554/mystream
1.1.3 RTSP视频推流:videotestsrc
/usr/bin/gst-launch-1.0 videotestsrc ! video/x-raw,width=1920,height=1080,framerate=30/1 ! timeoverlay ! videoconvert ! x264enc ! rtspclientsink location=rtsp://localhost:8554/mystream
1.2 使用opencv和ffmpeg推流(cuvid硬件编码)
std::string rtsp_server_url = "rtsp://localhost:8554/live/test_green";
std::stringstream command;
command << "ffmpeg ";
// infile options
command << "-y " // overwrite output files
<< "-an " // disable audio
<< "-hwaccel cuvid "
<< "-f rawvideo " // force format to rawvideo
<< "-vcodec rawvideo " // force video rawvideo ('copy' to copy stream)
<< "-pix_fmt bgr24 " // set pixel format to bgr24
<< "-s 1920x1080 " // set frame size (WxH or abbreviation)
<< "-r 30 "; // set frame rate (Hz value, fraction or abbreviation)
command << "-i - ";
// outfile options
command << "-c:v h264_nvenc " // nvenc Video encoder
<< "-pix_fmt yuv420p " // set pixel format to yuv420p
<< "-tune:v zerolatency "
<< "-preset fast " // set the h264_nvenc encoding preset to fast
<< "-f rtsp " // force format to flv for rtmp, rtsp for rtsp
<< rtsp_server_url;
cv::Mat frame;
// 在子进程中调用 ffmpeg 进行推流
FILE *fp = nullptr;
fp = popen(command.str().c_str(), "w");
int id = 0;
// 将 cv 读到的每一帧传入子进程
if (fp != nullptr)
{
while (1)
{
frame = cv::imread("./2016-04-14-09-25-42.jpg");
cv::String strFrameId;
id = (++id) % 10000;
QString idStr = QString("%1").arg(id);
strFrameId = idStr.toStdString();
cv::putText(frame, strFrameId, cv::Point(960,500), cv::HersheyFonts::FONT_ITALIC, 10, cv::Scalar(0,255,0), 3);
if(frame.empty())
{
continue;
}
std::cout << frame.size() << std::endl;
fwrite(frame.data, sizeof(char), frame.total() * frame.elemSize(), fp);
}
pclose(fp);
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
1.3 使用opencv和ffmpeg推流(x264软件解码)
std::string rtsp_server_url = "rtsp://localhost:8554/live/test_green";
std::stringstream command;
command << "ffmpeg ";
// infile options
command << "-y " // overwrite output files
<< "-an " // disable audio
<< "-f rawvideo " // force format to rawvideo
<< "-vcodec rawvideo " // force video rawvideo ('copy' to copy stream)
<< "-pix_fmt bgr24 " // set pixel format to bgr24
<< "-s 1920x1080 " // set frame size (WxH or abbreviation)
<< "-r 30 "; // set frame rate (Hz value, fraction or abbreviation)
command << "-i - ";
// outfile options
command << "-c:v libx264 " // Hyper fast Audio and Video encoder
<< "-pix_fmt yuv420p " // set pixel format to yuv420p
<< "-tune:v zerolatency "
<< "-preset ultrafast " // set the libx264 encoding preset to ultrafast
<< "-f rtsp " // force format to flv for rtmp, rtsp for rtsp
<< rtsp_server_url;
cv::Mat frame;
// 在子进程中调用 ffmpeg 进行推流
FILE *fp = nullptr;
fp = popen(command.str().c_str(), "w");
int id = 0;
// 将 cv 读到的每一帧传入子进程
if (fp != nullptr)
{
while (1)
{
frame = cv::imread("./2016-04-14-09-25-42.jpg");
cv::String strFrameId;
id = (++id) % 10000;
QString idStr = QString("%1").arg(id);
strFrameId = idStr.toStdString();
cv::putText(frame, strFrameId, cv::Point(960,500), cv::HersheyFonts::FONT_ITALIC, 10, cv::Scalar(0,255,0), 3);
if(frame.empty())
{
continue;
}
std::cout << frame.size() << std::endl;
fwrite(frame.data, sizeof(char), frame.total() * frame.elemSize(), fp);
}
pclose(fp);
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
1.4 使用opencv和gstreamer推流
cv::Mat frame;
cv::String gst_str = "appsrc ! videoconvert ! x264enc ! rtspclientsink location=rtsp://localhost:8554/live/test_red";
//cv::String gst_str = "appsrc ! videoconvert ! omxh264enc ! rtspclientsink location=rtsp://localhost:8554/live/test_red";//PC上没有omxh264enc插件,嵌入式平台有
cv::VideoWriter video_writer(gst_str,cv::CAP_GSTREAMER, 0, 30, cv::Size(1920,1080));
int id = 0;
// 将 cv 读到的每一帧传入子进程
while (1)
{
frame = cv::imread("./2016-04-14-09-25-42.jpg");
cv::String strFrameId;
id = (++id) % 10000;
QString idStr = QString("%1").arg(id);
strFrameId = idStr.toStdString();
cv::putText(frame, strFrameId, cv::Point(960,500), cv::HersheyFonts::FONT_ITALIC, 10, cv::Scalar(0,255,0), 3);
if(frame.empty())
{
continue;
}
std::cout << frame.size() << std::endl;
//
video_writer.write(frame);
}
return EXIT_SUCCESS;
2 opencv,x264,live555推流
2.1 原理
opencv将视频帧传递给x264进行编码,将输出的264数据流写入管道文件,管道文件放在live555的推流可执行文件live555MediaServer统一目录下。
管道文件为FIFO,写入(推流)和读出(拉流)需要同时进行,否则会阻塞。
2.2 示例代码
cv::Mat BasePic = cv::imread("./2016-04-14-09-25-42.jpg");
cv::Mat inputPic;
cv::resize(BasePic, BasePic, cv::Size(1920, 1080));
//使用YUV420编码
cv::cvtColor(BasePic, BasePic, cv::COLOR_BGR2YUV_I420);
//创建fifo
::unlink("/media/user/DATA/zhangchen/live555/live555-latest/live/mediaServer/demo.264");
::mkfifo("/media/user/DATA/zhangchen/live555/live555-latest/live/mediaServer/demo.264", O_CREAT | O_EXCL | 660);
//打开fifo
int fp_dst = ::open("/media/user/DATA/zhangchen/live555/live555-latest/live/mediaServer/demo.264", O_WRONLY);
if(fp_dst == NULL)
{
::close(fp_dst);
return;
}
//H264编码设置
int video_width = 1920;
int video_height = 1080;
int res = 0;
//压缩编码前的图像
x264_picture_t pic;
x264_picture_t pic_out;
//
x264_t *h;
int i_frame = 0;
int i_frame_size = 0;
//压缩编码后的码流数据
x264_nal_t *nal;
int i_nal;
x264_param_t param;
res = x264_param_default_preset(¶m, "veryfast", NULL);
param.i_bitdepth = 8;//编码位数
param.i_csp = X264_CSP_I420;//编码制式
param.rc.i_qp_min = 36;//编码质量:越小,视频质量损失越小
param.rc.i_qp_max = 40;
param.rc.i_qp_step = 2;
param.i_width = video_width;//分辨率
param.i_height = video_height;
param.i_fps_den = 1;//帧率
param.i_fps_num = 30;
param.i_keyint_max = 4;//设置IDR关键帧间隔
param.i_keyint_min = 1;
param.b_vfr_input = 0;//固定帧率
param.b_repeat_headers = 1;//在每个关键帧前放置SPS/PPS
param.b_annexb = 1;//在NAL单元前放置起始码
res = x264_param_apply_profile(¶m, "baseline");
res = x264_picture_alloc(&pic, param.i_csp, param.i_width, param.i_height);
//打开编码器
h = x264_encoder_open(¶m);
if(h == 0)
{
return;
}
//输出头信息
/*
i_frame_size = x264_encoder_headers(h, &nal, &i_nal);
if(i_frame_size < 0)
{
return;
}
else if(i_frame_size)
{
if(!::write(fp_dst, nal->p_payload, i_frame_size))
{
return;
}
}
*/
//对视频帧进行编码
unsigned int luma_size = video_width * video_height;
unsigned int chroma_size = luma_size / 4;
for(;;i_frame++)
{
//读取输入帧YUV420
inputPic = BasePic.clone();
//绘制帧号
int id = i_frame / 15;
cv::String strFrameId;
QString idStr = QString("%1").arg(id);
strFrameId = idStr.toStdString();
cv::putText(inputPic, strFrameId, cv::Point(960,500), cv::HersheyFonts::FONT_ITALIC, 10, cv::Scalar(255,0,0), 3);
//
memcpy(pic.img.plane[0], inputPic.data, luma_size);
memcpy(pic.img.plane[1], inputPic.data + luma_size, chroma_size);
memcpy(pic.img.plane[2], inputPic.data + luma_size + chroma_size, chroma_size);
//
pic.i_pts = i_frame;
i_frame_size = x264_encoder_encode(h, &nal, &i_nal, &pic, &pic_out);
if(i_frame_size < 0)
{
break;
}
else if(i_frame_size)
{
if(!::write(fp_dst, nal->p_payload, i_frame_size))
{
break;
}
}
if(i_frame == (30 * 600))
{
break;
}
}
/*
//Flush delayed frames
while(x264_encoder_delayed_frames(h))
{
i_frame_size = x264_encoder_encode(h, &nal, &i_nal, NULL, &pic_out);
if(i_frame_size < 0)
{
break;
}
else if(i_frame_size)
{
if(!::write(fp_dst, nal->p_payload, i_frame_size))
{
break;
}
}
}
*/
//关闭编码器
x264_encoder_close(h);
x264_picture_clean(&pic);
::close(fp_dst);
RTSP视频推流方法汇总相关推荐
- YouTube视频下载方法汇总
以下是一些下载 YouTube 视频的方法: 使用在线 YouTube 下载器:有一些在线视频下载器可帮助您从 YouTube 下载视频.您只需将视频链接复制并粘贴到下载器的指定区域,然后下载您所需的 ...
- 网页视频下载方法汇总
blob:https://...形式的video 1.浏览器切换到手机模式,重新查看媒体元素,即可正常看到媒体地址 m3u8媒体分片形式 有些情况是需要查看m3u8这样的媒体分片格式下载:(也是现在媒 ...
- 摄像头视频推流python_摄像头视频推流装置及方法与流程
本发明涉及视频推流技术领域,尤其涉及一种摄像头视频推流装置及方法. 背景技术: 在机器人领域,USB摄像头使用范围很广泛.USB摄像头安装于机器人本体,其采集的视频流需要推送到上位机或者云端. 对于l ...
- Android 内置RTSP/RTMP服务器,实现局域网内视频推流与播放
1. 背景 工作中有一个需求,在同一个局域网内, 需要将Android平板端(车机)上的摄像头上的画面,实时传输到手机上进行播放. 对于这个需求,我们想到了用RTSP/RTMP进行推流,然后在手机端拉 ...
- iOS开发之直播App流程介绍,直播资料收集汇总,视频推流,视频拉流,SMTP、RTMP、HLS、 PLPlayerKit
本文转自:http://blog.csdn.net/zhonggaorong/article/details/51483282,感谢 zhonggaorong的分享 (一) iOS 直播类APP开发流 ...
- 海康大华等网络摄像机监控视频RTSP/RTMP推流网页播放/直播无需插件低延迟解决方案研究
市面上常见监控视频推流方案简介 当前如果想要将监控视频在浏览器中播放,有几种常见的办法如下: 1.获取摄像头RTSP流,使用FFmpeg或者程序如JavaCV或者其他方式,将其推流成RTMP,通过服务 ...
- 【音视频开发系列】盘点音视频直播RTSP/RTMP推流一定会遇到的各种坑,教你快速解决
聊聊RTSP/RTMP推流那些坑 1.推流架构分析 2.推流缓存队列的设计 3.FFmpeg函数阻塞问题分析 [音视频开发系列]盘点音视频直播一定会遇到的各种坑,教你快速解决 更多精彩内容包括:C/C ...
- 【流媒体服务】本地视频推流、转码、拉流、直播综合应用(五):【1】VLC+FFMPEG+Nginx实现RTSP到RTMP网页直播
文章目录 前言 一.前几章节回顾 二.操作步骤 1.准备本地视频 2.VLC:获取RTSP视频流 3.FFMPEG:转码推流服务准备 4.Nginx:网站服务准备 5.FFMPEG推流:rtsp转rt ...
- 常见小票打印机打印故障及解决方法汇总视频教程-王西猛-专题视频课程
常见小票打印机打印故障及解决方法汇总视频教程-548人已学习 课程介绍 通过对"常见小票打印机故障及解决方法汇总视频教程"课程学习,达到以下目的:1.可以独立自主安 ...
最新文章
- BOM--location对象、history对象
- 浅谈万进制算法与大数定理的结合(高精度乘除法,阶乘)
- ElasticSearch基本插件head
- |洛谷|DFS|P1101 单词方阵
- text 热敏打印机_只要有想象力,打印机都能做游戏
- 多模光纤收发器的基本参数及主要特点
- Elasticsearch集群知识笔记
- php 将前端网页输出成unicdoe编码
- vitualbox的一个问题总结
- 数据挖掘实践(金融风控)——task02:数据分析
- paip. http 405 的解决..
- linux系统开启ssh端口号
- jQuery按住滑块拖动验证插件
- 织梦dede转换数据到人人站rrzcms免费插件
- Marlin固件配置
- latex调整caption字体大小
- 万里汇WorldFirst提现人民币实现当天到账!
- 动态网站数据采集 - 时光网电影信息爬虫
- linux环境vmd安装,如何在linux系统下安装VMD?
- Java、JSP教师信息管理系统
热门文章
- php selenium模拟登陆,python+selenium自动化实战携带cookies模拟登陆微博
- Ngxin禁止IP+端口号访问系统,设置只能通过域名访问
- Python课程设计:图书馆管理系统
- Java经典面试题分享
- Linux中使得新建的文件或文件夹自动继承其父文件夹的所属组
- java 选项卡面板_Java中的高级组件面板——JTabbedPane(选项卡面板)
- 深入jvm 讲得比较清楚
- dijkstra算法_最短路径算法—Dijkstra算法详解
- 微信商户平台注册步骤
- Qt学习之路之QMessageBox消息框