-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
ffprobe.go
64 lines (54 loc) · 1002 Bytes
/
ffprobe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build !windows
// +build !windows
package utils
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
"time"
)
type ffprobeInfo struct {
Format struct {
Duration string `json:"duration"`
} `json:"format"`
}
func DurationForMedia(f string) (string, error) {
_, err := os.Stat(f)
if err != nil {
return "", err
}
cmd := exec.Command(
"ffprobe",
"-loglevel", "error",
"-show_format",
"-of", "json",
f,
)
output, err := cmd.Output()
if err != nil {
return "", err
}
var info ffprobeInfo
if err := json.Unmarshal(output, &info); err != nil {
return "", err
}
ff, err := strconv.ParseFloat(info.Format.Duration, 64)
if err != nil {
return "", err
}
duration := time.Duration(ff * float64(time.Second))
return formatDuration(duration), nil
}
func formatDuration(t time.Duration) string {
t /= time.Millisecond
ms := t % 1000
t /= 1000
s := t % 60
t /= 60
m := t % 60
t /= 60
h := t
return fmt.Sprintf("%02d:%02d:%02d.%03d", h, m, s, ms)
}