增加 hls 转码支持

This commit is contained in:
snowlyg
2020-06-04 08:40:46 +08:00
parent 7e42d23f50
commit f8fb3fd4cd
27 changed files with 31 additions and 34 deletions
@@ -6,7 +6,7 @@ import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
)
type Model struct {
@@ -9,7 +9,6 @@ import (
"net/url"
"path"
"strings"
"time"
)
func GetRequestHref(r *http.Request) string {
@@ -56,7 +55,7 @@ func GetHttpCustomPath(roomName string) (string, error) {
customPath = fmt.Sprintf("rtsp://%s:8554/%s", outputIp, roomName)
return customPath, nil
} else if getServerType() == "hls" {
customPath := path.Join(roomName, time.Now().Format("20060102"))
customPath = roomName
return customPath, nil
} else {
return "", errors.New("错误推流服务类型")
@@ -86,7 +85,7 @@ func GetOutPutUrl(roomName, transType, customPath string) string {
url = fmt.Sprintf("rtsp://%s:8554/%v", outputIp, roomName)
return url
} else if getServerType() == "hls" {
url = path.Join(fmt.Sprintf("http://%s:10008/record", outputIp), customPath, fmt.Sprintf("out.m3u8"))
url = "http://" + path.Join(fmt.Sprintf("%s:10008/record", outputIp), customPath, fmt.Sprintf("out.m3u8"))
return url
} else {
return ""
+2 -2
View File
@@ -9,11 +9,11 @@ import (
"strings"
"time"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
figure "github.com/common-nighthawk/go-figure"
"github.com/kardianos/service"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"github.com/snowlyg/GoEasyFfmpeg/models"
"github.com/snowlyg/GoEasyFfmpeg/routers"
"github.com/snowlyg/GoEasyFfmpeg/rtsp"
+2 -2
View File
@@ -1,8 +1,8 @@
package models
import (
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
)
func Init() (err error) {
+1 -1
View File
@@ -2,7 +2,7 @@ package models
import (
"github.com/jinzhu/gorm"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
)
type User struct {
+1 -1
View File
@@ -13,7 +13,7 @@ import (
"time"
"github.com/gin-gonic/gin"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
)
/**
+2 -2
View File
@@ -7,14 +7,14 @@ import (
"net/http"
"path/filepath"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
"github.com/gin-contrib/pprof"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/penggy/cors"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/sessions"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
validator "gopkg.in/go-playground/validator.v8"
)
+2 -2
View File
@@ -1,13 +1,13 @@
package routers
import (
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
"github.com/snowlyg/GoEasyFfmpeg/models"
"log"
//"strings"
"github.com/gin-gonic/gin"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"github.com/snowlyg/GoEasyFfmpeg/rtsp"
)
+9 -3
View File
@@ -3,8 +3,8 @@ package routers
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"github.com/snowlyg/GoEasyFfmpeg/models"
"github.com/snowlyg/GoEasyFfmpeg/rtsp"
"log"
@@ -142,14 +142,16 @@ func (h *APIHandler) StreamStopAll(c *gin.Context) {
if pusher != nil {
pusher.Stoped = true
rtsp.GetServer().RemovePusher(pusher)
log.Printf("Stop success %v ", stream.ID)
} else {
log.Printf("Stop error %v ", stream.ID)
continue
}
}
}
c.IndentedJSON(200, "OK")
log.Printf("Stop success ")
return
}
@@ -250,6 +252,10 @@ func (h *APIHandler) StreamStartAll(c *gin.Context) {
pusher := rtsp.NewClientPusher(stream.ID, stream.URL, customPath)
pusher.Stoped = false
rtsp.GetServer().AddPusher(pusher)
log.Printf("Start success %v ", stream.ID)
} else {
log.Printf("Start error %v ", stream.ID)
continue
}
}
+2 -2
View File
@@ -2,8 +2,8 @@ package routers
import (
"fmt"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/db"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"log"
"net/http"
"runtime"
+1 -1
View File
@@ -1,6 +1,6 @@
package routers
import "github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
import "github.com/snowlyg/GoEasyFfmpeg/extend/utils"
var BuildVersion = "v9.0"
var BuildDateTime = ""
+1 -1
View File
@@ -1,7 +1,7 @@
package rtsp
import (
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"sync"
)
+5 -13
View File
@@ -2,7 +2,7 @@ package rtsp
import (
"fmt"
"github.com/snowlyg/GoEasyFfmpeg/extend/EasyGoLib/utils"
"github.com/snowlyg/GoEasyFfmpeg/extend/utils"
"log"
"os"
"os/exec"
@@ -10,7 +10,6 @@ import (
"strings"
"sync"
"syscall"
"time"
)
type Server struct {
@@ -41,6 +40,7 @@ func (server *Server) Start() (err error) {
logger := server.logger
ffmpeg := utils.Conf().Section("rtsp").Key("ffmpeg_path").MustString("")
m3u8DirPath := utils.Conf().Section("rtsp").Key("m3u8_dir_path").MustString("")
m3u8DirPath = strings.TrimRight(strings.Replace(m3u8DirPath, "\\", "/", -1), "/")
go func() { // 保持到本地
pusher2FfmpegMap := make(map[*Pusher]*exec.Cmd)
@@ -53,23 +53,15 @@ func (server *Server) Start() (err error) {
select {
case pusher, addChnOk = <-server.addPusherCh:
if addChnOk {
dir := path.Join(m3u8DirPath, "logs", time.Now().Format("20060102"))
err := utils.EnsureDir(dir)
if err != nil {
logger.Printf("EnsureDir:[%s] err:%v.", dir, err)
continue
}
m3u8DirPath := utils.Conf().Section("rtsp").Key("m3u8_dir_path").MustString("")
m3u8DirPath = strings.TrimRight(strings.Replace(m3u8DirPath, "\\", "/", -1), "/")
pusherPath := path.Join(m3u8DirPath, pusher.Path)
err = utils.EnsureDir(pusherPath)
if err != nil {
logger.Printf("EnsureDir:[%s] err:%v.", dir, err)
logger.Printf("EnsureDir:[%s] err:%v.", pusherPath, err)
continue
}
pusherPath = path.Join(pusherPath, fmt.Sprintf("out.m3u8"))
pusherPath = path.Join(pusherPath, fmt.Sprintf("out.m3u8"))
paramStr := utils.Conf().Section("rtsp").Key("decoder").MustString("-strict -2 -threads 2 -c:v copy -c:a copy -f rtsp")
paramsOfThisPath := strings.Split(paramStr, " ")
@@ -77,7 +69,7 @@ func (server *Server) Start() (err error) {
params = append(params[:4], append(paramsOfThisPath, params[4:]...)...)
cmd := exec.Command(ffmpeg, params...)
f, err := os.OpenFile(path.Join(dir, fmt.Sprintf("log.txt")), os.O_RDWR|os.O_CREATE, 0755)
f, err := os.OpenFile(path.Join(m3u8DirPath, fmt.Sprintf("log.txt")), os.O_RDWR|os.O_CREATE, 0755)
if err == nil {
cmd.Stdout = f
cmd.Stderr = f