1. 程式人生 > >ffmpeg+nginx+video實現rtsp流轉hls流,通過H5檢視監控視訊

ffmpeg+nginx+video實現rtsp流轉hls流,通過H5檢視監控視訊

一、FFmpeg下載:http://ffmpeg.zeranoe.com/builds/ 
下載並解壓FFmpeg資料夾,配置環境變數:在“Path”變數原有變數值內容上加上d:\ffmpeg\bin,驗證:ffmpeg -version 出現版本號則成功。
二、官網下載windows  Stable version版Nginx安裝nginx伺服器,配置:config和mime.types。
1.在nginx\conf\nginx.conf中:
 http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    access_log off;
    server {
修改:    
   listen       20000;
        server_name  localhost;
       修改:
        location / {
           root   html;
           index  index.html index.htm;
       }
為了能訪問到hls流協議新增:
location /hls {  
types{  
application/vnd.apple.mpegurl m3u8;  
video/mp2t ts;  
}  
root html;  
add_header Cache-Control no-cache;
       add_header Access-Control-Allow-Origin *;
        }  
2.在nginx\conf\mime.types中
為了支援hls流協議新增:
application/x-mpegURL m3u8; 
application/vnd.apple.mpegurl m3u8;
video/mp2t   ts;
3.在命令列中輸入即可轉換:也可寫成指令碼的形式執行。

ffmpeg -i "rtsp://admin:

[email protected]" -c copy -f hls -hls_time 2.0 -hls_list_size 0 -hls_wrap 15 C:/wjanzhuang/nginx/html/hls/test.m3u8

三、在H5中檢視:

1.引進:

 <link href="videolive/css/video.css" rel="stylesheet">
 <script src="videolive/js/video.js"></script>
  <script src="videolive/js/videojs-live.js"></script>

 2.使用video則可以在pc機瀏覽器上檢視視訊:

  <video id="my_video_1" class="video-js vjs-default-skin" controls preload="auto" width="500" height="500" 
  data-setup='{}'>
    <source src="http://192.168.10.245:20000/hls/test.m3u8" type="application/x-mpegURL"> 
  </video> 

三、在JAVA程式碼中檢視方法

List<String> commend = new ArrayList<String>();
        commend.add("ffmpeg");
        commend.add("-i");
        commend.add("\""+"rtsp://admin:

[email protected]"+"\"");
        commend.add("-c");
        commend.add("copy");
        commend.add("-f");
        commend.add("hls");
        commend.add("-hls_time");
        commend.add("2.0");
        commend.add("-hls_list_size");
        commend.add("0");
        commend.add("-hls_wrap");
        commend.add("15");
        commend.add("C:/wjanzhuang/nginx/html/hls/hls1/test.m3u8");
        try {
            ProcessBuilder builder = new ProcessBuilder(); //建立系統程序
            builder.command(commend);
            builder.start();//啟動程序
        } catch (Exception e) {
            e.printStackTrace();
        }