@@ -0,0 +1,55 @@
|
||||
package ffser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"91porn-server/common/crypt"
|
||||
)
|
||||
|
||||
// AvFormat 调用ffmpeg 获取信息
|
||||
func AvFormat(fpath string) (resloution string, playTime uint, err error) {
|
||||
args := makeArgs(fpath, []string{"width", "height", "duration"}...)
|
||||
fmt.Printf("%v", args)
|
||||
command := exec.CommandContext(context.Background(), "ffprobe", args...)
|
||||
out, err := command.CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Printf("ffprobe convert to ts wrong %+v", err)
|
||||
return
|
||||
}
|
||||
if strings.Contains(string(out), "Invalid argument") {
|
||||
err = errors.New("ffmpeg convert err")
|
||||
return
|
||||
}
|
||||
result, _ := crypt.JSON2Map(string(out))
|
||||
if v, ok := result["streams"].([]interface{}); ok {
|
||||
if v2, ok := v[0].(map[string]interface{}); ok {
|
||||
width, _ := json.Marshal(v2["width"])
|
||||
height, _ := json.Marshal(v2["height"])
|
||||
duration, _ := v2["duration"].(string)
|
||||
resloution = string(width) + "*" + string(height)
|
||||
pt, _ := strconv.ParseFloat(string(duration), 32)
|
||||
playTime = uint(math.Ceil(pt))
|
||||
fmt.Println(playTime)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func makeArgs(path string, command ...string) []string {
|
||||
com := strings.Join(command, ",")
|
||||
return []string{
|
||||
"-v", "quiet",
|
||||
"-v", "quiet",
|
||||
"-select_streams", "v", "-show_entries",
|
||||
"stream=" + com,
|
||||
"-i", path,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user