feat: oss video snapshot

pull/818/head
withchao 2 years ago
parent 89c18ac358
commit cb929e4c9c

@ -45,6 +45,11 @@ const (
imageWebp = "webp" imageWebp = "webp"
) )
const (
videoSnapshotImagePng = "png"
videoSnapshotImageJpg = "jpg"
)
func NewOSS() (s3.Interface, error) { func NewOSS() (s3.Interface, error) {
conf := config.Config.Object.Oss conf := config.Config.Object.Oss
if conf.BucketURL == "" { if conf.BucketURL == "" {
@ -271,7 +276,7 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration,
var opts []oss.Option var opts []oss.Option
if opt != nil { if opt != nil {
if opt.Image != nil { if opt.Image != nil {
// https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji // 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji
var format string var format string
switch opt.Image.Format { switch opt.Image.Format {
case case
@ -281,29 +286,54 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration,
imageGif, imageGif,
imageWebp: imageWebp:
format = opt.Image.Format format = opt.Image.Format
default:
opt.Image.Format = imageJpg
}
opt.ContentType = "image/" + format opt.ContentType = "image/" + format
if opt.Filename == "" { if opt.Filename == "" {
opt.Filename = filepath.Base(name) + "." + format opt.Filename = filepath.Base(name) + "." + opt.Video.ImageFormat
} } else if filepath.Ext(opt.Filename) != "."+opt.Video.ImageFormat {
} opt.Filename += "." + opt.Video.ImageFormat
var wh []string }
if opt.Image.Width > 0 { // https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit
wh = append(wh, "w_"+strconv.Itoa(opt.Image.Width)) process := "image/resize,m_lfit"
} if opt.Video.Width > 0 {
if opt.Image.Height > 0 { process += ",w_" + strconv.Itoa(opt.Image.Width)
wh = append(wh, "h_"+strconv.Itoa(opt.Image.Height)) }
if opt.Video.Height > 0 {
process += ",h_" + strconv.Itoa(opt.Image.Height)
}
process += ",format," + format
opts = append(opts, oss.Process(process))
}
if opt.Video != nil {
// 文档地址: https://help.aliyun.com/zh/oss/user-guide/video-snapshots?spm=a2c4g.11186623.0.0.23f743b0BR5WxX
// x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
millisecond := int(opt.Video.Time / time.Millisecond)
if millisecond < 0 {
millisecond = 0
}
switch opt.Video.ImageFormat {
case videoSnapshotImageJpg, videoSnapshotImagePng:
default:
opt.Video.ImageFormat = videoSnapshotImageJpg
} }
if len(format)+len(wh) > 0 { opt.ContentType = "image/" + opt.Video.ImageFormat
style := make([]string, 0, 3) if opt.Filename == "" {
style = append(style, "image") opt.Filename = filepath.Base(name) + "." + opt.Video.ImageFormat
if len(wh) > 0 { } else if filepath.Ext(opt.Filename) != "."+opt.Video.ImageFormat {
style = append(style, "resize,m_lfit,"+strings.Join(wh, ",")) opt.Filename += "." + opt.Video.ImageFormat
} }
if format != "" { process := "video/snapshot,t_" + strconv.Itoa(millisecond) + ",f_" + opt.Video.ImageFormat
style = append(style, "format,"+format) if opt.Video.Width > 0 {
process += ",w_" + strconv.Itoa(opt.Video.Width)
} }
opts = append(opts, oss.Process(strings.Join(style, "/"))) if opt.Video.Height > 0 {
process += ",h_" + strconv.Itoa(opt.Video.Height)
} }
process += ",m_fast"
fmt.Println(process)
opts = append(opts, oss.Process(process))
} }
if opt.ContentType != "" { if opt.ContentType != "" {
opts = append(opts, oss.ResponseContentType(opt.ContentType)) opts = append(opts, oss.ResponseContentType(opt.ContentType))

Loading…
Cancel
Save