一、安装nginx流媒体服务器
下载nginx:http://nginx.org/ 和 nginx-rtmp-moudle 模块 可在:http://nginx-win.ecsds.eu/上找到已集成的。
编辑nginx-win配置文件,在http{} 后面或前面添加
rtmp {
server {
listen 1996;
application rtmplive {
live on;
}
application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 5s;
}
}
}
在http{}内的server{}内添加
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root temp;
add_header Cache-Control no-cache;
}
启动命名: nginx.exe -c conf/nginx-win.conf
二、JavaCv调用ffmpeg 推送视频流
Java依赖:
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.1-1.4.4</version>
</dependency>
调用代码:
public void toRtmpServer(InputStream stream , String outPut ) throws org.bytedeco.javacv.FrameGrabber.Exception, org.bytedeco.javacv.FrameRecorder.Exception{
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(stream);
grabber.setOption("rtsp_transport", "tcp");
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outPut, 1);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("flv");
try {
grabber.start();
recorder.start();
Frame frame =null ;
while (isStart&& (frame = grabber.grabFrame()) != null) {
recorder.record(frame);
}
recorder.stop();
grabber.stop();
} finally {
if (grabber != null) {
grabber.stop();
}
}
}
命令推流
ffmpeg -i rtsp://.. -codec copy -rtsp_transport tcp -f flv rtmp://server