使用工具:ffmpeg+rtsp-simple-server
下载链接:
ffmepg:https://ffmpeg.org/download.html
server:https://github.com/aler9/rtsp-simple-server/releases
使用步骤:首先运行server,然后再进行推流
终端指令推流
ffmpeg -re -i /home/ken1/videos/one_angle_test.avi -vcodec copy -acodec copy -f rtsp rtsp://192.168.0.232:8554/live.ffm
程序推流
python:
import cv2
import subprocess as sp
rtspUrl = 'rtsp://192.168.0.123:8554/test' #这里改成本地ip,端口号不变,文件夹自定义
# 视频来源 地址需要替换自己的可识别文件地址
filePath='D:WorkBeltDefectDetection'
camera = cv2.VideoCapture(filePath+'\'+'Video.avi') # 从文件读取视频
# 视频属性
size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)), int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
sizeStr = str(size[0]) + 'x' + str(size[1])
fps = camera.get(cv2.CAP_PROP_FPS) # 30p/self
fps = int(fps)
hz = int(1000.0 / fps)
print('size:'+ sizeStr + ' fps:' + str(fps) + ' hz:' + str(hz))
# 视频文件输出
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(filePath+'res_mv.avi',fourcc, fps, size)
# 直播管道输出
# ffmpeg推送rtmp 重点 : 通过管道 共享数据的方式
command = [
'ffmpeg',
# 're',#
# '-y', # 无需询问即可覆盖输出文件
'-f', 'rawvideo', # 强制输入或输出文件格式
'-vcodec','rawvideo', # 设置视频编解码器。这是-codec:v的别名
'-pix_fmt', 'bgr24', # 设置像素格式
'-s', sizeStr, # 设置图像大小
'-r', str(fps), # 设置帧率
'-i', '-', # 输入
'-c:v', 'libx264',
'-pix_fmt', 'yuv420p',
'-preset', 'ultrafast',
'-f', 'rtsp',# 强制输入或输出文件格式
rtspUrl]
#管道特性配置
# pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
pipe = sp.Popen(command, stdin=sp.PIPE) #,shell=False
# pipe.stdin.write(frame.tostring())
while (camera.isOpened()):
ret, frame = camera.read() # 逐帧采集视频流
if not ret:
break
############################图片输出
# 结果帧处理 存入文件 / 推流 / ffmpeg 再处理
pipe.stdin.write(frame.tostring()) # 存入管道用于直播
out.write(frame) #同时 存入视频文件 记录直播帧数据
camera.release()
out.release()
c++推流
#include <iostream>
#include <csignal>
#include <opencv2/opencv.hpp>
bool is_running = true;
void OnSignal(int) {
is_running = false;
}
int main() {
// 触发下面的信号就退出
signal(SIGINT, OnSignal);
signal(SIGQUIT, OnSignal);
signal(SIGTERM, OnSignal);
// 打开摄像头
cv::VideoCapture capture(0);
if (!capture.isOpened()) {
std::cerr << "Failed to open camera." << std::endl;
return EXIT_FAILURE;
}
int fps = capture.get(cv::CAP_PROP_FPS);
int width = capture.get(cv::CAP_PROP_FRAME_WIDTH);
int height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
std::stringstream fpsstrt,widthstrt,heightstrt;
fpsstrt << fps;
widthstrt << width;
heightstrt << height;
std::string fpsstr,widthstr,heightstr;
fpsstr = fpsstrt.str();
widthstr = widthstrt.str();
heightstr = heightstrt.str();
//capture.set(cv::CAP_PROP_FRAME_WIDTH, 720);
//capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
//std::string rtsp_server_url = "rtsp://192.168.0.232:8554/live/test";
std::string rtsp_server_url = "rtsp://192.168.1.13:8554/live1";
std::stringstream command;
command << "ffmpeg ";
//ffmpeg -re -i ./videos/one_angle_test.avi -vcodec copy -acodec copy -f rtsp rtsp://192.168.0.232:8554/live.ffm
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 "+widthstr+"x"+heightstr // set frame size (WxH or abbreviation)
<< " -r "+fpsstr; // set frame rate (Hz value, fraction or abbreviation)
command << " -i - "; //
command << "-c:v libx264 " // Hyper fast Audio and Video encoder
<< "-pix_fmt yuv420p " // set pixel format to yuv420p
<< "-preset 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");
// 将 cv 读到的每一帧传入子进程
if (fp != nullptr) {
while (is_running) {
capture >> frame;
if (frame.empty()) {
continue;
std::cout << "can not open camera" << std::endl;
}
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、VLC播放器进行拉流
2、nvr播放保存rtsp流
3、使用opencv调用rtsp流
参考文章