diff --git a/go.mod b/go.mod index 5809595..42fc9b1 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/rs/xid v1.6.0 github.com/spf13/cobra v1.10.1 github.com/spf13/viper v1.21.0 + github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c golang.org/x/net v0.46.0 golang.org/x/time v0.14.0 ) diff --git a/go.sum b/go.sum index 626270e..c0c6162 100644 --- a/go.sum +++ b/go.sum @@ -265,6 +265,8 @@ github.com/ulikunitz/xz v0.5.15 h1:9DNdB5s+SgV3bQ2ApL10xRc35ck0DuIX/isZvIk+ubY= github.com/ulikunitz/xz v0.5.15/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c h1:xA2TJS9Hu/ivzaZIrDcwvpJ3Fnpsk5fDOJ4iSnL6J0w= +github.com/yapingcat/gomedia v0.0.0-20240906162731-17feea57090c/go.mod h1:WSZ59bidJOO40JSJmLqlkBJrjZCtjbKKkygEMfzY/kc= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= diff --git a/storage/telegram/telegram.go b/storage/telegram/telegram.go index b192359..779e6fe 100644 --- a/storage/telegram/telegram.go +++ b/storage/telegram/telegram.go @@ -147,6 +147,17 @@ func (t *Telegram) Save(ctx context.Context, r io.Reader, storagePath string) er switch mtypeStr := mtype.String(); { case strings.HasPrefix(mtypeStr, "video/"): media = docb.Video().SupportsStreaming() + rs.Seek(0, io.SeekStart) + switch mtypeStr { + case "video/mp4": + info, err := getMP4Info(rs) + if err == nil { + media = docb.Video(). + Duration(time.Duration(info.Duration)*time.Second). + Resolution(info.Width, info.Height). + SupportsStreaming() + } + } case strings.HasPrefix(mtypeStr, "audio/"): media = docb.Audio().Title(filename) case strings.HasPrefix(mtypeStr, "image/") && !strings.HasSuffix(mtypeStr, "webp"): diff --git a/storage/telegram/util.go b/storage/telegram/util.go new file mode 100644 index 0000000..c92e602 --- /dev/null +++ b/storage/telegram/util.go @@ -0,0 +1,36 @@ +package telegram + +import ( + "fmt" + "io" + + "github.com/yapingcat/gomedia/go-mp4" +) + +type MP4Info struct { + Duration int + Width int + Height int +} + +func getMP4Info(r io.ReadSeeker) (*MP4Info, error) { + d := mp4.CreateMp4Demuxer(r) + + tracks, err := d.ReadHead() + if err != nil { + return nil, err + } + + for _, track := range tracks { + if track.Cid == mp4.MP4_CODEC_H264 { + info := d.GetMp4Info() + return &MP4Info{ + Duration: int(info.Duration / info.Timescale), + Width: int(track.Width), + Height: int(track.Height), + }, nil + } + } + + return nil, fmt.Errorf("no h264 track found") +}