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

Conversation

Ovler-Young
Copy link

The error when sending animated stickers from Telegram to WeChat via EFB is often caused by WeChat's 1 MB size limit for GIF files used as stickers. Other users have also reported similar issues.

image
After investigating, I found that reducing the frame rate and image size of the GIF can help decrease the file size and mitigate these errors, which is the main approach I took in this PR.

This pull request should help alleviate the "too big file" issue when sending animated stickers from Telegram to WeChat via EFB.


通过 EFB 从 Telegram 发送动态表情贴图到微信时, 经常会出现错误, 这主要是由于微信对作为表情贴图的 GIF 文件有 1 MB 的大小限制所导致。其他用户也反馈了类似的问题

经过调查, 我发现降低 GIF 的帧率和图片大小可以有效减小文件体积, 从而避免这类错误。这也是我在该 PR 中采取的主要方法。

该 Pull Request 应该可以缓解从 Telegram 通过 EFB 发送动态表情贴图到微信时遇到的"文件过大"问题。

Copy link
Member

@blueset blueset left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution!


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?

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.

"""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.

"""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:
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)
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.

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.

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]
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?

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:
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?

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 = 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants