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.

72 lines
1.6 KiB

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
}
2 years ago
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{
2 years ago
"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
}
2 years ago
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
}