ffmpeg地址:Download FFmpeg
下载地址:VLC: Official site - Free multimedia solutions for all OS! - VideoLAN
参考:https://blog.csdn.net/weixin_45495419/article/details/103913313里的代码,直接复制可用
可能集成了 Rtmp、openssl、pcre、zlib,具体还没看,主要是拿来使用。
nginx.conf里的配置:
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application live {
live on;
}
application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 8s;
}
}
}
主要是通过VLC播放器搭建rtsp流媒体测试,操作流程:Windows上通过VLC播放器搭建rtsp流媒体测试地址操作步骤_fengbingchun的博客-CSDN博客_rtsp流媒体播放器
参考2.4Video相关CSS、JS的链接里的代码,里面的视频地址是rtmp流。
ffmpeg -i rtsp://192.168.1.121:8554/test -vcodec copy -acodec copy -f flv rtmp://127.0.0.1:1935/live/play1
注意:
这是VLC播放器转换后的rtsp流地址。
①端口要与Nginx的监听的rtmp端口一致。
②端口后面的live是必须要与Nginx里监听的一致,如下图:
③live后面的地址任意,每一个地址代表一个rmtp流,即一个视频。
/**
* 启动Nginx、停止Nginx
*/
public static void startNginx(){
if(!StringUtils.isEmpty(Nginx)){
return;
}
//启动nginx
String nginxStart = "cmd /c cd E:\\Others\\VideoConvertTest\\nginx-1.8.1 && start nginx";
//关闭nginx
String nginxStop ="cmd /c cd E:\\Others\\VideoConvertTest\\nginx-1.8.1 && nginx.exe -s stop";
Runtime run = Runtime.getRuntime();
Nginx = "startNginx";
try {
Process stop = run.exec(nginxStop);
Process start = run.exec(nginxStart);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* rtsp转为rtmp
* @param videoPort
* @param videoName
* @return
*/
public static void convert(String videoPort,String videoName){
String videoId = videoPort + videoName;
if(rtspList.contains(videoId)){
return;
}
rtspList.add(videoId);
// String convertVideo = "cmd /c start E:\\Others\\VideoConvertTest\\ffmpeg\\bin\\ffmpeg.exe -i \"rtsp://192.168.1.121:" + videoPort + "/test\" -vcodec copy -acodec copy -f flv \"rtmp://127.0.0.1:1935/live/" + videoName + "\"";
String convertVideo = "cmd /c start E:\\Others\\VideoConvertTest\\ffmpeg2020\\bin\\ffmpeg.exe -i \"rtsp://192.168.1.121:" + videoPort + "/test\" -vcodec copy -acodec copy -f flv \"rtmp://127.0.0.1:1935/live/" + videoName + "\"";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(convertVideo);
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// while((line = bufferedReader.readLine()) != null) {
// sb.append(line + "\n");
// System.out.println("line------" + line);
// process.destroy();
// }
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
package video.player.utils;
import org.thymeleaf.util.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class RtspToRtmpUtil {
/**
* 记录Nginx是否已启动
*/
public static String Nginx = "";
/**
* 记录rtsp流是否已启动转换
*/
public static List rtspList = new ArrayList<>();
/**
* 启动Nginx、停止Nginx
*/
public static void startNginx(){
if(!StringUtils.isEmpty(Nginx)){
return;
}
//启动nginx
String nginxStart = "cmd /c cd E:\\Others\\VideoConvertTest\\nginx-1.8.1 && start nginx";
//关闭nginx
String nginxStop ="cmd /c cd E:\\Others\\VideoConvertTest\\nginx-1.8.1 && nginx.exe -s stop";
Runtime run = Runtime.getRuntime();
Nginx = "startNginx";
try {
Process stop = run.exec(nginxStop);
Process start = run.exec(nginxStart);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* rtsp转为rtmp
* @param videoPort
* @param videoName
* @return
*/
public static void convert(String videoPort,String videoName){
String videoId = videoPort + videoName;
if(rtspList.contains(videoId)){
return;
}
rtspList.add(videoId);
// String convertVideo = "cmd /c start E:\\Others\\VideoConvertTest\\ffmpeg\\bin\\ffmpeg.exe -i \"rtsp://192.168.1.121:" + videoPort + "/test\" -vcodec copy -acodec copy -f flv \"rtmp://127.0.0.1:1935/live/" + videoName + "\"";
String convertVideo = "cmd /c start E:\\Others\\VideoConvertTest\\ffmpeg2020\\bin\\ffmpeg.exe -i \"rtsp://192.168.1.121:" + videoPort + "/test\" -vcodec copy -acodec copy -f flv \"rtmp://127.0.0.1:1935/live/" + videoName + "\"";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(convertVideo);
// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
// while((line = bufferedReader.readLine()) != null) {
// sb.append(line + "\n");
// System.out.println("line------" + line);
// process.destroy();
// }
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
@GetMapping("/convert")
@ResponseBody
public String convert(@RequestParam(value = "videoPort") String videoPort, @RequestParam(value = "videoName") String videoName){
//启动新的线程
new Thread() {
public void run() {
RtspToRtmpUtil.startNginx();
RtspToRtmpUtil.convert(videoPort, videoName);
}
}.start();
return "rtmp://127.0.0.1:1935/live/" + videoName + "/";
}
完整代码:
package video.player.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import video.player.utils.RtspToRtmpUtil;
@Controller
public class VideoPlayerController {
@GetMapping("/player")
public String player(){
return "video/player.html";
}
@GetMapping("/rtmp")
public String rtmp(){
return "video/rtmp.html";
}
@GetMapping("/rtmp2")
public String rtmp2(){
return "video/rtmp2.html";
}
@GetMapping("/convert")
@ResponseBody
public String convert(@RequestParam(value = "videoPort") String videoPort, @RequestParam(value = "videoName") String videoName){
//启动新的线程
new Thread() {
public void run() {
RtspToRtmpUtil.startNginx();
RtspToRtmpUtil.convert(videoPort, videoName);
}
}.start();
return "rtmp://127.0.0.1:1935/live/" + videoName + "/";
}
}
参考1:使用java执行ffmpeg命令进行推流操作_社会新人的博客-CSDN博客_java执行ffmpeg命令
参考2:使用FFmpeg将rtsp流摄像头视频转码为rtmp播放 - 知乎
参考3:windows下用java执行cmd命令_yunque_的博客-CSDN博客_java 执行cmd