最近调试个接口需要将接口返回的 " rtmp://ns8.indexforce.com/home/mystream "这个rtmp地址在html页面播放,因为现在各大浏览器都不支持flash插件了,所以这个rtmp需要转成html支持的格式才可以,我也是查了各种资料才发现可以用ffmpeg将视频流推到nginx上,再有nginx转成html支持的样式
1.首先要下载nginx和nginx-http-flv-module模板,这个没什么说的,大家可以参考下这个
nginx安装好后需要配置nginx.cof,关键的配置如下
rtmp {
server {
listen 1935; # 接受推流的端口号
chunk_size 8192; # 单一推流数据包的最大容量
application live {
live on;
record all;
record_unique off;#是否再文件名中启用时间戳
record_path "/usr/local/nginx/video";#视频缓存地址
record_suffix .flv;#文件名后缀
}
}
}
location / {
add_header 'Access-Control-Allow-Origin' '*';
root html;
index index.html index.htm;
}
# 无人机视频播放地址,flv
location /live {
flv_live on;
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
我的整个的配置是这样的,大家只要把上面两个配置放到正确位置就可以了
worker_processes 1;events {
worker_connections 1024;
}rtmp {
server {
listen 1935; # 接受推流的端口号
chunk_size 8192; # 单一推流数据包的最大容量?application mlive { # mlive 模块,可以自行更换名字
live on; # 打开直播
meta off; # 为了兼容网页前端的 flv.js,设置为 off 可以避免报错
gop_cache on; # 支持GOP缓存,以减少首屏时间
allow play all; # 允许来自任何 ip 的人拉流
}application live {
live on;
record all;
record_unique off;#是否再文件名中启用时间戳
record_path "/usr/local/nginx/video";#视频缓存地址
record_suffix .flv;#文件名后缀
}
}
}http {
include mime.types;
default_type application/octet-stream;
server {
listen 1877;
server_name localhost;location / {
add_header 'Access-Control-Allow-Origin' '*';
root html;
index index.html index.htm;
}# 无人机视频播放地址,flv
location /live {
flv_live on;
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2.下载安装Ffmpeg
应为我这边需要用Java操作,所以需要将Ffmpeg配置到Linux的环境变量中,还有一点是FFmpeg下载好后不要在本地解压后在上传至服务器,我的就是因为这种操作结果视频推流的时候报错.
可以参考下这个
Ffmpeg配置好后整个环境就搭建好了,可以用命令来看看视频是否能推流成功
ffmpeg -re -i rtmp://ns8.indexforce.com/home/mystream -c copy -f flv rtmp://192.168.1.101:1935/live/testv
这是将rtmp://ns8.indexforce.com/home/mystream(美国直播电视台)推给我的服务器上的nginx代理.
直接用html访转nginx转换后的地址就可以了http://192.168.1.101:1877/live?port=1935&app=live&stream=testv就可以了
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>flv播放页面</title>
<script type="text/javascript">
$(document).ready(function() {
});
//这是当关闭页面或浏览器的时候调用后台的方法把进程给停掉
window.onbeforeunload = function(e) {
$.ajax({
url:"${ctx}/zxjk/zxBasDevice/closeFfmpeg",
type:"post",
data:{
"command":"${command}"
},
dataType:"json",
async:true,
success: function (data){
}
})
}
</script>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/flv.js/1.5.0/flv.min.js"></script>
<video style="height: 400px;width: 600px;" id="videoElement" muted autoplay controls></video>
<script>
var flvPlayer = null;
if (flvjs.isSupported()) {
var videoElement = document.getElementById('videoElement');
flvPlayer = flvjs.createPlayer({
type: 'flv',
url: 'http://192.168.1.101:1877/live?port=1935&app=live&stream=testv'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
}
</script>
</body>
</html>
3.最后用Java代码操作linux命令
//启用ffmpeg并生成地址返回给html
public String toVideo(HttpServletRequest request, Model model) {
String rtmpurl= "rtmp://ns8.indexforce.com/home/mystream";
if(StringUtils.isNotEmpty(rtmpurl)){
List<String> commend = new ArrayList<String>();
commend.add("ffmpeg");
commend.add("-i");
commend.add(rtmpurl);
commend.add("-c");
commend.add("copy");
commend.add("-f");
commend.add("flv");
commend.add("rtmp://192.168.1.101:1935/live/testv");
try {
ProcessBuilder builder = new ProcessBuilder(); //创建系统进程
builder.command(commend);
builder.start();//启动进程
} catch (Exception e) {
e.printStackTrace();
}
//页面访问的地址
model.addAttribute("flvurl","http://192.168.1.101:1877/live?port=1935&app=live&stream=testv");
return "modules/zxjk/zxBasDeviceVideo";
}
/**
* 查询关闭Linux进程
* @param command
* @return
* @throws Exception
*/
public String getPID(String command) throws Exception {
logger.info("相关信息 -----------------------> " + command);
BufferedReader reader = null;
try {
// 显示所有进程
Process process = Runtime.getRuntime().exec("ps -ef");
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
if (line.contains(command)) {
String[] strs = line.split("\\s+");
logger.info(strs[1]+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!找到有关进程:"+line);
return strs[1];
}else{
String[] strs = line.split("\\s+");
logger.info(strs[1]+">>>>>>>>>>>>>>>>>>>>>>>没有找到有关进程:"+line);
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
logger.info("找不到有关进程pid返回null");
return null;
}
/**
* 关闭Linux进程
*
* @param Pid 进程的PID
*/
public void closeLinuxProcess(String Pid) {
Process process = null;
BufferedReader reader = null;
try {
// 杀掉进程
logger.info("准备执行 kill -9 " + Pid);
process = Runtime.getRuntime().exec("kill -9 " + Pid);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
logger.info("kill PID return info -----> " + line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
关于项目所用到的所有的包我都放到了下面的地址上,可以直接拿去用

https://blog.youkuaiyun.com/Aarstg/article/details/122937043
1460

被折叠的 条评论
为什么被折叠?



