Nginx搭建RTMP视频点播/直播/HLS服务器

一、nginx的服务器的搭建

安装nginx的依赖库

sudo apt-get update 

sudo apt-get  install libpcre3 libpcre3-dev

sudo apt-get  install openssl libssl-dev

下载nginx

wget http://nginx.org/download/nginx-1.11.3.tar.gz

下载rtmp的module

git clone https://github.com/arut/nginx-rtmp-module.git

然后分别解压缩,进入nginx目录

cd nginx-1.11.3

安装:

./configure--add-module=../nginx-rtmp-module

make

sudo make install

运行nginx

进入安装目录/usr/local/nginx,运行命令./sbin/nginx

打开浏览器在地址栏输入:localhost。如果,如下图显示那样就证明您的nginx服务器搭建成功了。


修改服务器的配置文件nginx.conf

cd /usr/local/nginx

vi conf/nginx.conf

我的内容大致是这样的:

#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;

}

rtmp {

server {

listen 8082;

chunk_size 4000;

#HLS

# For HLS to work please create a directory in tmpfs (/tmp/app here)

# for the fragments. The directory contents is served via HTTP (see

# http{} section in config)

#

# Incoming stream must be in H264/AAC. For iPhones use baseline H264

# profile (see ffmpeg example).

# This example creates RTMP stream from movie ready for HLS:

#

# ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264

#    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1

#    -f flv rtmp://localhost:1935/hls/movie

#

# If you need to transcode live stream use 'exec' feature.

#

application vod {

play /opt/video/vod ; #//file path

}

application live  {

live on;

hls on;

wait_key on;

hls_path /opt/video/hls;

hls_fragment 10s;

hls_playlist_length 60s;

hls_continuous on;

hls_cleanup on;

hls_nested on;

}

}

}

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;

72,0-1        21%

#access_log  logs/access.log  main;

sendfile        on;

#tcp_nopush    on;

#keepalive_timeout  0;

keepalive_timeout  65;

#gzip  on;

server {

listen  8081;

location /stat {

# Serve HLS fragments

rtmp_stat all;

rtmp_stat_stylesheet stat.xsl ;

}

location /stat.xsl {

root /home/vert/nginx-rtmp-module/;

}

location /live  {

types {

application/vnd.apple.mpegurl m3u8;

video/mp2t ts;

}

alias  /opt/video/hls;

expires -1;

add_header Cache-Control no-cache;

}

location / {

root html;

index index.html index.htm;

}

error_page 500 502 503 504  /50x.html;

location = /50x.html {

root html;

}

}

server {

listen      8080;

server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {

root  html;

index  index.html index.htm;

}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html

#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;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#    proxy_pass  http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

#    root          html;

#    fastcgi_pass  127.0.0.1:9000;

#    fastcgi_index  index.php;

#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

#    include        fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#    deny  all;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

}

重启nginx

sudo ./sbin/nginx -s reload

查看配置是否生效,浏览器输入 http://localhost:8081/stat

如图代表启动成功


二、使用ffmpeg推流(录屏)

如果没有ffmpeg需要先自行安装ffmpeg

推流命令无音频

ffmpeg -f avfoundation -i "1"    -strict -2  -acodec aac -f flv rtmp://your ip:8082/live/test

推流带有音频

不使用h264:     ffmpeg -f avfoundation -i "1:0"  -strict -2  -acodec aac -ar 44100 -ac 1 -f flv  rtmp://ip:8082/live/test

使用h264:

ffmpeg -f  avfoundation -i "1:0"  -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 4000k -acodec libmp3lame  -ar 44100 -ac 1 -f flv  rtmp://ip:8082/live/test

如果移动设备解码的话使用:

ffmpeg -f  avfoundation -i "1:0"  -framerate 25 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 4000k -acodec libmp3lame  -ar 44100 -ac 1 -f flv  rtmp://ip:8082/live/test

上面是MAC OS,下面是Ubuntu

./ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1024x768 -i :0.0 -framerate 25 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 4000k -acodec libmp3lame  -ar 44100 -ac 1 -f flv  rtmp://ip:8082/live/test

./ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1024x768 -i :0.0 -framerate 25 -vcodec libx264 -preset veryfast -maxrate 3000k -bufsize 4000k -strict -2  -acodec aac  -f flv  rtmp://ip:8082/live/test

查看RTMP流:

这里测试还有VLC客户端进行打开:

拉流地址(VLC客户端打开)

rtmp://your ip:8082/live/test

不出意外就能查看到屏幕视频信息了,测试过程中推流占用带宽过大(由于我用的mace ffmpeag未带h264编码器),后期需要进行视频压缩和音频压缩,同时我自己的服务器延迟比较大,后续有需要优化。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,757评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,478评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,540评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,593评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,903评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,329评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,659评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,383评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,055评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,337评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,864评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,227评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,820评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,999评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,750评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,365评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,260评论 2 258

推荐阅读更多精彩内容