美文网首页
[直播] nginx with rtmp module 服务器搭

[直播] nginx with rtmp module 服务器搭

作者: krmao | 来源:发表于2019-05-21 11:00 被阅读0次

安装 nginx with rtmp module

brew tap denji/nginx
brew install nginx-full --with-rtmp-module

配置 nginx

vi /usr/local/etc/nginx/nginx.conf
# 修改端口号
http {
    server {
        listen       5387; 
        server_name  localhost;
    }
}

# 增加监听 rtmp 模块, 这里与上面的 http 模块并列位置,即无父节点
rtmp {
  server {
      listen 5388;  
    #直播流配置
      application rtmplive {
          live on;
      #为 rtmp 引擎设置最大连接数。默认为 off
      max_connections 1024;
       }
      application hls{

          live on;
          hls on;
          hls_path /usr/local/var/www/hls;
          hls_fragment 1s;
      }
   }
}
nginx #启动
nginx -s reload #重启

查看是否启动

下载 https://evermeet.cx/ffmpeg/ (非必须, 如果已经有现成的推流拉流工具, 比如 ijkplayer/yasea 等)

下载到一个目录,自行添加到 PATH 环境变量

  1. 下载 编译好的 静态 ffmpeg (为了测试推流使用)
  2. 下载 编译好的 静态 ffplay (为了测试拉流播放使用)

使用 ffmpeg 推流

  1. 查看支持的设备
ffmpeg -devices
ffmpeg -f avfoundation -list_devices true -i ""
[AVFoundation input device @ 0x7fb223700700] AVFoundation video devices:
[AVFoundation input device @ 0x7fb223700700] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7fb223700700] [1] Capture screen 0
[AVFoundation input device @ 0x7fb223700700] [2] Capture screen 1
[AVFoundation input device @ 0x7fb223700700] [3] Capture screen 2
[AVFoundation input device @ 0x7fb223700700] AVFoundation audio devices:
[AVFoundation input device @ 0x7fb223700700] [0] USB Headphone Set
[AVFoundation input device @ 0x7fb223700700] [1] Built-in Microphone
: Input/output error

ffmpeg -i "0:0" 参数中 第一个 0 应该是 视频设配 的中括号中的数字, 第二个 0 应该是 音频设备中括号中的数字

  1. 推本地视频流
ffmpeg -re -i ~/Desktop/video.mp4 -c copy -f flv rtmp://localhost:5388/rtmplive/room # room 随便写, rtmplive 为nginx 中配置
  1. 推 mac 摄像头+音频
ffmpeg -f avfoundation -framerate 30 -video_size 1280x720 -i "0:1" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:5388/rtmplive/room-mac
  1. 推 mac 桌面+音频
ffmpeg -f avfoundation -framerate 30 -video_size 1280x720 -i "3:1" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:5388/rtmplive/room-desktop

在 mac 上使用 flv 播放器 预览

点击File->Open Network 
输入URL: rtmp://localhost:5388/rtmplive/room-desktop
VLC播放列表 推流第三个外接显示器

ffplay 播放 rtmp 流(加无缓冲参数延时大概只有0.2s 左右,不加大概3s+)

ffplay rtmp://10.32.33.20:5388/rtmplive/room-mobile  -fflags nobuffer  -analyzeduration 1000000
ffplay rtmp

参考链接

windows pc 下 nginx with rtmp modules

windows 下 ffmpeg 录屏 参考

# windows 下使用 ffmpeg 录屏桌面推送到 nginx-rtmp
# -s 尺寸
# -r 帧率
 ffmpeg -f gdigrab -offset_x 0 -offset_y 0 -i desktop -f dshow -list_devices 0 -s 720x480 -r 30 -f flv rtmp://localhost:5388/rtmplive/room-pc

windows 下录屏预览

windows 下录屏预览

win7-3D 热带鱼动态屏保下载


上面是服务端
下面是移动端


移动端推拉流

android 推流 https://github.com/begeekmyfriend/yasea

yasea push
ffplay 测试拉流播放 1s 内延迟

android 拉流 https://github.com/bilibili/ijkplayer

ijkplayer pull

我的案例代码 https://github.com/krmao/template/tree/master/apps/app-template/android

想要 run 起来估计要 npm install 同目录下的 react native 项目, 处理好依赖关系

image.png

相关文章

网友评论

      本文标题:[直播] nginx with rtmp module 服务器搭

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