Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Animated Sticker Size Issues via Gif compression #151

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Added
-----
- Add UOS weixin desktop patch
- Add 'replace_emoticon' flag, disable this flag to stop emoticon conversion
- Add compression for gif files to avoid 1 MB limit

Changed
-------
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ EWS 是兼容 EH Forwarder Bot 的微信从端,基于逆向工程的微
- ffmpeg
- libmagic
- pillow
- gifsicle

安装与启用
----------
Expand Down
7 changes: 3 additions & 4 deletions efb_wechat_slave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .__version__ import __version__
from .chats import ChatManager
from .slave_message import SlaveMessageManager
from .utils import ExperimentalFlagsManager
from .utils import ExperimentalFlagsManager, gif_conversion
from .vendor import wxpy
from .vendor.wxpy import ResponseError
from .vendor.wxpy.utils import PuidMap
Expand Down Expand Up @@ -436,9 +436,6 @@ def send_message(self, msg: Message) -> Message:
self.logger.debug(
'[%s] Image converted from %s to GIF', msg.uid, msg.mime)
file.close()
if f.seek(0, 2) > self.MAX_FILE_SIZE:
raise EFBMessageError(
self._("Image size is too large. (IS02)"))
f.seek(0)
r.append(self._bot_send_image(chat, f.name, f))
finally:
Expand All @@ -464,6 +461,8 @@ def send_message(self, msg: Message) -> Message:
if not file.closed:
file.close()
else:
if msg.mime == "image/gif" and file.seek(0, 2) > 1048576:
file = gif_conversion(file)
try:
if file.seek(0, 2) > self.MAX_FILE_SIZE:
raise EFBMessageError(
Expand Down
67 changes: 66 additions & 1 deletion efb_wechat_slave/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import base64
import io
import os
import subprocess
import json
from typing import Dict, Any, TYPE_CHECKING, List
from tempfile import NamedTemporaryFile
from typing import Dict, Any, TYPE_CHECKING, List, IO


from ehforwarderbot.types import MessageID
from .vendor.itchat import utils as itchat_utils
Expand Down Expand Up @@ -221,3 +224,65 @@ def print_st():
res += base64.b64encode(file.getvalue()).decode()
res += print_st()
return res


if os.name == "nt":
# Workaround for Windows which cannot open the same file as "read" twice.
# Using stdin/stdout pipe for IO with ffmpeg.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dose this still use ffmpeg?

# Said to be only working with a few encodings. It seems that Telegram GIF
# (MP4, h264, soundless) luckily felt in that range.
#
# See: https://etm.1a23.studio/issues/90

def gif_conversion(file: IO[bytes]) -> IO[bytes]:
"""Convert Telegram GIF to real GIF, the NT way."""
file.seek(0)
new_file_size = os.path.getsize(file.name)
print(f"file_size: {new_file_size/1024}KB")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logging for debug logs. Avoid using print directly.

if new_file_size > 1024 * 1024:
# try to use gifsicle lossy compression
compress_file = NamedTemporaryFile(suffix='.gif')
subprocess.run(["gifsicle", "--resize-method=catrom", "--lossy=100", "-O2", "-o", compress_file.name, file.name], check=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not using std I/O piping as commented above.

new_file_size = os.path.getsize(compress_file.name)
if new_file_size > 1024 * 1024:
scales = [512, 480, 400, 360, 300, 256, 250, 200, 150, 100]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are these values defined?

for scale in scales:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use binary search here?

subprocess.run(["gifsicle", "--resize-method=catrom", "--resize-fit", f"{scale}x{scale}", "--lossy=100", "-O2", "-o", compress_file.name, file.name], check=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not using std I/O piping as commented above.

new_file_size = os.path.getsize(compress_file.name)
print(f"new_file_size: {new_file_size/1024}KB after resize to {scale}x{scale}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logging for debug logs. Avoid using print directly.

if new_file_size < 1024 * 1024:
break
file.close()
file = compress_file
if new_file_size > 1024 * 1024:
raise EFBMessageError(
self._("Image size is too large. (IS02)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a different error code for every new raise.

file.seek(0)
return file

else:
def gif_conversion(file: IO[bytes]) -> IO[bytes]:
"""Convert Telegram GIF to real GIF, the non-NT way."""
file.seek(0)
new_file_size = os.path.getsize(file.name)
print(f"file_size: {new_file_size/1024}KB")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logging for debug logs. Avoid using print directly.

if new_file_size > 1024 * 1024:
# try to use gifsicle lossy compression
compress_file = NamedTemporaryFile(suffix='.gif')
subprocess.run(["gifsicle", "--resize-method=catrom", "--lossy=100", "-O2", "-o", compress_file.name, file.name], check=True)
new_file_size = os.path.getsize(compress_file.name)
if new_file_size > 1024 * 1024:
scales = [512, 480, 400, 360, 300, 256, 250, 200, 150, 100]
for scale in scales:
subprocess.run(["gifsicle", "--resize-method=catrom", "--resize-fit", f"{scale}x{scale}", "--lossy=100", "-O2", "-o", compress_file.name, file.name], check=True)
new_file_size = os.path.getsize(compress_file.name)
print(f"new_file_size: {new_file_size/1024}KB after resize to {scale}x{scale}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logging for debug logs. Avoid using print directly.

if new_file_size < 1024 * 1024:
break
file.close()
file = compress_file
if new_file_size > 1024 * 1024:
raise EFBMessageError(
self._("Image size is too large. (IS02)"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a different error code for every new raise.

file.seek(0)
return file