為了實(shí)現(xiàn)視頻點(diǎn)播和直播,整了一天,看了N多博客,終于有了不小進(jìn)展,在這里分享給大家。 什么是視頻點(diǎn)播?可能你對這個名詞不熟悉,但是愛奇藝,騰訊視頻等這些視頻播放軟件想必你肯定知道。沒錯,視頻點(diǎn)播就是在線觀看某個視頻。額,視頻直播?這個就不解釋了吧,美女直播,游戲直播等等,哈哈。
首先我們看如何實(shí)現(xiàn)視頻點(diǎn)播,視頻點(diǎn)播支持flv文件及H264編碼視頻,ACC編碼音頻的mp4文件: 第一步,創(chuàng)建單獨(dú)的目錄(因?yàn)檐浖^多,容易混亂),下載需要的軟件: 我們需要下載nginx,pcre,zlib,openssl以及nginx-rtmp-module
mkdir workcd workwget http://nginx.org/download/nginx-1.10.3.tar.gzwget http://zlib.net/zlib-1.2.11.tar.gzwget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gzwget https://www.openssl.org/source/openssl-1.0.2k.tar.gzwget 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-mastermakesudo make install第四步,進(jìn)入 /usr/local/nginx/conf,配置nginx.conf文件,在文件末尾添加如下內(nèi)容:
rtmp { server { listen 1935; application vod { play /var/flvs; #指定存放視頻文件的路徑 } application vod_http { #myserver.com及服務(wù)器地址,如果只是本地播放,填寫127.0.0.1:端口號 就行,端口好看配置文件中http監(jiān)聽的端口下同 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; # } #}}然后,進(jìn)入與conf文件夾同級的sbin目錄下,運(yùn)行:
sudo ./nginx至此,服務(wù)器搭建完成,可以實(shí)現(xiàn)視頻點(diǎn)播了。 視頻點(diǎn)播測試: 播放位于 /video_test 下的file.flv視頻文件:
ffplay rtmp://localhost/vod//file.flv可以使用VLC這個播放器或者ffplay(需要安裝ffmpeg),VLC自行百度安裝即可。 ffmpeg安裝:
sudo apt-add-repository ppa:jon-severinsson/ffmpegsudo apt-get updatesudo apt-get install ffmpeg
|
新聞熱點(diǎn)
疑難解答
圖片精選