首页 > 系统相关 >使用 nginx 和 rtmp 插件搭建视频直播和点播服务器

使用 nginx 和 rtmp 插件搭建视频直播和点播服务器

时间:2023-11-14 17:04:16浏览次数:35  
标签:stat http application hls 插件 nginx rtmp

使用 nginx 和 rtmp 模块 ,可以很容易地搭建一个视频直播和点播服务器出来。下面我们来看一下具体实施步骤:

1. 安装 nginx 和 rtmp 模块

有关 nginx 的编译和安装比较简单,这里就不介绍了,看参考文献。这里提示以下几点:

(1) 安装好 nginx 后,配置文件在这里:

/usr/local/nginx/conf/nginx.conf
(2) 启动 nginx 的命令:

$ sudo /usr/local/nginx/sbin/nginx -s stop
$ sudo /usr/local/nginx/sbin/nginx

2. 配置 nginx 视频直播和点播服务

先看一下完整的 nginx 配置文件里有关视频点播和直播的配置:

rtmp {
	server {
		listen 1935;
		chunk_size 4096;
		application live {
			live on;
			record off;
		}
		application live2 {
			live on;
			record off;
		}
		# video on demand
		application vod {
			play /var/flvs;
		}
		application vod_http {
			play http://192.168.31.185/vod;
		}
		application hls {
			live on;
			hls on;
			hls_path /tmp/hls;
		}
	}
}
# HTTP can be used for accessing RTMP stats
http {
    server {
	listen      8080;
	# This URL provides RTMP statistics in XML
	location /stat {
	    rtmp_stat all;
	    # Use this stylesheet to view XML as web page
	    # in browser
	    rtmp_stat_stylesheet stat.xsl;
	}
	location /stat.xsl {
	    # XML stylesheet to view RTMP stats.
	    # Copy stat.xsl wherever you want
	    # and put the full directory path here
	    root /path/to/stat.xsl/;
	}
	location /hls {
	    # Serve HLS fragments
	    types {
		application/vnd.apple.mpegurl m3u8;
		video/mp2t ts;
	    }
	    root /tmp;
	    add_header Cache-Control no-cache;
	}
	location /dash {
	    # Serve DASH fragments
	    root /tmp;
	    add_header Cache-Control no-cache;
	}
    }
}

现在来解释一下里面各行代码的含义。对于视频直播服务,如果需要支持多路流输入的话,很简单,在 nginx 配置文件里多配几个 Application 就只可以了,像下面这样:

application live {
	live on;
	record off;
}
application live2 {
	live on;
	record off;
}

这样就可以通过下面的地址来推送直播流,其它观众端也可以通过下面的地址来访问直播流:

rtmp://192.168.31.185/live/test
rtmp://192.168.31.185/live2/test
后面紧跟的 test 关键字,可以随便更换,只要你的推送流和访问流的地址一样就可以了。

rtmp 模块也可以直接支持 VOD 这种视频点播服务 ,只需要在配置文件里添加如下内容即可:

# video on demand
application vod {
    play /var/flvs;
}

application vod_http {
    play http://myserver.com/vod;
}

然后把一个 mp4 或是 flv 文件扔到 /var/flvs 目录下,对于 /var/flvs/dir/file.flv 这个视频文件,就可以通过下面的网址来访问了:

http://myserver.com/vod//dir/file.flv

这样直接在浏览器里就可以通过网页观看视频。对于 mp4 文件,也可以实现 VOD 服务,不过需要的是采用 H.264 和 AAC 格式编码的 mp4 文件。

3. HLS 直播流的配置

如果需要使用 HLS 来视频直播,可以直接像配置文件那样,写上下面这一段:

application hls {
        live on;
        hls on;
        hls_path /tmp/hls;
}

同时把后面有关 http 访问的内容写上:

# HTTP can be used for accessing RTMP stats
http {
	server {
		listen	  8080;
		# This URL provides RTMP statistics in XML
		location /stat {
			rtmp_stat all;
			# Use this stylesheet to view XML as web page
			# in browser
			rtmp_stat_stylesheet stat.xsl;
		}
		location /stat.xsl {
			# XML stylesheet to view RTMP stats.
			# Copy stat.xsl wherever you want
			# and put the full directory path here
			root /path/to/stat.xsl/;
		}
		location /hls {
			# Serve HLS fragments
			types {
				application/vnd.apple.mpegurl m3u8;
				video/mp2t ts;
			}
			root /tmp;
			add_header Cache-Control no-cache;
		}
		location /dash {
			# Serve DASH fragments
			root /tmp;
			add_header Cache-Control no-cache;
		}
	}
}

配好以后,推流可以使用下面的地址:

