mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-09 22:41:55 +08:00
feat: add MP4 metadata extraction and integrate gomedia for video handling
This commit is contained in:
@@ -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
36
storage/telegram/util.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user