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.
74 lines
1.6 KiB
74 lines
1.6 KiB
package tools
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"recook/internal/static_path"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
FfmpegSplitStart = "Duration: "
|
|
FfmpegSplitEnd = ", start:"
|
|
)
|
|
|
|
func ParseVideoDuration(path string) (second int, err error) {
|
|
command := fmt.Sprintf("ffmpeg -i %v", path)
|
|
|
|
cmd := exec.Command("bash", "-c", command)
|
|
w := bytes.NewBuffer(nil)
|
|
cmd.Stderr = w
|
|
_ = cmd.Run()
|
|
|
|
res := string(w.Bytes())
|
|
res = strings.Split(res, FfmpegSplitStart)[1]
|
|
res = strings.Split(res, FfmpegSplitEnd)[0]
|
|
|
|
// 00:01:04.27'
|
|
res = strings.Split(res, ".")[0]
|
|
array := strings.Split(res, ":")
|
|
h := array[0]
|
|
m := array[1]
|
|
s := array[2]
|
|
|
|
hour, err := strconv.Atoi(h)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
// 不允许超过一小时的
|
|
if hour > 0 {
|
|
err = fmt.Errorf("视频过长")
|
|
return 0, err
|
|
}
|
|
|
|
min, err := strconv.Atoi(m)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
sec, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return min*60 + sec, nil
|
|
}
|
|
|
|
// GenerateVideoThumbnail ffmpeg -i ec64616125dba12b171f24a60bdfd08c.mp4 -y -f image2 -t 0.001 -s 800x450 test.jpg
|
|
func GenerateVideoThumbnail(path string) string {
|
|
prename := path[7:]
|
|
name := prename[:len(prename)-4]
|
|
outPath := filepath.Join(static_path.Dir.Root, static_path.Dir.Photo, name) + ".jpg"
|
|
|
|
command := fmt.Sprintf("ffmpeg -i %v -y -f image2 -t 0.001 -s 800x450 %v", filepath.Join(static_path.Dir.Root, path), outPath)
|
|
|
|
cmd := exec.Command("bash", "-c", command)
|
|
w := bytes.NewBuffer(nil)
|
|
cmd.Stderr = w
|
|
_ = cmd.Run()
|
|
|
|
return filepath.Join(static_path.Dir.Photo, name) + ".jpg"
|
|
}
|