赞
踩
rtsp-simple-server是一个rtsp推流服务器,可以与gstreamer或ffmpeg结合进行推流。下载对应平台版本,直接运行即可。
/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
/usr/bin/gst-launch-1.0 v4l2src device=/dev/video0 ! timeoverlay ! videoconvert ! x264enc ! rtspclientsink location=rtsp://localhost:8554/mystream
/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
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;
}
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;
}
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;
opencv将视频帧传递给x264进行编码,将输出的264数据流写入管道文件,管道文件放在live555的推流可执行文件live555MediaServer统一目录下。
管道文件为FIFO,写入(推流)和读出(拉流)需要同时进行,否则会阻塞。
//需要忽略SIGPIPE信号
signal(SIGPIPE,SIG_IGN);//不处理PIPE信号
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);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。