FFmpeg+RMTP實(shí)現(xiàn)推流以及瀏覽器顯示

文章內(nèi)容

1.安裝nginx-rtmp服務(wù)器
2.ffmpeg推流到nginx-rtmp服務(wù)器
3.瀏覽器播放RTMP視頻流

所需材料
  • nginx 1.7.11.3 Gryphon
  • nginx-rtmp-module-master
  • ffmpeg4.0
  • video-js-5.20.1

這些東西可以自行百度下載,也可以下載我打包好的,點(diǎn)擊下載

一、安裝nginx-rtmp服務(wù)器

1.把nginx 1.7.11.3 Gryphon.zip解壓并更名為nginx-1.7.11.3-Gryphon

2.把 nginx-1.7.11.3-Gryphon/conf/ 下的nginx-win.conf復(fù)制一份并更名為nginx.conf
在里邊添加如下內(nèi)容:

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # TV mode: one publisher, many subscribers
        application flow{

            # enable live streaming
            live on;

            # record first 1K of stream
            record all;
            record_path /tmp/av;
            record_max_size 1K;

            # append current timestamp to each flv
            record_unique on;

            # publish only from localhost
            allow publish 127.0.0.1;
            deny publish all;

            allow play all;
        }
    }
}

具體含義請參考:https://blog.csdn.net/defonds/article/details/9274479/

3.在 nginx-1.7.11.3-Gryphon/ 下的地址欄輸入cmd回車,啟動(dòng)cmd界面

  • 輸入start nginx啟動(dòng)nginx服務(wù)
    在瀏覽器輸入localhost:80(端口可在nginx.conf 的 http那部分進(jìn)行修改)可以看到Welcome to nginx!
  • 輸入nginx.exe -s quit停止nginx服務(wù)
  • nginx.exe -s stop也可以停止nginx服務(wù),不過相比上一個(gè)指令而言比較暴力

二、.ffmpeg安裝和使用

這個(gè)非常的簡單
1.解壓ffmpeg-20180606-e4006a4-win64-static.zip到你想安裝的目錄

2.把 ffmpeg-20180606-e4006a4-win64-static\bin 添加到系統(tǒng)環(huán)境變量

3.在cmd窗口輸入ffmpeg可以看到如下界面說明安裝成功

4.把視頻流推到RTMP服務(wù)器,命令如下:
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/flow/test
出現(xiàn)如下信息說明推流成功了

其中test.mp4是你要推的視頻地址,flow指nginx.conf里配置的application


命令含義及更多ffmpeg的命令請移步大佬的博客:http://www.cnblogs.com/wainiwann/p/4128154.html

5.要播放剛剛的推流可以另開一個(gè)cmd窗口輸入
ffplay rtmp://localhost:1935/flow/test

這樣播放可能會(huì)比較卡,可以用其他播放器播放,例如PotPlayer:




三、瀏覽器播放RTMP視頻流

這部分采用video.js來播放rtmp

1.進(jìn)入到 nginx-1.7.11.3-Gryphon\html 文件夾,可以看到有兩個(gè).html文件,index.html就是Welcome to nginx!這個(gè)頁面

先把它備份一份,再自己寫個(gè)index.html,用來顯示視頻流。內(nèi)容如下:

<!DOCTYPE html>  
<html>  
    <head>  
      <title>RTMP Sample Player Videojs</title>  
      <!-- Chang URLs to wherever Video.js files will be hosted -->  
      <link  rel="stylesheet" type="text/css">  
      <!-- video.js must be in the <head> for older IEs to work. -->  
      <script src="http://vjs.zencdn.net/5.20.1/video.js"></script>  
      <!-- Unless using the CDN hosted version, update the URL to the Flash SWF --> 
      <!-- 
      <script>  
        videojs.options.flash.swf = "video-js.swf";  
      </script>-->  
    </head>  
    <body>  
        <div align = "center">  
            <h1>RTMP Sample Player Videojs</h1>
            <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="640" height="480" data-setup="{}">  
                <source src="rtmp://localhost:1935/flow/test" type="rtmp/flv"/>  
                  
                <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a  target="_blank">supports HTML5 video</a></p>  
            </video>
        </div>
      
      
    </body>  
</html>  

2.video-js.swf文件從video-js-5.20.1.zip壓縮包中解壓出來放到nginx-1.7.11.3-Gryphon\html 文件夾下。

PS: video-js.cssvideo.js文件也在這個(gè)壓縮包中,也可以把它們都解壓出來放到nginx-1.7.11.3-Gryphon\html 文件夾下,然后把
http://vjs.zencdn.net/5.20.1/video-js.css改為video-js.css
http://vjs.zencdn.net/5.20.1/video.js改為video.js

3.在瀏覽器地址欄輸入localhost:80,不出意外的話可以看到這個(gè)界面:

莫慌,把Flash設(shè)為“在此網(wǎng)站上始終允許”即可正常播放


參考:
https://blog.csdn.net/defonds/article/details/9274479/
https://blog.csdn.net/king1425/article/details/72147376
https://www.cnblogs.com/dwj192/p/7040250.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容