一、 前言
1.本文目的为实现windows下开机自动启动的桌面直播流媒体服务。
2具体方案为定时任务自动启动ffmpeg脚本开启hls流媒体直播,nginx自动启动hls流web服务。
3.网页使用EasyPlayerjs插件播放流媒体视频。(也可以用别的)
4.windows下搭建nginx的rtmp流媒体服务器(rtmp流媒体服务器/hls流媒体服务器)需要为nginx添加nginx-rtmp-module。
5.不过给nginx添加nginx-rtmp-module需要编译,另外一种更方便的方法就是使用带rtmp的nginx 1.7.11.3 Gryphon可以免去编译的步骤。
二、 环境与准备文件
1.官方ffmpeg下载地址:http://www.ffmpeg.org
2.官方nginx 1.7.11.3 Gryphon下载地址:http://nginx-win.ecsds.eu/download
3.官方nginx-rtmp-module下载地址:https://github.com/arut/nginx-rtmp-module
三、 安装nginx、ffmpeg
1.分别下载 nginx 1.7.11.3 Gryphon 和 ffmpeg,都复制到 D:/test 目录下。nginx 1.7.11.3 Gryphon文件夹改名为nginx-1.7.11.3-Gryphon。



2.然后再将下载好的 nginx-rtmp-module 复制到 D:/test/nginx-1.7.11.3-Gryphon/目录下。保证 stat.xls 的目录为:nginx-1.7.11.3-Gryphon/nginx-rtmp-module/stat.xsl。
3.然后配置环境变量:右键我的电脑 》属性 》高级系统设置 》高级 》环境变量 》双击 Path ,将 D:/test/ffmpeg-4.2.1-win64-static/bin 和 D:/test/nginx-1.7.11.3-Gryphon 都添加到 Path 路径下。
4.测试ffmpeg,调用命令行(windows+R输入cmd)输入“ffmpeg –version”,出现版本号说明配置成功。
注意:直接使用nginx没有 nginx-rtmp-module 插件会报错 nginx: [emerg] unknown directive “rtmp” 。想要自己编译请看第二讲:win7下nginx-rtmp-module的编译方法 – 简书 (jianshu.com)
四、 配置nginx.conf文件,启动nginx
1.在 D:/test/nginx-1.7.11.3-Gryphon/conf/ 下新建nginx-win-hls.conf,内容如下
1 worker_processes 2;
2
3 events {
4 worker_connections 8192;
5 }
6
7 rtmp {
8 server {
9 listen 1935;
10 application live {
11 #rtmp直播
12 live on;
13 }
14 application hls {
15 #hls直播
16 live on;
17 hls on;
18 hls_path D:/test/nginx-1.7.11.3-Gryphon/hls/;
19 hls_fragment 5s;
20 }
21 chunk_size 4096;
22 #数据传输块的大小
23 }
24 }
25
26
27 http {
28 include mime.types;
29 default_type application/octet-stream;
30
31 sendfile off;
32
33 server_names_hash_bucket_size 128;
34
35 client_body_timeout 10;
36 client_header_timeout 10;
37 keepalive_timeout 30;
38 send_timeout 10;
39 keepalive_requests 10;
40
41 server {
42 listen 80;
43 server_name localhost;
44 index web/index.html;
45 # 直播页
46
47 location /hls/ {
48 types {
49 application/vnd.apple.mpegurl m3u8;
50 video/mp2t ts;
51 }
52 alias D:/test/nginx-1.7.11.3-Gryphon/hls/;
53 expires -1;
54 }
55
56 location /stat {
57 rtmp_stat all;
58 rtmp_stat_stylesheet stat.xsl;
59 }
60 location /stat.xsl {
61 root nginx-rtmp-module/;
62 }
63 location /control {
64 rtmp_control all;
65 }
66
67 location / {
68 root html;
69 index index.html index.htm;
70 }
71
72 error_page 500 502 503 504 /50x.html;
73 location = /50x.html {
74 root html;
75 }
76 }
77 }
2.按住 windows 键 +R,输入 cmd,进入 cmd 命令窗口,进入 nginx 目录:cd D:/test/nginx-1.7.11.3-Gryphon,然后启动 nginx 流转换服务器:
nginx.exe -c conf/nginx-win-hls.conf
3.windows查看nginx是否启动
tasklist /fi "imagename eq nginx.exe"
五、 RTMP 推流测试
1.再打开一个 cmd 命令窗口,启动
ffmpeg -f gdigrab -i desktop -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f hls -hls_time 5.0 -hls_list_size 2 -hls_flags 2 D:/test/nginx-1.7.11.3-Gryphon/hls/tv.m3u8
2.启动没有报错,就可以使用vlc播放器打开测是否能看到。


六、 网页展示
1.找到Easyplayer的gitcode地址,下载解压。修改demo里面的index。
<!DOCTYPE html>
<html>
<head>
<title>Live - HLS</title>
<link rel="icon" href="./favicon.ico" />
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="renderer" content="webkit" />
<meta name="force-rendering" content="webkit" />
<meta
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
name="viewport"
/>
<script type="text/javascript" src="EasyPlayer-element.min.js"></script>
<style>
#player .easy-player {
height: 900px;
width: 3200px;
position: relative;
}
</style>
</head>
<body>
<easy-player
id="player"
video-url="http://127.0.0.1/hls/tv.m3u8"
live="true"
aspect="3200:900"
debug="true"
isresolution="true"
resolution="yh,fhd,hd,sd"
resolutiondefault="yh"
></easy-player>
</body>
</html>
2.把 demo 文件夹拷贝到 D:/test/nginx-1.7.11.3-Gryphon/html 中,浏览器访问127.0.0.1/demo即可看到直播画面。
七、 编写脚本服务随系统自启动
1.新建text文本,录入以下内容。保存后修改后缀为bat文件。
2.打开windows定时任务,配置新增任务。
3.重启测试。
四、 参考资料
Windows 10系统下安装FFmpeg教程详解_SLASH-YONG的博客-CSDN博客_windows 安装ffmpeg
Windows 搭建 nginx RTMP 服务器 – fengMisaka – 博客园 (cnblogs.com)
Windows11实现录屏直播,H5页面直播 HLS ,不依赖Flash – 林诺欧巴 – 博客园 (cnblogs.com)
使用Nginx+FFmpeg 将Rtmp转码显示在前端的解决方案_苏筱没猫的博客-CSDN博客_nginx rtmp转码
nginx搭建rtmp服务器(windows) – 知乎 (zhihu.com)
ffmpeg hls_wrap限制生成文件个数命令不生效_ffmpeg吧_百度贴吧 (baidu.com)
Windows 11实现直播,VLC超简单实现捕获、串流、播放 – 林诺欧巴 – 博客园 (cnblogs.com)
mirrors / tsingsee / EasyPlayer.js · GitCode
Windows cmd(bat) 脚本简单使用 – 腾讯云开发者社区-腾讯云 (tencent.com)
Windows下程序的自启动脚本bat(教你直接用)_望夫山居士的博客-CSDN博客_自启动脚本
五、 补充
windows关闭nginx命令()
taskkill /f /t /im nginx.exe
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/288166.html