This commit is contained in:
YouROK
2023-02-27 23:20:29 +03:00
parent 7b505a26c9
commit dd9e72ef80
2 changed files with 53 additions and 27 deletions

51
server/ffprobe/ffprobe.go Normal file
View File

@@ -0,0 +1,51 @@
package ffprobe
import (
"context"
"gopkg.in/vansante/go-ffprobe.v2"
"io"
"os"
"os/exec"
"path/filepath"
"time"
)
var binFile = "ffprobe"
func init() {
path, err := exec.LookPath("ffprobe")
if err == nil {
ffprobe.SetFFProbeBinPath(path)
binFile = path
} else {
// working dir
if _, err := os.Stat("ffprobe"); os.IsNotExist(err) {
ffprobe.SetFFProbeBinPath(filepath.Dir(os.Args[0]) + "/ffprobe")
binFile = filepath.Dir(os.Args[0]) + "/ffprobe"
}
}
}
func Exists() bool {
_, err := os.Stat(binFile)
return !os.IsNotExist(err)
}
func ProbeUrl(link string) (*ffprobe.ProbeData, error) {
data, err := ffprobe.ProbeURL(getCtx(), link)
return data, err
}
func ProbeReader(reader io.Reader) (*ffprobe.ProbeData, error) {
data, err := ffprobe.ProbeReader(getCtx(), reader)
return data, err
}
func getCtx() context.Context {
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(5 * time.Minute)
cancel()
}()
return ctx
}

View File

@@ -1,25 +1,16 @@
package api
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"server/ffprobe"
"server/utils"
"github.com/gin-gonic/gin"
"gopkg.in/vansante/go-ffprobe.v2"
)
func commandExists(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func ffp(c *gin.Context) {
hash := c.Param("hash")
indexStr := c.Param("id")
@@ -32,23 +23,7 @@ func ffp(c *gin.Context) {
host := utils.GetScheme(c) + "://" + c.Request.Host + "/stream?link=" + hash + "&index=" + indexStr + "&play"
// log.Println("ffprobe", host)
ctx, cancelFn := context.WithCancel(context.Background())
defer cancelFn()
// path lookup
path, err := exec.LookPath("ffprobe")
if err == nil {
// log.Println("ffprobe found in", path)
ffprobe.SetFFProbeBinPath(path)
} else {
// log.Println("ffprobe not found in $PATH")
// working dir
if _, err := os.Stat("ffprobe"); os.IsNotExist(err) {
ffprobe.SetFFProbeBinPath(filepath.Dir(os.Args[0]) + "/ffprobe")
}
}
data, err := ffprobe.ProbeURL(ctx, host)
data, err := ffprobe.ProbeUrl(host)
if err != nil {
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("error getting data: %v", err))
return