ubuntu下使用nginx搭建流媒体服务器,实现视频点播
首先我们看如何实现视频点播,视频点播支持flv文件及H264编码视频,ACC编码音频的mp4文件:
第一步,创建单独的目录(因为软件较多,容易混乱),下载需要的软件:
我们需要下载nginx,pcre,zlib,openssl以及nginx-rtmp-module
1 mkdir work 2 3 cd work 4 5 wget http://nginx.org/download/nginx-1.10.3.tar.gz 6 7 wget http://zlib.net/zlib-1.2.11.tar.gz 8 9 wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz 10 11 wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz 12 13 wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
第二步,分别解压这四个文件:
tar -zxvf 文件名
第三步,编译安装nginx:
./configure --prefix=/usr/local/nginx --with-debug --with-pcre=../pcre-8.40 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2k --add-module=../nginx-rtmp-module-master make sudo make install
第四步,进入 /usr/local/nginx/conf,配置nginx.conf文件,在文件末尾添加如下内容:
rtmp { server { listen 1935; application vod { play /var/flvs; #指定存放视频文件的路径 } application vod_http { #myserver.com及服务器地址,如果只是本地播放,填写127.0.0.1:端口号 就行,端口好看配置文件中http监听的端口下同 play http://myserver.com/vod; } application vod_mirror { play /var/local_mirror http://myserver.com/vod; } } }
示例完整版:
#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 1935; application vod { play /video_test; } application vod_http { play http://192.168.1.102:8080/vod; } application vod_mirror { # try local location first, then access remote location play /var/local_mirror http://192.168.1.102:8080/vod; } } } 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 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 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 # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
然后,进入与conf文件夹同级的sbin目录下,运行:
sudo ./nginx
至此,服务器搭建完成,可以实现视频点播了。
视频点播测试:
播放位于 /video_test 下的file.flv视频文件:
ffplay rtmp://localhost/vod//file.flv
可以使用VLC这个播放器或者ffplay(需要安装ffmpeg),VLC自行百度安装即可。
ffmpeg安装:
sudo apt-add-repository ppa:jon-severinsson/ffmpeg sudo apt-get update sudo apt-get install ffmpeg