You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
2 years ago
|
package ffmpegServer
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
ffmpegCmd "github.com/u2takey/ffmpeg-go"
|
||
|
"os/exec"
|
||
|
"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 = filepath.Join(streamArr...)
|
||
|
return streamPath, true
|
||
|
}
|
||
|
|
||
|
func ProbeWithTimeout(url string, timeoutMs int) error {
|
||
|
args := ffmpegCmd.ConvertKwargsToCmdLineArgs(ffmpegCmd.KwArgs{
|
||
|
"stimeout": timeoutMs * 1000,
|
||
|
"loglevel": "error",
|
||
|
})
|
||
|
args = append(args, url)
|
||
|
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
|
||
|
}
|