feat(uploader): mp4 dur,width,height info

This commit is contained in:
iyear 2022-10-12 10:14:02 +08:00
parent 841a7732ec
commit 6dd9bbfb07
5 changed files with 41 additions and 2 deletions

1
go.mod
View File

@ -66,6 +66,7 @@ require (
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/yapingcat/gomedia v0.0.0-20221011015708-089ed08359e1 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect

2
go.sum
View File

@ -308,6 +308,8 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/yapingcat/gomedia v0.0.0-20221011015708-089ed08359e1 h1:v+Fn3+hyNp3a0xXm5kPFMCYBHUnVr/zAQvCMIxWPYTE=
github.com/yapingcat/gomedia v0.0.0-20221011015708-089ed08359e1/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

View File

@ -11,8 +11,14 @@ type Iter interface {
Total(ctx context.Context) int
}
type ReadSeekCloser interface {
io.Reader
io.Seeker
io.Closer
}
type Item struct {
R io.ReadCloser
R ReadSeekCloser
Name string
MIME string
Size int64

View File

@ -114,7 +114,15 @@ func (u *Uploader) upload(ctx context.Context, item *Item) error {
var media message.MediaOption = doc
if utils.Media.IsVideo(item.MIME) {
media = doc.Video().SupportsStreaming()
// reset reader
if _, err = item.R.Seek(0, io.SeekStart); err != nil {
return err
}
dur, w, h, err := utils.Media.GetMP4Info(item.R)
if err != nil {
return err
}
media = doc.Video().DurationSeconds(dur).Resolution(w, h).SupportsStreaming()
}
if utils.Media.IsAudio(item.MIME) {
media = doc.Audio().Title(utils.FS.GetNameWithoutExt(item.Name))

View File

@ -1,6 +1,9 @@
package utils
import (
"fmt"
"github.com/yapingcat/gomedia/go-mp4"
"io"
"strings"
)
@ -35,3 +38,22 @@ func (m media) IsImage(mime string) bool {
return primary == "image" && ok
}
// GetMP4Info returns duration, width, height, error
func (m media) GetMP4Info(r io.ReadSeeker) (int, int, int, error) {
d := mp4.CreateMp4Demuxer(r)
tracks, err := d.ReadHead()
if err != nil {
return 0, 0, 0, err
}
for _, track := range tracks {
if track.Cid == mp4.MP4_CODEC_H264 {
info := d.GetMp4Info()
return int(info.Duration / info.Timescale), int(track.Width), int(track.Height), nil
}
}
return 0, 0, 0, fmt.Errorf("no h264 track found")
}