Skip to content

Commit

Permalink
chore: 可能完全优化了 qq gif 到 tg 的显示
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Jan 24, 2025
1 parent 47b611c commit 05b226e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
4 changes: 4 additions & 0 deletions main/src/client/Telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,8 @@ export default class Telegram {
stickerset: new Api.InputStickerSetShortName({ shortName: handle }),
})) as Api.messages.StickerSet;
}

public async uploadFile(fileParams: Parameters<typeof this.client.uploadFile>[0]) {
return await this.client.uploadFile(fileParams);
}
}
15 changes: 9 additions & 6 deletions main/src/encoding/convertWithFfmpeg.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import ffmpeg from 'fluent-ffmpeg';

export default function (sourcePath: string, targetPath: string, format: string){
export default function (sourcePath: string, targetPath: string, format: string, cv?: string) {
return new Promise<void>(resolve => {
ffmpeg(sourcePath).toFormat(format).save(targetPath)
.on('end', () => {
resolve();
})
})
const ff = ffmpeg(sourcePath).toFormat(format).save(targetPath);
if (cv) {
ff.videoCodec(cv);
}
return ff.on('end', () => {
resolve();
});
});
}
16 changes: 15 additions & 1 deletion main/src/helpers/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fsP from 'fs/promises';
import convertWithFfmpeg from '../encoding/convertWithFfmpeg';
import tgsToGif from '../encoding/tgsToGif';
import env from '../models/env';
import { fileTypeFromBuffer } from 'file-type';
import { fileTypeFromBuffer, fileTypeFromFile } from 'file-type';

const CACHE_PATH = env.CACHE_DIR;
fs.mkdirSync(CACHE_PATH, { recursive: true });
Expand Down Expand Up @@ -49,6 +49,20 @@ const convert = {
cachedConvert(key + '.webp', async (convertedPath) => {
await sharp(await imageData()).webp().toFile(convertedPath);
}),
webm: (key: string, filePath: string) =>
cachedConvert(key + '.webm', async (convertedPath) => {
await convertWithFfmpeg(filePath, convertedPath, 'webm', 'libvpx-vp9');
}),
webpOrWebm: async (key: string, imageData: () => Promise<Buffer | Uint8Array>) => {
const filePath = await convert.cachedBuffer(key, imageData);
const fileType = await fileTypeFromFile(filePath);
if (fileType.mime === 'image/gif') {
return await convert.webm(key, filePath);
}
else {
return await convert.webp(key, async () => filePath);
}
},
customEmoji: async (key: string, imageData: () => Promise<Buffer | Uint8Array | string>, useSmallSize: boolean) => {
if (useSmallSize) {
const pathPng = path.join(CACHE_PATH, key + '@50.png');
Expand Down
24 changes: 20 additions & 4 deletions main/src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import helper from '../helpers/forwardHelper';
import db from '../models/db';
import { Button } from 'telegram/tl/custom/button';
import { SendMessageParams } from 'telegram/client/messages';
import { Api } from 'telegram';
import { Api, utils } from 'telegram';
import { file as createTempFileBase, FileResult } from 'tmp-promise';
// @ts-ignore
import eviltransform from 'eviltransform';
Expand Down Expand Up @@ -48,6 +48,8 @@ import { FaceElemEx } from '../client/NapCatClient/convert';
import nameColor from '../constants/nameColor';
import memberRoleCache from '../helpers/memberRoleCache';
import { GroupRole } from '@icqqjs/icqq/lib/common';
import path from 'path';
import { fileTypeFromFile } from 'file-type';

const NOT_CHAINABLE_ELEMENTS = ['flash', 'record', 'video', 'location', 'share', 'json', 'xml', 'poke'];
const IMAGE_MIMES = ['image/jpeg', 'image/png', 'image/apng', 'image/webp', 'image/gif', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/avif', 'image/heic', 'image/heif'];
Expand Down Expand Up @@ -344,14 +346,27 @@ export default class ForwardService {
});
}
try {
if (elem.type === 'image' && elem.asface
&& !(elem.file as string).toLowerCase().endsWith('.gif')
if (elem.type === 'image' && (elem.asface || 'emoji_package_id' in elem)
// 同时存在文字消息就不作为 sticker 发送
&& !event.message.some(it => it.type === 'text')
// 防止在 TG 中一起发送多个 sticker 失败
&& event.message.filter(it => it.type === 'image').length === 1
) {
useSticker(await convert.webp(elem.file as string, () => fetchFile(elem.url)));
const res = await convert.webpOrWebm(elem.file as string, () => fetchFile(elem.url));
const stat = await fsP.stat(res);
const upload = await pair.tg.parent.uploadFile({
file: new CustomFile(path.basename(res), stat.size, res),
workers: 2,
});
const fileType = await fileTypeFromFile(res);
useSticker(new Api.InputMediaUploadedDocument({
file: upload,
mimeType: fileType.mime,
attributes: [new Api.DocumentAttributeSticker({
alt: '猫',
stickerset: new Api.InputStickerSetEmpty(),
})],
}));
}
else {
const file = await helper.downloadToCustomFile(url, !(message || messageHeader));
Expand Down Expand Up @@ -774,6 +789,7 @@ export default class ForwardService {
}
else if (message.video || message.videoNote || message.gif) {
const file = message.video || message.videoNote || message.gif;
console.log(file);
const face = this.getFaceByTgFileId(file.id);
if (face) {
chain.push(face);
Expand Down

0 comments on commit 05b226e

Please sign in to comment.