rtmp://192.168.31.185/hls/movie
movie 关键字可以任何替换。对于观众端来说,可以有几种播放方式:

(1) 用 rtmp:

rtmp://192.168.31.185/hls/movie
(2) 用 hls 播放:

http://192.168.31.185:8080/hls/movie.m3u8这样就可以看到主播端推出来的流。注意,如果使用 http 方式,则是监听的 8080 端口,这个是在配置文件里写的。

4. 网页播放器播放

在第二步里,除了可以直接在浏览器里打开网址来观看视频,还可以写一个网页,实现像优酷那样的视频点播业务。通过使用第三方的播放器,在网页里植入该播放器来实现这个功能。

标签:stat,http,application,hls,插件,nginx,rtmp
From: https://blog.51cto.com/u_16357539/8371410

相关文章

  • vue3 AntV-X6 引入插件报错
    vue3AntV-X6引入插件报错:UncaughtTypeError:Cannotreadpropertiesofundefined(reading'ToolItem')vite引入路径的问题解决就是在引入插件的路径后面加上/lib:import{Keyboard}from'@antv/x6-plugin-keyboard/lib'直接写 import{Keyboard}from'@antv/......
  • maven 添加 checkstyle 插件约束代码规范
    本例示例,是引用http链接这种在线checkstyle.xml文件的配置方式,如下示例:<properties><maven.checkstyle.plugin.version>3.3.0</maven.checkstyle.plugin.version><!--支持本地绝对路径、本地相对路径、HTTP远程路径--><checkstyle.config.location>......
  • PostCSS通过px2rem插件和lib-flexible将px单位转换为rem(root em)单位实现大屏适配
    目录文档postcss中使用postcss-plugin-px2rem安装postcss-plugin-px2rem示例默认配置webpack中使用postcss-plugin-px2rem项目结构安装依赖文件内容大屏适配参考文章文档类似的插件postcss-plugin-px2remhttps://www.npmjs.com/package/postcss-plugin-px2remhttps://github.com/......
  • 推荐一个前端读取CSV文件的插件Papa Parse
    PapaParse点击跳转到官网,该插件可以将文件解析成2层数组。下面是vue项目引用的方法1.安装npminstallvue-papa-parse2.引入,在main.js里importVuefrom'vue'importVuePapaParsefrom'vue-papa-parse'Vue.use(VuePapaParse)3.使用,例如delimiter这类配置,可以参考......
  • Nginx 禁止页面缓存
    location^~/vue{add_headerCache-Control"no-cache,private,no-store,must-revalidate,max-stale=0,post-check=0,pre-check=0";indexindex.html;alias/home/zhuge/project/test/dist/;try_files......
  • Linux安装Nginx
    1.在/usr/local建nginx文件夹,下载nginx压缩文件nginx下载地址:http://nginx.org/download找稳定版本执行命令cd/usr/localmkdirnginxwgethttp://nginx.org/download/nginx-1.24.0.tar.gztar-zxvfnginx-1.24.0.tar.gz2.安装编译工具、库文件yum-yinstallmak......
  • 通过NGINX搭建TiDB负载均衡
    作者:像风一样的男子前言目前TIDB的负载均衡官网推荐使用HAProxy,社区主流也是HAProxy,本文尝试使用nginx四层代理tidb提供TCP协议下的负载均衡能力,因为nginx安装编译需要自己添加模块,很多小伙伴觉得麻烦,本文使用基于Nginx的openresty来安装,可以实现一键安装并打包各个模块,快速......
  • 记录工作项目中使用的插件(持续更新中)
    1.HighLightingSystem用于3D物体高亮显示在项目中的使用方法:导入插件后在需要高亮显示的3d物体上附加Highlighter组件,在需要显示高亮效果的摄像机上附加HighlightingRenderer组件。在代码中调整Highlighter属性即可控制物体高亮效果的开关、闪烁。使用场景:提示玩家点击,或鼠标......
  • Apipost IDEA插件如何使用
    Apipost-Helper是由Apipost推出的IDEA插件,写完接口可以进行快速调试,且支持搜索接口、根据method跳转接口,还支持生成标准的API文档,注意:这些操作都可以在代码编辑器内独立完成,非常好用!这里给大家介绍一下Apipost-Helper的安装和使用安装在IDEA编辑器插件中心输入Apipost搜索安装:Api......
  • 完全免费!超好用的IDEA插件推荐:Apipost-Helper
    Idea是一款功能强大的集成开发环境(IDE),它可以帮助开发人员更加高效地编写、调试和部署软件应用程序,Idea还具有许多插件和扩展,可以根据开发人员的需要进行定制和扩展,从而提高开发效率,今天我们就来介绍一款国产的API调试插件:Apipost-Helper什么是Apipost-Helper?Apipost-Helper是一......