media: convert png and jpeg stickers to webp without ffmpeg

This commit is contained in:
Toni Spets
2025-05-22 13:52:51 +03:00
parent 9e719429e7
commit 11f105c0e7
4 changed files with 24 additions and 1 deletions
+3
View File
@@ -115,6 +115,9 @@ var fileCaps = event.FileFeatureMap{
event.CapMsgSticker: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"image/webp": event.CapLevelFullySupported,
// These are converted to webp
"image/jpeg": event.CapLevelPartialSupport,
"image/png": event.CapLevelPartialSupport,
// TODO
//"video/lottie+json": event.CapLevelFullySupported,
//"video/webm": event.CapLevelFullySupported,
+18 -1
View File
@@ -38,6 +38,7 @@ import (
"github.com/rs/zerolog"
"go.mau.fi/util/ffmpeg"
"go.mau.fi/util/variationselector"
"go.mau.fi/webp"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
@@ -75,7 +76,23 @@ func (t *TelegramClient) transferMediaToTelegram(ctx context.Context, content *e
filename := getMediaFilename(content)
err := t.main.Bridge.Bot.DownloadMediaToFile(ctx, content.URL, content.File, false, func(f *os.File) (err error) {
uploadFilename := f.Name()
if sticker && content.Info != nil && (content.Info.MimeType != "video/webm" && content.Info.MimeType != "application/x-tgsticker") {
if sticker && content.Info != nil && (content.Info.MimeType == "image/png" || content.Info.MimeType == "image/jpeg") {
tempFile, err := os.CreateTemp("", "telegram-sticker-*")
if err != nil {
return err
}
defer func() {
tempFile.Close()
os.Remove(tempFile.Name())
}()
if image, _, err := image.Decode(f); err != nil {
return fmt.Errorf("failed to decode sticker image: %w", err)
} else if err := webp.Encode(tempFile, image, nil); err != nil {
return fmt.Errorf("failed to encode sticker webp image: %w", err)
}
uploadFilename = tempFile.Name()
content.Info.MimeType = "image/webp"
} else if sticker && content.Info != nil && (content.Info.MimeType != "video/webm" && content.Info.MimeType != "application/x-tgsticker") {
uploadFilename, err = ffmpeg.ConvertPath(ctx, uploadFilename, ".webp", []string{}, []string{}, false)
if err != nil {
return fmt.Errorf("failed to convert sticker to webm: %+w", err)