安裝完FFmpeg,nginx,nginx-rtmp-module,vlc後就可以進行推流、拉流測試了。博主的nginx安裝在VMWare的Ubuntu中(安裝方法可以參考https://blog.csdn.net/u014552102/article/details/86599289),ffmpeg和vlc都安裝在windows中(win10)。
一、nginx配置
首先我們得進行nginx的rtmp功能配置和直播狀態監聽配置。我們切換爲root用戶,使用vim打開Ubuntu的/usr/local/nginx/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 1935; chunk_size 4096; application live { live 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; location /stat{ rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl{ root /home/cjc/安裝包/nginx/nginx-rtmp-module-master; } } server { listen 80; 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; # } #}}
其中,在nginx.conf文件中修改的內容中,新增加的下面的內容是使nginx增加rtmp功能。其中1935是監聽的端口,live on表示開啓實時。
rtmp{ server { listen 1935; chunk_size 4096; application live { live on; } }}
在nginx.conf文件中新增加的下面的內容,是使nginx能具有直播狀態監聽的功能。其中/home/cjc/安裝包/nginx/nginx-rtmp-module-master是博主安裝的nginx-rtmp-module的絕對路徑,各位得根據自己安裝的nginx-rtmp-module的路徑進行修改。8080是拉流請求的端口號。
server { listen 8080; location /stat{ rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl{ root /home/cjc/安裝包/nginx/nginx-rtmp-module-master; } }
執行完上述步驟後,如果nginx服務器正在運行,我們得先關掉nginx服務器然後重啓nginx後,上述更改的配置纔會生效。我們執行命令:
killall nginx
二、推流
推流,指的是把採集階段封包好的內容傳輸到服務器的過程,主播端把本地採集的語音視頻流推送到媒體服務器。就是將現場的視頻信號傳到網絡的過程。在本博文中我們會將主機windows中的媒體文件video5.mp4推流到虛擬機Ubuntu的流媒體服務器nginx中,然後通過windows下的vlc播放出來。
在Ubuntu中執行命令:
ifconfig
如下圖所示,可以看到Ubuntu的ip地址是192.168.1.109。記住這個地址。推流和拉流都要用到這個地址。
然後啓動nginx服務器,在Ubuntu中執行命令:
/usr/local/nginx/sbin/nginx
然後我們來到FFmpeg安裝目錄。博主的FFmpeg安裝在主機的win10下,如下圖所示,文件夾裏面有ffmpeg.exe和其依賴的dll,還有媒體文件video5.mp4。我們通過ffmpeg將video5.mp4推流到流媒體服務器crtmpserver中。
在windows的命令提示符中執行命令:
ffmpeg -i video5.mp4 -f flv rtmp://192.168.1.109/live/test1
其中-i 表示輸入流。這裏的輸入流是video5.mp4。-f 表示設定的輸出格式。這裏因爲要推流,推流得使用flv格式,所以設成-f flv。192.168.1.109是要推流到的流媒體服務器所在的Ubuntu的ip地址。上述語句的意思是通過ffmpeg將媒體文件video5.mp4推流到ip地址爲192.168.1.109的平臺中。
執行完上述命令,會出現如下界面,表示推流成功了。
然後我們在瀏覽器中輸入http://192.168.1.109:8080/stat。其中192.168.1.109是nginx所在的Ubuntu的ip地址,8080是端口號。如下圖所示,在下面的頁面中我們可以進行直播狀態監聽了。
三、拉流
拉流指的是用戶端從服務器拉取語音視頻流到客戶端播放。在本博文中我們會通過vlc拉流Ubuntu的crtmpserver中的語音視頻流,然後在vlc播放。
執行完上述推流的步驟後,我們在windows中打開vlc,點擊“打開網絡串流”,如下圖所示:
輸入網絡URL,如下圖所示:
可以看到在vlc中出現視頻畫面了,表示拉流成功了