直播流媒体测试

docker

docker pull jasonrivers/nginx-rtmp
docker run -it -p 1935:1935 -p 8080:8080 jasonrivers/nginx-rtmp /bin/sh
/opt/nginx/sbin/nginx

nginx目录似乎在/opt/nginx/下面,查看配置文件,发现是在节点live
hls的url路径是在hls节点

mac安装ffmpeg

brew install ffmpeg

转码并推流

#ffmpeg -re -i ~/Projects/rtmp/xgtt/1.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/live/mystream
ffmpeg -re -i ~/Projects/rtmp/xgtt/1.mp4  -c copy -f flv rtmp://localhost:1935/live/mystream

ffmpeg -i rtmp://localhost:1936/live/mystream -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/live/mystream
ffmpeg -i rtmp://localhost:1936/live/mystream -c:v copy -c:a copy -f flv -y rtmp://localhost:1935/live/mystream

访问

<html>
  <head>
    <link href="video-js/video-js.min.css" rel="stylesheet" type="text/css">
    <script src="video-js/video.min.js"></script>
    <script>
      videojs.options.flash.swf = "video-js/video-js.swf";
    </script>
  </head>

  <body>
    <!--  rtmp loader-->
    <center>
      <font face="verdana" color=993333><h1>测试rtmp</h1></font>
       <video id="livestream" class="video-js vjs-default-skin vjs-big-play-centered"
         controls autoplay preload="auto" width="800" height="600"
         data-setup='{"example_option":true}'>
          <source src="rtmp://192.168.1.55:1935/live/mystream" type="rtmp/mp4">
      </video>
    </center>

    <div>
      <p>mobile test</p>
      <video autoplay webkit-playsinline>
          <source src="http://192.168.1.55:8080/hls/mystream.m3u8?key=rico" type="application/vnd.apple.mpegurl" />
          <p class="warning">Your browser does not support HTML5 video.</p>
      </video>
    </div>
  </body>
</html>

 

推流加密

on_publish设置自己的url

on_publish.php

<?php

// ?user=user&pass=pass

$user = isset($_GET['user']) ? $_GET['user'] : '';
$pass = isset($_GET['pass']) ? $_GET['pass'] : '';

if (empty($user) || empty($pass)) {
    echo "wrong query input";
    header('HTTP/1.0 404 Not Found');
    exit();
}

$saveuser = user;
$savepass = pass;

if (strcmp($user, $saveuser) == 0 && strcmp($pass, $savepass) == 0) {
    echo "Username and Password OK";
} else {
    echo "Username or Password wrong";
    header('HTTP/1.0 404 Not Found');
    exit();
}

?>

ffmpeg的推流命令改为

ffmpeg -re -i ~/Projects/rtmp/1.mkv -vcodec libx264 -acodec aac -f flv "rtmp://localhost:1935/live/mystream?user=user&pass=pass"

播放的话,也一样,on_publish换成on_play即可,但是这个是针对rtmp协议播放的,http的m3u8鉴权大概如下

        location /hls {  
                types {                    
                        video/mp2t ts;                     
                }                     
                root /tmp;
                add_header Cache-Control no-cache;
        }                                                  
                                      
        location ~* .*.m3u8 {
                types {       
                        application/vnd.apple.mpegurl m3u8;
                }                                          
                root /tmp;            
                add_header Cache-Control no-cache;
                                                           
                proxy_pass http://hls_backend;             
        }

自己的逻辑服务器

package main

import (
    "fmt"
    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/fasthttp"
    "net/http"
)

func main() {
    e := echo.New()

    e.GET("/hls/:fileName", func(c echo.Context) error {
        fileName := "/tmp/hls/" + c.Param("fileName")
        key := c.QueryParam("key")

        fmt.Println("fileName",fileName)
        fmt.Println("key",key)

        if key == "rico"{
            return c.File(fileName)
        }
        return c.String(http.StatusInternalServerError, "not accepted")

    })
    e.Run(fasthttp.New(":4000"))
}
原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/5777531.html