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")) }