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

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