-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoResize.go
165 lines (145 loc) · 4.11 KB
/
videoResize.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mediaResize
import (
"fmt"
"net/http"
"os"
"strings"
)
// ========================
//
// 批量处理图片
// paths []string 原图片路径
// newPaths []string 新图片路径
// formats []string 图片格式
// maxWHs []MediaWH 图片宽高
// quality int 图片质量
// isPrint bool 是否打印错误及提示信息
// 返回值 []string 新图片路径
// 返回值 error 错误信息
func VideoResizes(paths []string, newPaths []string, formats []string, maxWHs []MediaWH, quality int, isPrint bool) ([][]string, [][]string, [][]string, error) {
newImagePath := [][]string{}
newsizes := [][]string{}
newformats := [][]string{}
for i := 0; i < len(paths); i++ {
newpaths, sizes, formats, err := VideoResize(paths[i], newPaths[i], formats, maxWHs, quality, isPrint)
if err != nil {
return newImagePath, newsizes, newformats, err
}
newImagePath = append(newImagePath, newpaths)
newsizes = append(newsizes, sizes)
newformats = append(newformats, formats)
}
return newImagePath, newsizes, newformats, nil
}
// ========================
//
// 处理图片
// paths string 原图片路径
// newPaths string 新图片路径
// formats []string 图片格式
// maxWHs []MediaWH 图片宽高
// quality int 图片质量
// isPrint bool 是否打印错误及提示信息
// 返回值 []string 新图片路径
// 返回值 error 错误信息
func VideoResize(path string, newPath string, formats []string, maxWHs []MediaWH, codeRate int, isPrint bool) ([]string, []string, []string, error) {
newImagePath := []string{}
sizes := []string{}
newformats := []string{}
file, err := os.Open(path)
if err != nil {
// fmt.Println("os.Open failed:", err)
file.Close()
return newImagePath, sizes, newformats, err
}
info, err := file.Stat()
if err != nil {
file.Close()
return newImagePath, sizes, newformats, err
}
buffer := make([]byte, info.Size())
_, err = file.Read(buffer)
file.Close()
if err != nil {
return newImagePath, sizes, newformats, err
}
contentType := http.DetectContentType(buffer)
fmt.Println("contentType:", contentType)
// Mediatypes := strings.Split(contentType, "/")
// fExt := "mp4"
// if len(Mediatypes) > 1 {
// fExt = strings.ToLower(Mediatypes[1])
// }
// 解析图片宽高后,进行图片缩放
videowh, err := DecodeFileWidthHeight(path, contentType)
if err != nil {
if isPrint {
fmt.Println("1 DecodeFileWidthHeight failed:", err)
}
return newImagePath, sizes, newformats, err
}
exists := map[string]bool{}
sizeNamei := 0
for i := 0; i < len(maxWHs); i++ {
var (
videoSize string = ""
w int = videowh.Width
h int = videowh.Height
)
switch sizeNamei {
case 0:
videoSize = "S"
case 1:
videoSize = "M"
case 2:
videoSize = "L"
default:
for ii := 2; ii < i; ii++ {
videoSize += "X"
}
videoSize += "L"
}
fmt.Println("videoSize:", videoSize)
if isPrint && i == 0 {
fmt.Println("Image size is:", videowh.Width, "x", videowh.Height)
}
if maxWHs[i].Width < 0 || maxWHs[i].Height < 0 {
// 不进行图片缩放
videoSize = "R"
sizeNamei--
sizes = append(sizes, videoSize)
} else {
w, h = calcResolutionRatio(videowh.Width, videowh.Height, maxWHs[i].Width, maxWHs[i].Height)
fmt.Println(">>>>> calcResolutionRatio", w, h)
sizes = append(sizes, videoSize)
}
sizeNamei++
// isRformat := false
// 处理视频并保存到指定地址
for _, v := range formats {
// if fExt == v {
// isRformat = true
// }
pathList := strings.Split(newPath, ".")
pathList[len(pathList)-1] = v
resizePath := strings.Join(pathList, ".")
resizePath = strings.Replace(resizePath, ".", "."+videoSize+".", -1)
_, err = Resize(path, resizePath, contentType, codeRate, w, h)
if err != nil {
if isPrint {
fmt.Println("Resize failed:", err)
}
return newImagePath, sizes, newformats, err
}
if isPrint {
fmt.Println("save video:", path)
}
newImagePath = append(newImagePath, resizePath)
if _, ok := exists[v]; !ok {
newformats = append(newformats, v)
exists[v] = true
}
}
}
return newImagePath, sizes, newformats, nil
}