Linux---nginx+ffmpeg搭建流媒体服务器

原帖地址:https://blog.csdn.net/loyachen/article/details/50907828

这里实现了简单nginx+ffmpeg 推本地mp4视频文件的功能,以后将会继续更新

环境

系统环境:CentOS release 6.7 (Final)

需求

利用nginx和ffmpeg搭建流媒体服务器

步骤

安装ffmpeg

安装nginx

git clone https://github.com/arut/nginx-rtmp-module.git
#编译的时候添加nginx-rtmp-module模块
--add-module=path_of_/nginx-rtmp-module 
  • 1
  • 2
  • 3
  • 4
  • 我的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
  • 1

修改nginx配置文件nginx.conf

加入rtmp配置

#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;

#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s; rtmp { server { listen 1935; #直播流配置 application myapp { live on; } application hls { live on; hls on; hls_path /tmp/hls; } application qiniu { live on; push 推流地址; } application pull { live on; pull 拉流地址; } #rtmp日志设置 access_log logs/rtmp_access.log ; } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

在http中增加一个location配置支持hls

location /hls {  
            types {  
                application/vnd.apple.mpegurl m3u8;  
                video/mp2t ts;  
            }  
            root /tmp;  
            add_header Cache-Control no-cache; } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

完整的nginx.conf如下


#user  nobody;
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; } application hls { live on; hls on; hls_path /tmp/hls; } application qiniu { live on; push 推流地址; } application pull { live on; pull 拉流地址; } #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 0; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { root /opt/www/html; index index.html index.htm; } #rtmp状态页面 location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /opt/software/nginx-rtmp-module/; } 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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 这是一个最简单,最基础的配置, rtmp监听1935端口,如果是hls的话用hls on开启hls,并且为hls设置一个临时文件目录hls_path /tmp/hls; 其它更高级的配置可以参看nginx-rtmp-module的readme,里面有比较详细的介绍其它的配置,并且它还提供了一个通过JWPlayer在网页上播放的例子.

重启nginx

nginx -t
nginx -s reload
  • 1
  • 2
  • 查看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"
      • 1
      流播放地址为(10.0.0.6是我本地的IP):rtmp://10.0.0.6:1935/myapp/test1
      • 1
    • 推一个本地的mp4到hls上

      ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"
      • 1
      流播放地址为: http://10.0.0.6/hls/test2.m3u8
      • 1
    • 流媒体播放器地址:http://www.cutv.com/demo/live_test.swf

      这里写图片描述

    • VLC播放hls流: 
      这里写图片描述

猜你喜欢

转载自www.cnblogs.com/blackhumour2018/p/9427770.html
今日推荐