当前位置: 首页 > 编程笔记 >

利用nginx与ffmpeg搭建流媒体服务器过程详解

柴砚文
2023-03-14
本文向大家介绍利用nginx与ffmpeg搭建流媒体服务器过程详解,包括了利用nginx与ffmpeg搭建流媒体服务器过程详解的使用技巧和注意事项,需要的朋友参考一下

需求

本文介绍的是利用nginx和ffmpeg搭建流媒体服务器的过程。例如这种场景:公司内部需要同时观看在线直播时,如果每个人直接观看必然给出口带宽带来压力,影响正常访问外网的同事。所以可以在内网通过nginx+ffmpeg拉一路直播流,然后内网的用户访问内网的这台流媒体服务器即可。通过nginx+ffmpeg还可以实现推流、拉流、转推甚至利用FFmpeg实时切片、视频处理等,实现一套直播服务模型。

环境

系统环境:CentOS release 6.7 (Final)

步骤

安装ffmpeg

安装过程参考官方文档:https://trac.ffmpeg.org/wiki/CompilationGuide

安装Nginx

这里采用了编译安装的方式,需要注意的是:一定要添加nginx-rtmp-module模块

git clone https://github.com/arut/nginx-rtmp-module.git
#编译的时候添加nginx-rtmp-module模块
--add-module=path_of_/nginx-rtmp-module

我的nginx编译参数

./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/opt/software/pcre-8.35 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1i --add-module=/opt/software/nginx-1.8.1/modules/nginx-rtmp-module

修改nginx配置文件nginx.conf

user nginx;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid  logs/nginx.pid;
events {
 use epoll;
 worker_connections 1024;
}
#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;
#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s;
rtmp { 
 server { 
  listen 1935; 
  
  #直播流配置
  application myapp { 
   live on; 
  } 
 
  # hls切片
  application hls { 
   live on; 
   hls on; 
   hls_path /tmp/hls; 
  } 
 
  # 拉流
  application qiniu {
   live on;
   push rtmp://拉流地址;
  }
 
  # 转推
  application pull {
   live on;
   pull rtmp://转推地址;
  }
  
  # rtmp日志设置
  access_log logs/rtmp_access.log ;
 } 
}
http {
 include  mime.types;
 default_type application/octet-stream;
 
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
      
 access_log logs/access.log main;
 
 sendfile  on;
 #tcp_nopush  on;
 keepalive_timeout 65;
 gzip on;
 
 server {
  listen  80;
  server_name localhost;
  charset utf-8;
  access_log logs/host.access.log main;
  
  location /stat {
  rtmp_stat all;
   rtmp_stat_stylesheet stat.xsl;
  }
  
  location /stat.xsl {
   root /opt/software/nginx-rtmp-module/;
  }
  
  location / {
   root /opt/www/html;
   index index.html index.htm;
  }
  
  location /hls { 
   types { 
    application/vnd.apple.mpegurl m3u8; 
    video/mp2t ts; 
   } 
   root /tmp; 
   add_header Cache-Control no-cache; 
  } 
  
  #error_page 404    /404.html;
  # redirect server error pages to the static page /50x.html
 
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
include vhosts/*.conf;
}

这是一个较简单的配置, rtmp监听1935端口,如果是hls的话用hls on开启hls,并且为hls设置一个临时文件目录hls_path /tmp/hls; 其它更高级的配置可以参看nginx-rtmp-module的readme,里面有比较详细的介绍其它的配置。

重启Nginx

nginx -t
nginx -s reload

查看Nginx已监听1935端口。

使用ffmpeg推流到nginx

推一个本地的mp4到上面配置的myapp上:

ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1"

流播放地址为(10.0.0.6是我本地的IP):rtmp://10.0.0.6:1935/myapp/test1

推一个本地的mp4到hls上:

ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"

流播放地址为: http://10.0.0.6/hls/test2.m3u8

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。谢谢大家对呐喊教程的支持。

 类似资料:
  • 本文向大家介绍Nginx服务器搭建和基本配置详解,包括了Nginx服务器搭建和基本配置详解的使用技巧和注意事项,需要的朋友参考一下 Nginx(engine X) 是一个高性能的 HTTP 服务器和反向代理服务器,这款软件开发的目的是为了解决 C10k 问题。 Nginx 的架构利用了许多现代操作系统的特性,以实现一个高性能的 HTTP 服务器。例如在 Linux 系统上,Nginx 使用了 ep

  • 本文向大家介绍Python使用socketServer包搭建简易服务器过程详解,包括了Python使用socketServer包搭建简易服务器过程详解的使用技巧和注意事项,需要的朋友参考一下 官方提供了socketserver包去方便我们快速的搭建一个服务器框架。 server类 socketserver包提供5个Server类,这些单独使用这些Server类都只能完成同步的操作,他是一个单线程的

  • 本文向大家介绍java使用xfire搭建webservice服务的过程详解,包括了java使用xfire搭建webservice服务的过程详解的使用技巧和注意事项,需要的朋友参考一下 前言 以前用的都是 apache 的cxf来搞webservice,今天做项目发现这个项目用的是 xfire,于是搭一个,写个demo用一下,在此记录一下过程。 搭建过程 本文使用的是maven形式的web工程。不知

  • 本文向大家介绍Python3 jupyter notebook 服务器搭建过程,包括了Python3 jupyter notebook 服务器搭建过程的使用技巧和注意事项,需要的朋友参考一下 1. jupyter notebook 安装 •创建 jupyter 目录 •创建独立的 Python3 运行环境,并激活进入该环境 •安装 jupyter 2. jupyter notebook 配置 •创

  • 本文向大家介绍Linux下Redis服务器搭建过程,包括了Linux下Redis服务器搭建过程的使用技巧和注意事项,需要的朋友参考一下 系统环境 操作系统:CentOS 6.9 redis版本:redis-4.0.2安装步骤 1,安装预环境 运行以下命令安装预环境。 [root@redis02 redis-4.0.2]# yum -y install gcc make 2,下载redis源代码文件

  • 本文向大家介绍利用nginx搭建静态资源服务器的方法步骤,包括了利用nginx搭建静态资源服务器的方法步骤的使用技巧和注意事项,需要的朋友参考一下 以windows为例,linux其实一样; 搭建静态资源服务器 我电脑上的work文件夹下面有很多图片,我想通过nginx搭建静态资源服务器,通过在地址栏输入ip+port的方式完成目录的映射 找到nginx安装目录,打开/conf/nginx.con