美文网首页UI
[Server] 基于RTMP搭建nginx直播推拉流serve

[Server] 基于RTMP搭建nginx直播推拉流serve

作者: 木小易Ying | 来源:发表于2020-02-09 18:40 被阅读0次

这个也属于玩玩闹闹系列0.0 啥也不懂纯属瞎玩儿~ 步骤大概如下:

  • 安装Nginx (服务器)
  • 安装FFmpeg (推流用,把视频推给服务器)
  • 安装VLC客户端 (拉流用,从服务器拿数据)
1. 安装nginx
// 拉取nginx
brew tap denji/nginx 

// 安装带有rtmp模块的nginx
brew install nginx-full --with-rtmp-module

安装过程中,homebrew或帮我们自动的安装如pcre、openssl等模块。因此相对于其他平台的安装方式或者源码安装方式,homebrew非常方便。

然后我们可以通过下面的命令看nginx的信息:

brew info nginx-full

我们可以看到nginx安装目录之类的信息:

$ brew info nginx-full
denji/nginx/nginx-full: stable 1.17.8, HEAD
HTTP(S) server, reverse proxy, IMAP/POP3 proxy server
https://nginx.org/
Conflicts with:
  nginx (because nginx-full symlink with the name for compatibility with nginx)
/usr/local/Cellar/nginx-full/1.17.8 (8 files, 1.3MB) *
  Built from source on 2020-02-09 at 08:19:10 with: --with-rtmp-module
From: https://github.com/denji/homebrew-nginx/blob/master/Formula/nginx-full.rb
==> Dependencies
Required: pcre ✔, openssl@1.1 ✔
Optional: gd ✘, geoip ✘, gperftools ✘, passenger ✘, imlib2 ✘
==> Options
……
--HEAD
    Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

- Tips -
Run port 80:
 $ sudo chown root:wheel /usr/local/opt/nginx-full/bin/nginx
 $ sudo chmod u+s /usr/local/opt/nginx-full/bin/nginx
Reload config:
 $ nginx -s reload
Reopen Logfile:
 $ nginx -s reopen
Stop process:
 $ nginx -s stop
Waiting on exit process
 $ nginx -s quit

To have launchd start denji/nginx/nginx-full now and restart at login:
  brew services start denji/nginx/nginx-full
Or, if you don't want/need a background service you can just run:
  nginx

config文件在/usr/local/etc/nginx/nginx.conf,web容器路径为/usr/local/var/www

然后我们就需要配置rtmp啦~ 在nginx.conf配置文件里面和http同级加一段rtmp:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    ……
}

// 添加
rtmp {
    server {
      listen 1935;
      chunk_size 4000;
      application rtmplive {
        live on;
        max_connections 1024;
      }

      application hls {
        live on;
        hls on;
        hls_path /usr/local/var/www/hls;
        hls_fragment 1s;
      }
    }
}

最后面hls_path就是待会要用到的推流文件目录了。一般来说不必创建,如果出现文件夹权限问题的话,手动添加下可读可写权限就可以了。

2. 安装ffmpeg

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

brew install ffmpeg

中间可能经常error不断重新install就好~ 成功以后可以通过ffmpeg命令来查看这个库的信息:

ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.16)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.2_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.0.1.jdk/Contents/Home/include/darwin -fno-stack-check' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
3. 下载VLC客户端(支持rtmp协议的视频播放器)

https://get.videolan.org/vlc/3.0.8/macosx/vlc-3.0.8.dmg

4. 联调

首先是关于server的一些命令,我们先reload一下server:

// 关闭nginx
nginx -s stop

// 打开:
nginx

// reload:
nginx -s reload

然后准备一个视频命名为test.mp4,通过ffmpeg把它推上去:(注意这里的rtmplive和nginx config里面的配置要一致如果你的服务名不是这个需要替换哦)

ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/rtmplive/rtmp

然后打开VLC,通过菜单中File-Open Network输入网址rtmp://localhost:1935/rtmplive/rtmp即可:

VLC client

效果就是酱紫的:


推拉流

用手机端推流,电脑拉流的效果是酱紫的:


客户端推流

好啦这就是今天的玩儿一下啦~~ 最近都没啥深度,真的是在家太痛苦了想出去逛街买衣服0.0 anyway希望所有人健健康康开开心心的吖!

参考:
https://www.jianshu.com/p/4f96e18827e2
https://www.jianshu.com/p/7fecc80ae51c

相关文章

网友评论

    本文标题:[Server] 基于RTMP搭建nginx直播推拉流serve

    本文链接:https://www.haomeiwen.com/subject/rrmgxhtx.html