Fix Animated Sticker Size Issues via Gif compression#151
Fix Animated Sticker Size Issues via Gif compression#151Ovler-Young wants to merge 9 commits intoehForwarderBot:masterfrom
Conversation
blueset
left a comment
There was a problem hiding this comment.
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. |
| 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) |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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] |
| 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: |
There was a problem hiding this comment.
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)")) |
There was a problem hiding this comment.
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)")) |
There was a problem hiding this comment.
Use a different error code for every new raise.
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.
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 发送动态表情贴图到微信时遇到的"文件过大"问题。