-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoUtil.go
110 lines (105 loc) · 2.39 KB
/
VideoUtil.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package videoutil
import (
"encoding/json"
"httpfs/util/osutil"
"strconv"
"strings"
)
type VideoInfo struct {
Video map[string]interface{}
Audio map[string]interface{}
Format map[string]interface{}
}
func GetVideoInfo(media string) (VideoInfo, error) {
cmd := "ffprobe -v quiet -print_format json -show_format -show_streams " + media
// state, stdout, stderr = run(c)
// video_info = json.loads(stdout)
// streams = {s['codec_type'] : s for s in video_info['streams']}
// format = video_info['format']
info := VideoInfo{}
_, stdout, _, err := osutil.Exec(cmd)
if err != nil {
return info, err
}
// fmt.Println(stdout, err)
r := make(map[string]interface{})
json.Unmarshal([]byte(stdout), &r)
info.Format = r["format"].(map[string]interface{})
streams := r["streams"].([]interface{})
for i, s := range streams {
v := s.(map[string]interface{})
if "video" == v["codec_type"] {
info.Video = streams[i].(map[string]interface{})
}
if "audio" == v["codec_type"] {
info.Audio = streams[i].(map[string]interface{})
}
}
// return streams, format
return info, nil
}
func (v VideoInfo) Size() int64 {
if v.Format != nil {
r, _ := strconv.ParseInt(v.Format["size"].(string), 10, 64)
return r
}
return 0
}
func (v VideoInfo) Duration() float64 {
if v.Format != nil {
r, _ := strconv.ParseFloat(v.Format["duration"].(string), 64)
return r
}
return 0
}
func (v VideoInfo) BitRate() int {
if v.Format != nil {
r, _ := strconv.Atoi(v.Format["bit_rate"].(string))
return r
}
return 0
}
func (v VideoInfo) Width() int {
if v.Video != nil {
return int(v.Video["width"].(float64))
}
return 0
}
func (v VideoInfo) Height() int {
if v.Video != nil {
return int(v.Video["height"].(float64))
}
return 0
}
func (v VideoInfo) VideoBitRate() int {
if v.Video != nil {
r, _ := strconv.Atoi(v.Video["bit_rate"].(string))
return r
}
return 0
}
func (v VideoInfo) VideoCodec() string {
if v.Video != nil {
return strings.ToLower(v.Video["codec_name"].(string))
}
return ""
}
func (v VideoInfo) AudioBitRate() int {
if v.Audio != nil {
r, _ := strconv.Atoi(v.Audio["bit_rate"].(string))
return r
}
return 0
}
func (v VideoInfo) AudioCodec() string {
if v.Audio != nil {
return strings.ToLower(v.Audio["codec_name"].(string))
}
return ""
}
func (v VideoInfo) IsH264() bool {
return "h264" == v.VideoCodec()
}
func (v VideoInfo) IsAac() bool {
return "aac" == v.AudioCodec()
}