|
|
|
package ffmpegServer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
ffmpegCmd "github.com/u2takey/ffmpeg-go"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PrepareUrl(url string) (string, bool) {
|
|
|
|
if url == "" {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
urlArr := strings.Split(url, "?")
|
|
|
|
if len(urlArr) > 2 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
url = urlArr[0]
|
|
|
|
return url, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrepareStreamPath(streamPath string) (string, bool) {
|
|
|
|
streamArr := strings.Split(streamPath, "/")
|
|
|
|
if len(streamArr) != 2 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
streamPath = path.Join(streamArr...)
|
|
|
|
return streamPath, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildStreamFilePath(streamPath string) string {
|
|
|
|
streamArr := strings.Split(streamPath, "/")
|
|
|
|
return filepath.Join(streamArr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProbeWithTimeout(url string, timeoutMs int) error {
|
|
|
|
args := ffmpegCmd.ConvertKwargsToCmdLineArgs(ffmpegCmd.KwArgs{
|
|
|
|
"i": url,
|
|
|
|
"stimeout": timeoutMs * 1000,
|
|
|
|
"loglevel": "error",
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
cmd := exec.CommandContext(ctx, "ffprobe", args...)
|
|
|
|
errBuf := new(strings.Builder)
|
|
|
|
cmd.Stderr = errBuf
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
errStr := strings.TrimSpace(errBuf.String())
|
|
|
|
return errors.New(errStr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProbeStreamsWithTimeout(url string, timeoutMs int) (string, error) {
|
|
|
|
args := ffmpegCmd.ConvertKwargsToCmdLineArgs(ffmpegCmd.KwArgs{
|
|
|
|
"i": url,
|
|
|
|
"show_streams": "",
|
|
|
|
"stimeout": timeoutMs * 1000,
|
|
|
|
"of": "json",
|
|
|
|
"loglevel": "error",
|
|
|
|
})
|
|
|
|
ctx := context.Background()
|
|
|
|
cmd := exec.CommandContext(ctx, "ffprobe", args...)
|
|
|
|
infoBuf, errBuf := new(strings.Builder), new(strings.Builder)
|
|
|
|
cmd.Stdout = infoBuf
|
|
|
|
cmd.Stderr = errBuf
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
errStr := strings.TrimSpace(errBuf.String())
|
|
|
|
return "", errors.New(errStr)
|
|
|
|
}
|
|
|
|
infoStr := strings.TrimSpace(infoBuf.String())
|
|
|
|
return infoStr, nil
|
|
|
|
}
|