feat: add MP4 metadata extraction and integrate gomedia for video handling

This commit is contained in:
krau
2025-11-22 15:42:02 +08:00
parent 4f314bd37f
commit 7d57ad30a9
4 changed files with 50 additions and 0 deletions

1
go.mod
View File

@@ -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
)

2
go.sum
View File

@@ -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=

View File

@@ -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"):

36
storage/telegram/util.go Normal file
View File

@@ -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")
}